Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
06f06750
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,发现更多精彩内容 >>
提交
06f06750
编写于
2月 20, 2018
作者:
T
Tomas Valent
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add private: true option for ActiveSupport delegate
上级
87de79e9
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
117 addition
and
2 deletion
+117
-2
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+37
-0
activesupport/lib/active_support/core_ext/module/delegation.rb
...esupport/lib/active_support/core_ext/module/delegation.rb
+23
-2
activesupport/test/core_ext/module_test.rb
activesupport/test/core_ext/module_test.rb
+57
-0
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
06f06750
## Rails 6.0.0.alpha (Unreleased) ##
*
Add
`private: true`
option to ActiveSupport's
`delegate`
.
In order to delegate methods as private methods:
class User < ActiveRecord::Base
has_one :profile
delegate :date_of_birth, to: :profile, private: true
def age
Date.today.year - date_of_birth.year
end
end
# User.new.age # => 29
# User.new.date_of_birth
# => NoMethodError: private method `date_of_birth' called for #<User:0x00000008221340>
More information in #31944.
*Tomas Valent*
*
Return all mappings for a timezone identifier in
`country_zones`
Some timezones like `Europe/London` have multiple mappings in
`ActiveSupport::TimeZone::MAPPING` so return all of them instead
of the first one found by using `Hash#value`. e.g:
# Before
ActiveSupport::TimeZone.country_zones("GB") # => ["Edinburgh"]
# After
ActiveSupport::TimeZone.country_zones("GB") # => ["Edinburgh", "London"]
Fixes #31668.
*Andrew White*
*
`String#truncate_bytes`
to truncate a string to a maximum bytesize without
breaking multibyte characters or grapheme clusters like 👩👩👦👦.
...
...
activesupport/lib/active_support/core_ext/module/delegation.rb
浏览文件 @
06f06750
...
...
@@ -114,6 +114,24 @@ class DelegationError < NoMethodError; end
# invoice.customer_name # => 'John Doe'
# invoice.customer_address # => 'Vimmersvej 13'
#
# If you want the delegated method to be a private method,
# use the <tt>:private</tt> option.
#
# class User < ActiveRecord::Base
# has_one :profile
# delegate :first_name, to: :profile
# delegate :date_of_birth, :religion, to: :profile, private: true
#
# def age
# Date.today.year - date_of_birth.year
# end
# end
#
# User.new.age # 2
# User.new.first_name # Tomas
# User.new.date_of_birth # NoMethodError: private method `date_of_birth' called for #<User:0x00000008221340>
# User.new.religion # NoMethodError: private method `religion' called for #<User:0x00000008221340>
#
# If the target is +nil+ and does not respond to the delegated method a
# +Module::DelegationError+ is raised. If you wish to instead return +nil+,
# use the <tt>:allow_nil</tt> option.
...
...
@@ -151,7 +169,7 @@ class DelegationError < NoMethodError; end
# Foo.new("Bar").name # raises NoMethodError: undefined method `name'
#
# The target method must be public, otherwise it will raise +NoMethodError+.
def
delegate
(
*
methods
,
to:
nil
,
prefix:
nil
,
allow_nil:
nil
)
def
delegate
(
*
methods
,
to:
nil
,
prefix:
nil
,
allow_nil:
nil
,
private:
nil
)
unless
to
raise
ArgumentError
,
"Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, to: :greeter)."
end
...
...
@@ -173,7 +191,7 @@ def delegate(*methods, to: nil, prefix: nil, allow_nil: nil)
to
=
to
.
to_s
to
=
"self.
#{
to
}
"
if
DELEGATION_RESERVED_METHOD_NAMES
.
include?
(
to
)
methods
.
map
do
|
method
|
method
_names
=
method
s
.
map
do
|
method
|
# Attribute writer methods only accept one argument. Makes sure []=
# methods still accept two arguments.
definition
=
/[^\]]=$/
.
match?
(
method
)
?
"arg"
:
"*args, &block"
...
...
@@ -213,6 +231,9 @@ def delegate(*methods, to: nil, prefix: nil, allow_nil: nil)
module_eval
(
method_def
,
file
,
line
)
end
private
(
*
method_names
)
if
private
method_names
end
# When building decorators, a common pattern may emerge:
...
...
activesupport/test/core_ext/module_test.rb
浏览文件 @
06f06750
...
...
@@ -440,4 +440,61 @@ def initialize(place)
assert_not_respond_to
place
,
:the_city
assert
place
.
respond_to?
(
:the_city
,
true
)
end
def
test_private_delegate_with_private_option
location
=
Class
.
new
do
def
initialize
(
place
)
@place
=
place
end
delegate
(
:street
,
:city
,
to: :@place
,
private:
true
)
end
place
=
location
.
new
(
Somewhere
.
new
(
"Such street"
,
"Sad city"
))
assert_not_respond_to
place
,
:street
assert_not_respond_to
place
,
:city
assert
place
.
respond_to?
(
:street
,
true
)
# Asking for private method
assert
place
.
respond_to?
(
:city
,
true
)
end
def
test_some_public_some_private_delegate_with_private_option
location
=
Class
.
new
do
def
initialize
(
place
)
@place
=
place
end
delegate
(
:street
,
to: :@place
)
delegate
(
:city
,
to: :@place
,
private:
true
)
end
place
=
location
.
new
(
Somewhere
.
new
(
"Such street"
,
"Sad city"
))
assert_respond_to
place
,
:street
assert_not_respond_to
place
,
:city
assert
place
.
respond_to?
(
:city
,
true
)
# Asking for private method
end
def
test_private_delegate_prefixed_with_private_option
location
=
Class
.
new
do
def
initialize
(
place
)
@place
=
place
end
end
assert_equal
%i(the_street the_city)
,
location
.
delegate
(
:street
,
:city
,
to: :@place
,
prefix: :the
,
private:
true
)
place
=
location
.
new
(
Somewhere
.
new
(
"Such street"
,
"Sad city"
))
assert_not_respond_to
place
,
:street
assert_not_respond_to
place
,
:city
assert_not_respond_to
place
,
:the_street
assert
place
.
respond_to?
(
:the_street
,
true
)
assert_not_respond_to
place
,
:the_city
assert
place
.
respond_to?
(
:the_city
,
true
)
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录