Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
018e832d
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
018e832d
编写于
4月 12, 2012
作者:
J
Jon Leighton
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2733 from dasch/improve-delegate
Improve the performance of #delegate
上级
65be11ff
300868d9
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
35 addition
and
10 deletion
+35
-10
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+2
-0
activesupport/lib/active_support/core_ext/module/delegation.rb
...esupport/lib/active_support/core_ext/module/delegation.rb
+14
-10
activesupport/test/core_ext/module_test.rb
activesupport/test/core_ext/module_test.rb
+19
-0
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
018e832d
## Rails 4.0.0 (unreleased) ##
*
Make Module#delegate stop using
`send`
- can no longer delegate to private methods.
*dasch*
*
AS::Callbacks: deprecate
`:rescuable`
option.
*Bogdan Gusiev*
*
Adds Integer#ordinal to get the ordinal suffix string of an integer.
*Tim Gildea*
...
...
activesupport/lib/active_support/core_ext/module/delegation.rb
浏览文件 @
018e832d
class
Module
# Provides a delegate class method to easily expose contained objects' methods
# Provides a delegate class method to easily expose contained objects'
public
methods
# as your own. Pass one or more methods (specified as symbols or strings)
# and the name of the target object via the <tt>:to</tt> option (also a symbol
# or string). At least one method and the <tt>:to</tt> option are required.
...
...
@@ -124,23 +124,27 @@ def delegate(*methods)
file
,
line
=
caller
.
first
.
split
(
':'
,
2
)
line
=
line
.
to_i
if
allow_nil
methods
.
each
do
|
method
|
methods
.
each
do
|
method
|
method
=
method
.
to_s
# Attribute writer methods only accept one argument. Makes sure []=
# methods still accept two arguments.
definition
=
(
method
=~
/[^\]]=$/
)
?
"arg"
:
"*args, &block"
if
allow_nil
module_eval
(
<<-
EOS
,
file
,
line
-
2
)
def
#{
method_prefix
}#{
method
}
(
*args, &block
) # def customer_name(*args, &block)
def
#{
method_prefix
}#{
method
}
(
#{
definition
}
) # def customer_name(*args, &block)
if
#{
to
}
||
#{
to
}
.respond_to?(:
#{
method
}
) # if client || client.respond_to?(:name)
#{
to
}
.
__send__(:
#{
method
}
, *args, &block) # client.__send__(:name,
*args, &block)
#{
to
}
.
#{
method
}
(
#{
definition
}
) # client.name(
*args, &block)
end # end
end # end
EOS
end
else
methods
.
each
do
|
method
|
else
exception
=
%(raise "#{self}##{method_prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil:
\#
{self.inspect}")
module_eval
(
<<-
EOS
,
file
,
line
-
1
)
def
#{
method_prefix
}#{
method
}
(
*args, &block
) # def customer_name(*args, &block)
#{
to
}
.
__send__(:
#{
method
}
, *args, &block) # client.__send__(:name,
*args, &block)
def
#{
method_prefix
}#{
method
}
(
#{
definition
}
) # def customer_name(*args, &block)
#{
to
}
.
#{
method
}
(
#{
definition
}
) # client.name(
*args, &block)
rescue NoMethodError # rescue NoMethodError
if
#{
to
}
.nil? # if client.nil?
#{
exception
}
# # add helpful message to the exception
...
...
activesupport/test/core_ext/module_test.rb
浏览文件 @
018e832d
...
...
@@ -60,6 +60,14 @@ class Someone < Struct.new(:name, :place)
delegate
:name
,
:to
=>
:client
,
:prefix
=>
false
end
class
ParameterSet
delegate
:[]
,
:[]=
,
:to
=>
:@params
def
initialize
@params
=
{
:foo
=>
"bar"
}
end
end
class
Name
delegate
:upcase
,
:to
=>
:@full_name
...
...
@@ -83,6 +91,17 @@ def test_delegation_to_assignment_method
assert_equal
"Fred"
,
@david
.
place
.
name
end
def
test_delegation_to_index_get_method
@params
=
ParameterSet
.
new
assert_equal
"bar"
,
@params
[
:foo
]
end
def
test_delegation_to_index_set_method
@params
=
ParameterSet
.
new
@params
[
:foo
]
=
"baz"
assert_equal
"baz"
,
@params
[
:foo
]
end
def
test_delegation_down_hierarchy
assert_equal
"CHICAGO"
,
@david
.
upcase
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录