Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
74d7df17
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,发现更多精彩内容 >>
提交
74d7df17
编写于
3月 28, 2014
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #12550 from Peeja/hash-wia-update-respects-to-hash
HashWithIndifferentAccess#update respects #to_hash
上级
555ec365
03f35a27
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
51 addition
and
3 deletion
+51
-3
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+7
-0
activesupport/lib/active_support/hash_with_indifferent_access.rb
...upport/lib/active_support/hash_with_indifferent_access.rb
+4
-3
activesupport/test/core_ext/hash_ext_test.rb
activesupport/test/core_ext/hash_ext_test.rb
+40
-0
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
74d7df17
*
`HashWithIndifferentAccess`
better respects
`#to_hash`
on objects it's
given. In particular,
`.new`
,
`#update`
,
`#merge`
,
`#replace`
all accept
objects which respond to
`#to_hash`
, even if those objects are not Hashes
directly.
*Peter Jaros*
*
Deprecate
`Class#superclass_delegating_accessor`
, use
`Class#class_attribute`
instead.
*Akshay Vishnoi*
...
...
activesupport/lib/active_support/hash_with_indifferent_access.rb
浏览文件 @
74d7df17
...
...
@@ -55,9 +55,9 @@ def nested_under_indifferent_access
end
def
initialize
(
constructor
=
{})
if
constructor
.
is_a?
(
H
ash
)
if
constructor
.
respond_to?
(
:to_h
ash
)
super
()
update
(
constructor
)
update
(
constructor
.
to_hash
)
else
super
(
constructor
)
end
...
...
@@ -72,6 +72,7 @@ def default(key = nil)
end
def
self
.
new_from_hash_copying_default
(
hash
)
hash
=
hash
.
to_hash
new
(
hash
).
tap
do
|
new_hash
|
new_hash
.
default
=
hash
.
default
end
...
...
@@ -125,7 +126,7 @@ def update(other_hash)
if
other_hash
.
is_a?
HashWithIndifferentAccess
super
(
other_hash
)
else
other_hash
.
each_pair
do
|
key
,
value
|
other_hash
.
to_hash
.
each_pair
do
|
key
,
value
|
if
block_given?
&&
key?
(
key
)
value
=
yield
(
convert_key
(
key
),
self
[
key
],
value
)
end
...
...
activesupport/test/core_ext/hash_ext_test.rb
浏览文件 @
74d7df17
...
...
@@ -23,6 +23,16 @@ def nested_under_indifferent_access
end
end
class
HashByConversion
def
initialize
(
hash
)
@hash
=
hash
end
def
to_hash
@hash
end
end
def
setup
@strings
=
{
'a'
=>
1
,
'b'
=>
2
}
@nested_strings
=
{
'a'
=>
{
'b'
=>
{
'c'
=>
3
}
}
}
...
...
@@ -411,6 +421,12 @@ def test_indifferent_update
assert
[
updated_with_strings
,
updated_with_symbols
,
updated_with_mixed
].
all?
{
|
h
|
h
.
keys
.
size
==
2
}
end
def
test_update_with_to_hash_conversion
hash
=
HashWithIndifferentAccess
.
new
hash
.
update
HashByConversion
.
new
({
:a
=>
1
})
assert_equal
hash
[
'a'
],
1
end
def
test_indifferent_merging
hash
=
HashWithIndifferentAccess
.
new
hash
[
:a
]
=
'failure'
...
...
@@ -430,6 +446,12 @@ def test_indifferent_merging
assert_equal
2
,
hash
[
'b'
]
end
def
test_merge_with_to_hash_conversion
hash
=
HashWithIndifferentAccess
.
new
merged
=
hash
.
merge
HashByConversion
.
new
({
:a
=>
1
})
assert_equal
merged
[
'a'
],
1
end
def
test_indifferent_replace
hash
=
HashWithIndifferentAccess
.
new
hash
[
:a
]
=
42
...
...
@@ -442,6 +464,18 @@ def test_indifferent_replace
assert_same
hash
,
replaced
end
def
test_replace_with_to_hash_conversion
hash
=
HashWithIndifferentAccess
.
new
hash
[
:a
]
=
42
replaced
=
hash
.
replace
(
HashByConversion
.
new
(
b:
12
))
assert
hash
.
key?
(
'b'
)
assert
!
hash
.
key?
(
:a
)
assert_equal
12
,
hash
[
:b
]
assert_same
hash
,
replaced
end
def
test_indifferent_merging_with_block
hash
=
HashWithIndifferentAccess
.
new
hash
[
:a
]
=
1
...
...
@@ -893,6 +927,12 @@ def test_compact!
assert_equal
({},
h
.
compact!
)
assert_equal
({},
h
)
end
def
test_new_with_to_hash_conversion
hash
=
HashWithIndifferentAccess
.
new
(
HashByConversion
.
new
(
a:
1
))
assert
hash
.
key?
(
'a'
)
assert_equal
1
,
hash
[
:a
]
end
end
class
IWriteMyOwnXML
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录