Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
56417b40
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,发现更多精彩内容 >>
提交
56417b40
编写于
5月 25, 2012
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #4785 from ayamomiji/add-self-to-allow-method-name-using-ruby-keyword
add `self.` to allow method name using ruby keyword
上级
73db73fe
c140a27f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
26 addition
and
4 deletion
+26
-4
activemodel/lib/active_model/attribute_methods.rb
activemodel/lib/active_model/attribute_methods.rb
+4
-4
activemodel/test/cases/attribute_methods_test.rb
activemodel/test/cases/attribute_methods_test.rb
+22
-0
未找到文件。
activemodel/lib/active_model/attribute_methods.rb
浏览文件 @
56417b40
...
...
@@ -196,7 +196,7 @@ def alias_attribute(new_name, old_name)
attribute_method_matchers
.
each
do
|
matcher
|
matcher_new
=
matcher
.
method_name
(
new_name
).
to_s
matcher_old
=
matcher
.
method_name
(
old_name
).
to_s
define_
optimized_call
self
,
matcher_new
,
matcher_old
define_
proxy_call
false
,
self
,
matcher_new
,
matcher_old
end
end
...
...
@@ -238,7 +238,7 @@ def define_attribute_method(attr_name)
if
respond_to?
(
generate_method
,
true
)
send
(
generate_method
,
attr_name
)
else
define_
optimized_call
generated_attribute_methods
,
method_name
,
matcher
.
method_missing_target
,
attr_name
.
to_s
define_
proxy_call
true
,
generated_attribute_methods
,
method_name
,
matcher
.
method_missing_target
,
attr_name
.
to_s
end
end
end
...
...
@@ -293,7 +293,7 @@ def attribute_method_matcher(method_name) #:nodoc:
# Define a method `name` in `mod` that dispatches to `send`
# using the given `extra` args. This fallbacks `define_method`
# and `send` if the given names cannot be compiled.
def
define_
optimized_call
(
mod
,
name
,
send
,
*
extra
)
#:nodoc:
def
define_
proxy_call
(
include_private
,
mod
,
name
,
send
,
*
extra
)
#:nodoc:
if
name
=~
NAME_COMPILABLE_REGEXP
defn
=
"def
#{
name
}
(*args)"
else
...
...
@@ -303,7 +303,7 @@ def define_optimized_call(mod, name, send, *extra) #:nodoc:
extra
=
(
extra
.
map
(
&
:inspect
)
<<
"*args"
).
join
(
", "
)
if
send
=~
CALL_COMPILABLE_REGEXP
target
=
"
#{
send
}
(
#{
extra
}
)"
target
=
"
#{
"self."
unless
include_private
}#{
send
}
(
#{
extra
}
)"
else
target
=
"send(:'
#{
send
}
',
#{
extra
}
)"
end
...
...
activemodel/test/cases/attribute_methods_test.rb
浏览文件 @
56417b40
...
...
@@ -76,6 +76,19 @@ def attribute(name)
end
end
class
ModelWithRubyKeywordNamedAttributes
include
ActiveModel
::
AttributeMethods
def
attributes
{
:begin
=>
'value of begin'
,
:end
=>
'value of end'
}
end
private
def
attribute
(
name
)
attributes
[
name
.
to_sym
]
end
end
class
ModelWithoutAttributesMethod
include
ActiveModel
::
AttributeMethods
end
...
...
@@ -155,6 +168,15 @@ def foo
assert_equal
"value of foo bar"
,
ModelWithAttributesWithSpaces
.
new
.
foo_bar
end
test
'#alias_attribute works with attributes named as a ruby keyword'
do
ModelWithRubyKeywordNamedAttributes
.
define_attribute_methods
([
:begin
,
:end
])
ModelWithRubyKeywordNamedAttributes
.
alias_attribute
(
:from
,
:begin
)
ModelWithRubyKeywordNamedAttributes
.
alias_attribute
(
:to
,
:end
)
assert_equal
"value of begin"
,
ModelWithRubyKeywordNamedAttributes
.
new
.
from
assert_equal
"value of end"
,
ModelWithRubyKeywordNamedAttributes
.
new
.
to
end
test
'#undefine_attribute_methods removes attribute methods'
do
ModelWithAttributes
.
define_attribute_methods
(
:foo
)
ModelWithAttributes
.
undefine_attribute_methods
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录