Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
80d98450
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,发现更多精彩内容 >>
未验证
提交
80d98450
编写于
1月 06, 2020
作者:
J
John Hawthorn
提交者:
GitHub
1月 06, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #38144 from jhawthorn/mattr_location
Define mattr_* methods at caller's location
上级
be40e8e5
b5b1b020
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
37 addition
and
30 deletion
+37
-30
actionview/test/template/form_options_helper_test.rb
actionview/test/template/form_options_helper_test.rb
+1
-1
activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
...lib/active_support/core_ext/module/attribute_accessors.rb
+23
-29
activesupport/test/core_ext/module/attribute_accessor_test.rb
...vesupport/test/core_ext/module/attribute_accessor_test.rb
+13
-0
未找到文件。
actionview/test/template/form_options_helper_test.rb
浏览文件 @
80d98450
...
...
@@ -57,8 +57,8 @@ def dummy; :test; end
end
def
self
.
prepended
(
base
)
base
.
mattr_accessor
(
:fake_zones
)
class
<<
base
mattr_accessor
(
:fake_zones
)
prepend
ClassMethods
end
end
...
...
activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
浏览文件 @
80d98450
...
...
@@ -48,28 +48,25 @@ class Module
# end
#
# Person.new.hair_colors # => [:brown, :black, :blonde, :red]
def
mattr_reader
(
*
syms
,
instance_reader:
true
,
instance_accessor:
true
,
default:
nil
)
def
mattr_reader
(
*
syms
,
instance_reader:
true
,
instance_accessor:
true
,
default:
nil
,
location:
nil
)
raise
TypeError
,
"module attributes should be defined directly on class, not singleton"
if
singleton_class?
location
||=
caller_locations
(
1
,
1
).
first
definition
=
[]
syms
.
each
do
|
sym
|
raise
NameError
.
new
(
"invalid attribute name:
#{
sym
}
"
)
unless
/\A[_A-Za-z]\w*\z/
.
match?
(
sym
)
class_eval
(
<<-
EOS
,
__FILE__
,
__LINE__
+
1
)
@@
#{
sym
}
= nil unless defined? @@
#{
sym
}
def self.
#{
sym
}
@@
#{
sym
}
end
EOS
definition
<<
"def self.
#{
sym
}
; @@
#{
sym
}
; end"
if
instance_reader
&&
instance_accessor
class_eval
(
<<-
EOS
,
__FILE__
,
__LINE__
+
1
)
def
#{
sym
}
@@
#{
sym
}
end
EOS
definition
<<
"def
#{
sym
}
; @@
#{
sym
}
; end"
end
sym_default_value
=
(
block_given?
&&
default
.
nil?
)
?
yield
:
default
class_variable_set
(
"@@
#{
sym
}
"
,
sym_default_value
)
unless
sym_default_value
.
nil?
class_variable_set
(
"@@
#{
sym
}
"
,
sym_default_value
)
unless
sym_default_value
.
nil?
&&
class_variable_defined?
(
"@@
#{
sym
}
"
)
end
module_eval
(
definition
.
join
(
";"
),
location
.
path
,
location
.
lineno
)
end
alias
:cattr_reader
:mattr_reader
...
...
@@ -115,28 +112,24 @@ def #{sym}
# end
#
# Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red]
def
mattr_writer
(
*
syms
,
instance_writer:
true
,
instance_accessor:
true
,
default:
nil
)
def
mattr_writer
(
*
syms
,
instance_writer:
true
,
instance_accessor:
true
,
default:
nil
,
location:
nil
)
raise
TypeError
,
"module attributes should be defined directly on class, not singleton"
if
singleton_class?
location
||=
caller_locations
(
1
,
1
).
first
definition
=
[]
syms
.
each
do
|
sym
|
raise
NameError
.
new
(
"invalid attribute name:
#{
sym
}
"
)
unless
/\A[_A-Za-z]\w*\z/
.
match?
(
sym
)
class_eval
(
<<-
EOS
,
__FILE__
,
__LINE__
+
1
)
@@
#{
sym
}
= nil unless defined? @@
#{
sym
}
def self.
#{
sym
}
=(obj)
@@
#{
sym
}
= obj
end
EOS
definition
<<
"def self.
#{
sym
}
=(val); @@
#{
sym
}
= val; end"
if
instance_writer
&&
instance_accessor
class_eval
(
<<-
EOS
,
__FILE__
,
__LINE__
+
1
)
def
#{
sym
}
=(obj)
@@
#{
sym
}
= obj
end
EOS
definition
<<
"def
#{
sym
}
=(val); @@
#{
sym
}
= val; end"
end
sym_default_value
=
(
block_given?
&&
default
.
nil?
)
?
yield
:
default
send
(
"
#{
sym
}
="
,
sym_default_value
)
unless
sym_default_value
.
nil?
class_variable_set
(
"@@
#{
sym
}
"
,
sym_default_value
)
unless
sym_default_value
.
nil?
&&
class_variable_defined?
(
"@@
#{
sym
}
"
)
end
module_eval
(
definition
.
join
(
";"
),
location
.
path
,
location
.
lineno
)
end
alias
:cattr_writer
:mattr_writer
...
...
@@ -205,8 +198,9 @@ def #{sym}=(obj)
#
# Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red]
def
mattr_accessor
(
*
syms
,
instance_reader:
true
,
instance_writer:
true
,
instance_accessor:
true
,
default:
nil
,
&
blk
)
mattr_reader
(
*
syms
,
instance_reader:
instance_reader
,
instance_accessor:
instance_accessor
,
default:
default
,
&
blk
)
mattr_writer
(
*
syms
,
instance_writer:
instance_writer
,
instance_accessor:
instance_accessor
,
default:
default
)
location
=
caller_locations
(
1
,
1
).
first
mattr_reader
(
*
syms
,
instance_reader:
instance_reader
,
instance_accessor:
instance_accessor
,
default:
default
,
location:
location
,
&
blk
)
mattr_writer
(
*
syms
,
instance_writer:
instance_writer
,
instance_accessor:
instance_accessor
,
default:
default
,
location:
location
)
end
alias
:cattr_accessor
:mattr_accessor
end
activesupport/test/core_ext/module/attribute_accessor_test.rb
浏览文件 @
80d98450
...
...
@@ -134,4 +134,17 @@ def test_declaring_multiple_attributes_at_once_invokes_the_block_multiple_times
assert_equal
1
,
@module
.
defn1
assert_equal
2
,
@module
.
defn2
end
def
test_declaring_attributes_on_singleton_errors
klass
=
Class
.
new
ex
=
assert_raises
TypeError
do
class
<<
klass
mattr_accessor
:my_attr
end
end
assert_equal
"module attributes should be defined directly on class, not singleton"
,
ex
.
message
assert_not_includes
Module
.
class_variables
,
:@@my_attr
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录