Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
b8dc06b8
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,体验更适合开发者的 AI 搜索 >>
提交
b8dc06b8
编写于
2月 08, 2019
作者:
G
Guillermo Iguaran
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add 'Hash#deep_transform_values', and 'Hash#deep_transform_values!'
上级
5f9e0504
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
84 addition
and
0 deletion
+84
-0
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+4
-0
activesupport/lib/active_support/core_ext/hash.rb
activesupport/lib/active_support/core_ext/hash.rb
+1
-0
activesupport/lib/active_support/core_ext/hash/deep_transform_values.rb
...lib/active_support/core_ext/hash/deep_transform_values.rb
+52
-0
activesupport/test/core_ext/hash/deep_transform_values_test.rb
...esupport/test/core_ext/hash/deep_transform_values_test.rb
+0
-0
activesupport/test/core_ext/hash_ext_test.rb
activesupport/test/core_ext/hash_ext_test.rb
+27
-0
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
b8dc06b8
...
@@ -12,6 +12,10 @@
...
@@ -12,6 +12,10 @@
*Stefan Schüßler*
*Stefan Schüßler*
*
Add
`Hash#deep_transform_values`
, and
`Hash#deep_transform_values!`
.
*Guillermo Iguaran*
## Rails 6.0.0.beta1 (January 18, 2019) ##
## Rails 6.0.0.beta1 (January 18, 2019) ##
*
Remove deprecated
`Module#reachable?`
method.
*
Remove deprecated
`Module#reachable?`
method.
...
...
activesupport/lib/active_support/core_ext/hash.rb
浏览文件 @
b8dc06b8
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
require
"active_support/core_ext/hash/conversions"
require
"active_support/core_ext/hash/conversions"
require
"active_support/core_ext/hash/deep_merge"
require
"active_support/core_ext/hash/deep_merge"
require
"active_support/core_ext/hash/deep_transform_values"
require
"active_support/core_ext/hash/except"
require
"active_support/core_ext/hash/except"
require
"active_support/core_ext/hash/indifferent_access"
require
"active_support/core_ext/hash/indifferent_access"
require
"active_support/core_ext/hash/keys"
require
"active_support/core_ext/hash/keys"
...
...
activesupport/lib/active_support/core_ext/hash/deep_transform_values.rb
0 → 100644
浏览文件 @
b8dc06b8
# frozen_string_literal: true
class
Hash
# Returns a new hash with all keys converted by the block operation.
# This includes the keys from the root hash and from all
# nested hashes and arrays.
#
# hash = { person: { name: 'Rob', age: '28' } }
#
# hash.deep_transform_values{ |value| value.to_s.upcase }
# # => {person: {name: "ROB", age: "28"}}
def
deep_transform_values
(
&
block
)
_deep_transform_values_in_object
(
self
,
&
block
)
end
# Destructively converts all values by using the block operation.
# This includes the values from the root hash and from all
# nested hashes and arrays.
def
deep_transform_values!
(
&
block
)
_deep_transform_values_in_object!
(
self
,
&
block
)
end
private
# support methods for deep transforming nested hashes and arrays
def
_deep_transform_values_in_object
(
object
,
&
block
)
case
object
when
Hash
object
.
each_with_object
({})
do
|
(
key
,
value
),
result
|
result
[
key
]
=
_deep_transform_values_in_object
(
value
,
&
block
)
end
when
Array
object
.
map
{
|
e
|
_deep_transform_values_in_object
(
e
,
&
block
)
}
else
yield
(
object
)
end
end
def
_deep_transform_values_in_object!
(
object
,
&
block
)
case
object
when
Hash
object
.
keys
.
each
do
|
key
|
value
=
object
.
delete
(
key
)
object
[
key
]
=
_deep_transform_values_in_object!
(
value
,
&
block
)
end
object
when
Array
object
.
map!
{
|
e
|
_deep_transform_values_in_object!
(
e
,
&
block
)
}
else
yield
(
object
)
end
end
end
activesupport/test/core_ext/hash/deep_transform_values_test.rb
0 → 100644
浏览文件 @
b8dc06b8
activesupport/test/core_ext/hash_ext_test.rb
浏览文件 @
b8dc06b8
...
@@ -33,6 +33,8 @@ def test_methods
...
@@ -33,6 +33,8 @@ def test_methods
h
=
{}
h
=
{}
assert_respond_to
h
,
:deep_transform_keys
assert_respond_to
h
,
:deep_transform_keys
assert_respond_to
h
,
:deep_transform_keys!
assert_respond_to
h
,
:deep_transform_keys!
assert_respond_to
h
,
:deep_transform_values
assert_respond_to
h
,
:deep_transform_values!
assert_respond_to
h
,
:symbolize_keys
assert_respond_to
h
,
:symbolize_keys
assert_respond_to
h
,
:symbolize_keys!
assert_respond_to
h
,
:symbolize_keys!
assert_respond_to
h
,
:deep_symbolize_keys
assert_respond_to
h
,
:deep_symbolize_keys
...
@@ -78,6 +80,31 @@ def test_deep_transform_keys_with_bang_mutates
...
@@ -78,6 +80,31 @@ def test_deep_transform_keys_with_bang_mutates
assert_equal
({
"a"
=>
{
b:
{
"c"
=>
3
}
}
},
@nested_mixed
)
assert_equal
({
"a"
=>
{
b:
{
"c"
=>
3
}
}
},
@nested_mixed
)
end
end
def
test_deep_transform_values
assert_equal
({
"a"
=>
"1"
,
"b"
=>
"2"
},
@strings
.
deep_transform_values
{
|
value
|
value
.
to_s
})
assert_equal
({
"a"
=>
{
"b"
=>
{
"c"
=>
"3"
}
}
},
@nested_strings
.
deep_transform_values
{
|
value
|
value
.
to_s
})
assert_equal
({
"a"
=>
[
{
"b"
=>
"2"
},
{
"c"
=>
"3"
},
"4"
]
},
@string_array_of_hashes
.
deep_transform_values
{
|
value
|
value
.
to_s
})
end
def
test_deep_transform_values_not_mutates
transformed_hash
=
@nested_mixed
.
deep_dup
transformed_hash
.
deep_transform_values
{
|
value
|
value
.
to_s
}
assert_equal
@nested_mixed
,
transformed_hash
end
def
test_deep_transform_values!
assert_equal
({
"a"
=>
"1"
,
"b"
=>
"2"
},
@strings
.
deep_transform_values!
{
|
value
|
value
.
to_s
})
assert_equal
({
"a"
=>
{
"b"
=>
{
"c"
=>
"3"
}
}
},
@nested_strings
.
deep_transform_values!
{
|
value
|
value
.
to_s
})
assert_equal
({
"a"
=>
[
{
"b"
=>
"2"
},
{
"c"
=>
"3"
},
"4"
]
},
@string_array_of_hashes
.
deep_transform_values!
{
|
value
|
value
.
to_s
})
end
def
test_deep_transform_values_with_bang_mutates
transformed_hash
=
@nested_mixed
.
deep_dup
transformed_hash
.
deep_transform_values!
{
|
value
|
value
.
to_s
}
assert_equal
({
"a"
=>
{
b:
{
"c"
=>
"3"
}
}
},
transformed_hash
)
assert_equal
({
"a"
=>
{
b:
{
"c"
=>
3
}
}
},
@nested_mixed
)
end
def
test_symbolize_keys
def
test_symbolize_keys
assert_equal
@symbols
,
@symbols
.
symbolize_keys
assert_equal
@symbols
,
@symbols
.
symbolize_keys
assert_equal
@symbols
,
@strings
.
symbolize_keys
assert_equal
@symbols
,
@strings
.
symbolize_keys
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录