mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add Hash#deep_transform_keys and Hash#deep_transform_keys! Also convert deep_*_keys to use deep_transform_keys.
This commit is contained in:
parent
541429fbe4
commit
2b5b60fc3c
4 changed files with 53 additions and 22 deletions
|
@ -1,6 +1,6 @@
|
|||
## Rails 4.0.0 (unreleased) ##
|
||||
|
||||
* Add `Hash#transform_keys` and `Hash#transform_keys!`. *Mark McSpadden*
|
||||
* Add `Hash#transform_keys`, `Hash#transform_keys!`, `Hash#deep_transform_keys`, and `Hash#deep_transform_keys!`. *Mark McSpadden*
|
||||
|
||||
* Changed xml type `datetime` to `dateTime` (with upper case letter `T`). *Angelo Capilleri*
|
||||
|
||||
|
|
|
@ -65,47 +65,56 @@ class Hash
|
|||
end
|
||||
end
|
||||
|
||||
# Return a new hash with all keys converted by the block operation.
|
||||
# This includes the keys from the root hash and from all
|
||||
# nested hashes.
|
||||
#
|
||||
# { :person => { :name => 'Rob', :years => '28' } }.deep_transform_keys{ |key| key.to_s.upcase }
|
||||
# # => { "PERSON" => { "NAME" => "Rob", "YEARS" => "28" } }
|
||||
def deep_transform_keys(&block)
|
||||
result = {}
|
||||
each do |key, value|
|
||||
result[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys(&block) : value
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
# Destructively convert all keys by using the block operation.
|
||||
# This includes the keys from the root hash and from all
|
||||
# nested hashes.
|
||||
def deep_transform_keys!(&block)
|
||||
keys.each do |key|
|
||||
value = delete(key)
|
||||
self[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys!(&block) : value
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
# Return a new hash with all keys converted to strings.
|
||||
# This includes the keys from the root hash and from all
|
||||
# nested hashes.
|
||||
def deep_stringify_keys
|
||||
result = {}
|
||||
each do |key, value|
|
||||
result[key.to_s] = value.is_a?(Hash) ? value.deep_stringify_keys : value
|
||||
end
|
||||
result
|
||||
deep_transform_keys{ |key| key.to_s }
|
||||
end
|
||||
|
||||
# Destructively convert all keys to strings.
|
||||
# This includes the keys from the root hash and from all
|
||||
# nested hashes.
|
||||
def deep_stringify_keys!
|
||||
keys.each do |key|
|
||||
val = delete(key)
|
||||
self[key.to_s] = val.is_a?(Hash) ? val.deep_stringify_keys! : val
|
||||
end
|
||||
self
|
||||
deep_transform_keys!{ |key| key.to_s }
|
||||
end
|
||||
|
||||
# Destructively convert all keys to symbols, as long as they respond
|
||||
# to +to_sym+. This includes the keys from the root hash and from all
|
||||
# nested hashes.
|
||||
def deep_symbolize_keys!
|
||||
keys.each do |key|
|
||||
val = delete(key)
|
||||
self[(key.to_sym rescue key)] = val.is_a?(Hash) ? val.deep_stringify_keys! : val
|
||||
end
|
||||
self
|
||||
deep_transform_keys!{ |key| key.to_sym rescue key }
|
||||
end
|
||||
|
||||
# Return a new hash with all keys converted to symbols, as long as
|
||||
# they respond to +to_sym+. This includes the keys from the root hash
|
||||
# and from all nested hashes.
|
||||
def deep_symbolize_keys
|
||||
result = {}
|
||||
each do |key, value|
|
||||
result[(key.to_sym rescue key)] = value.is_a?(Hash) ? value.deep_symbolize_keys : value
|
||||
end
|
||||
result
|
||||
deep_transform_keys{ |key| key.to_sym rescue key }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -32,14 +32,17 @@ class HashExtTest < ActiveSupport::TestCase
|
|||
@fixnums = { 0 => 1, 1 => 2 }
|
||||
@nested_fixnums = { 0 => { 1 => { 2 => 3} } }
|
||||
@illegal_symbols = { [] => 3 }
|
||||
@upcase_strings = { 'A' => 1, 'B' => 2 }
|
||||
@nested_illegal_symbols = { [] => { [] => 3} }
|
||||
@upcase_strings = { 'A' => 1, 'B' => 2 }
|
||||
@nested_upcase_strings = { 'A' => { 'B' => { 'C' => 3 } } }
|
||||
end
|
||||
|
||||
def test_methods
|
||||
h = {}
|
||||
assert_respond_to h, :transform_keys
|
||||
assert_respond_to h, :transform_keys!
|
||||
assert_respond_to h, :deep_transform_keys
|
||||
assert_respond_to h, :deep_transform_keys!
|
||||
assert_respond_to h, :symbolize_keys
|
||||
assert_respond_to h, :symbolize_keys!
|
||||
assert_respond_to h, :deep_symbolize_keys
|
||||
|
@ -58,12 +61,24 @@ class HashExtTest < ActiveSupport::TestCase
|
|||
assert_equal @upcase_strings, @mixed.transform_keys{ |key| key.to_s.upcase }
|
||||
end
|
||||
|
||||
def test_deep_transform_keys
|
||||
assert_equal @nested_upcase_strings, @nested_symbols.deep_transform_keys{ |key| key.to_s.upcase }
|
||||
assert_equal @nested_upcase_strings, @nested_strings.deep_transform_keys{ |key| key.to_s.upcase }
|
||||
assert_equal @nested_upcase_strings, @nested_mixed.deep_transform_keys{ |key| key.to_s.upcase }
|
||||
end
|
||||
|
||||
def test_transform_keys!
|
||||
assert_equal @upcase_strings, @symbols.dup.transform_keys!{ |key| key.to_s.upcase }
|
||||
assert_equal @upcase_strings, @strings.dup.transform_keys!{ |key| key.to_s.upcase }
|
||||
assert_equal @upcase_strings, @mixed.dup.transform_keys!{ |key| key.to_s.upcase }
|
||||
end
|
||||
|
||||
def test_deep_transform_keys!
|
||||
assert_equal @nested_upcase_strings, @nested_symbols.deep_transform_keys!{ |key| key.to_s.upcase }
|
||||
assert_equal @nested_upcase_strings, @nested_strings.deep_transform_keys!{ |key| key.to_s.upcase }
|
||||
assert_equal @nested_upcase_strings, @nested_mixed.deep_transform_keys!{ |key| key.to_s.upcase }
|
||||
end
|
||||
|
||||
def test_symbolize_keys
|
||||
assert_equal @symbols, @symbols.symbolize_keys
|
||||
assert_equal @symbols, @strings.symbolize_keys
|
||||
|
|
|
@ -2579,6 +2579,13 @@ end
|
|||
|
||||
There's also the bang variant +transform_keys!+ that applies the block operations to keys in the very receiver.
|
||||
|
||||
Besides that, one can use +deep_transform_keys+ and +deep_transform_keys!+ to perform the block operation on all the keys in the given hash and all the hashes nested into it. An example of the result is:
|
||||
|
||||
<ruby>
|
||||
{nil => nil, 1 => 1, :nested => {:a => 3, 5 => 5}}.deep_transform_keys{ |key| key.to_s.upcase }
|
||||
# => {""=>nil, "1"=>1, "NESTED"=>{"A"=>3, "5"=>5}}
|
||||
</ruby>
|
||||
|
||||
NOTE: Defined in +active_support/core_ext/hash/keys.rb+.
|
||||
|
||||
h5. +stringify_keys+ and +stringify_keys!+
|
||||
|
|
Loading…
Reference in a new issue