From 6ff887321b930197a8d62ddd0fc1dcb965ccda29 Mon Sep 17 00:00:00 2001 From: Mark McSpadden Date: Thu, 10 May 2012 16:05:19 -0500 Subject: Add Hash#transform_keys and Hash#transform_keys! and refactor *_keys methods to use them. --- .../lib/active_support/core_ext/hash/keys.rb | 43 +++++++++++++--------- 1 file changed, 25 insertions(+), 18 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/hash/keys.rb') diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb index be4d611ce7..8b24ef3350 100644 --- a/activesupport/lib/active_support/core_ext/hash/keys.rb +++ b/activesupport/lib/active_support/core_ext/hash/keys.rb @@ -1,46 +1,53 @@ class Hash - # Return a new hash with all keys converted to strings. + # Return a new hash with all keys converted using the block operation. # - # { :name => 'Rob', :years => '28' }.stringify_keys - # #=> { "name" => "Rob", "years" => "28" } - def stringify_keys + # { :name => 'Rob', :years => '28' }.transform_keys{ |key| key.to_s.upcase } + # # => { "NAME" => "Rob", "YEARS" => "28" } + def transform_keys result = {} keys.each do |key| - result[key.to_s] = self[key] + result[yield(key)] = self[key] end result end - # Destructively convert all keys to strings. Same as - # +stringify_keys+, but modifies +self+. - def stringify_keys! + # Destructively convert all keys using the block operations. + # Same as transform_keys but modifies +self+ + def transform_keys! keys.each do |key| - self[key.to_s] = delete(key) + self[yield(key)] = delete(key) end self end + # Return a new hash with all keys converted to strings. + # + # { :name => 'Rob', :years => '28' }.stringify_keys + # #=> { "name" => "Rob", "years" => "28" } + def stringify_keys + transform_keys{ |key| key.to_s } + end + + # Destructively convert all keys to strings. Same as + # +stringify_keys+, but modifies +self+. + def stringify_keys! + transform_keys!{ |key| key.to_s } + end + # Return a new hash with all keys converted to symbols, as long as # they respond to +to_sym+. # # { 'name' => 'Rob', 'years' => '28' }.symbolize_keys # #=> { :name => "Rob", :years => "28" } def symbolize_keys - result = {} - keys.each do |key| - result[(key.to_sym rescue key)] = self[key] - end - result + transform_keys{ |key| key.to_sym rescue key } end alias_method :to_options, :symbolize_keys # Destructively convert all keys to symbols, as long as they respond # to +to_sym+. Same as +symbolize_keys+, but modifies +self+. def symbolize_keys! - keys.each do |key| - self[(key.to_sym rescue key)] = delete(key) - end - self + transform_keys!{ |key| key.to_sym rescue key } end alias_method :to_options!, :symbolize_keys! -- cgit v1.2.3 From a4bb195c91fa9bb25e136aa8850f3acafea0a597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20H=C3=BAngaro?= Date: Sun, 29 Apr 2012 15:43:06 -0300 Subject: Adding deep versions of stringify_keys and symbolize_keys (plain and bang) for nested hashes --- .../lib/active_support/core_ext/hash/keys.rb | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'activesupport/lib/active_support/core_ext/hash/keys.rb') diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb index 8b24ef3350..5eb861934d 100644 --- a/activesupport/lib/active_support/core_ext/hash/keys.rb +++ b/activesupport/lib/active_support/core_ext/hash/keys.rb @@ -64,4 +64,48 @@ class Hash raise ArgumentError.new("Unknown key: #{k}") unless valid_keys.include?(k) end 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 + 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 + 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 + 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 + end end -- cgit v1.2.3 From 2b5b60fc3c4251536b837937391ebdd028892b15 Mon Sep 17 00:00:00 2001 From: Mark McSpadden Date: Wed, 23 May 2012 12:31:35 -0500 Subject: Add Hash#deep_transform_keys and Hash#deep_transform_keys! Also convert deep_*_keys to use deep_transform_keys. --- .../lib/active_support/core_ext/hash/keys.rb | 43 +++++++++++++--------- 1 file changed, 26 insertions(+), 17 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/hash/keys.rb') diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb index 5eb861934d..362d584ba1 100644 --- a/activesupport/lib/active_support/core_ext/hash/keys.rb +++ b/activesupport/lib/active_support/core_ext/hash/keys.rb @@ -65,47 +65,56 @@ class Hash end end - # Return a new hash with all keys converted to strings. + # 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. - def deep_stringify_keys + # + # { :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[key.to_s] = value.is_a?(Hash) ? value.deep_stringify_keys : value + result[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys(&block) : value end result end - # Destructively convert all keys to strings. + # Destructively convert all keys by using the block operation. # This includes the keys from the root hash and from all # nested hashes. - def deep_stringify_keys! + def deep_transform_keys!(&block) keys.each do |key| - val = delete(key) - self[key.to_s] = val.is_a?(Hash) ? val.deep_stringify_keys! : val + 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 + 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! + 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 -- cgit v1.2.3 From 1d485d51f8cd6e122a66d754f95918d378a9bb33 Mon Sep 17 00:00:00 2001 From: Mark McSpadden Date: Fri, 25 May 2012 15:29:01 -0500 Subject: Reorder deep_symbolize_keys methods --- activesupport/lib/active_support/core_ext/hash/keys.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/hash/keys.rb') diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb index 362d584ba1..e5e77bcef4 100644 --- a/activesupport/lib/active_support/core_ext/hash/keys.rb +++ b/activesupport/lib/active_support/core_ext/hash/keys.rb @@ -104,17 +104,17 @@ class Hash 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! - 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 deep_transform_keys{ |key| key.to_sym rescue key } 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! + deep_transform_keys!{ |key| key.to_sym rescue key } + end end -- cgit v1.2.3 From ac50b633d4b9fd12bf0212a5cf8ebc5ab3ee8f37 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Sat, 26 May 2012 22:24:06 -0500 Subject: add examples to Hash#deep_stringify_keys and Hash#deep_symbolize_keys [ci skip] --- activesupport/lib/active_support/core_ext/hash/keys.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activesupport/lib/active_support/core_ext/hash/keys.rb') diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb index e5e77bcef4..988a0b5949 100644 --- a/activesupport/lib/active_support/core_ext/hash/keys.rb +++ b/activesupport/lib/active_support/core_ext/hash/keys.rb @@ -93,6 +93,11 @@ class Hash # Return a new hash with all keys converted to strings. # This includes the keys from the root hash and from all # nested hashes. + # + # hash = { person: { name: 'Rob', age: '28' } } + # + # hash.deep_stringify_keys + # # => { "person" => { "name" => "Rob", "age" => "28" } } def deep_stringify_keys deep_transform_keys{ |key| key.to_s } end @@ -107,6 +112,11 @@ class Hash # 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. + # + # hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } } + # + # hash.deep_symbolize_keys + # # => { person: { name: "Rob", age: "28" } } def deep_symbolize_keys deep_transform_keys{ |key| key.to_sym rescue key } end -- cgit v1.2.3 From 29713d7f3bca4267343a0bcaf8f1ee0ea3fe02ae Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Sat, 26 May 2012 22:28:49 -0500 Subject: update Hash documentation with 1.9 syntax [ci skip] --- .../lib/active_support/core_ext/hash/keys.rb | 24 ++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/hash/keys.rb') diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb index 988a0b5949..8e728691c6 100644 --- a/activesupport/lib/active_support/core_ext/hash/keys.rb +++ b/activesupport/lib/active_support/core_ext/hash/keys.rb @@ -1,8 +1,10 @@ class Hash # Return a new hash with all keys converted using the block operation. # - # { :name => 'Rob', :years => '28' }.transform_keys{ |key| key.to_s.upcase } - # # => { "NAME" => "Rob", "YEARS" => "28" } + # hash = { name: 'Rob', age: '28' } + # + # hash.transform_keys{ |key| key.to_s.upcase } + # # => { "NAME" => "Rob", "AGE" => "28" } def transform_keys result = {} keys.each do |key| @@ -22,8 +24,10 @@ class Hash # Return a new hash with all keys converted to strings. # - # { :name => 'Rob', :years => '28' }.stringify_keys - # #=> { "name" => "Rob", "years" => "28" } + # hash = { name: 'Rob', age: '28' } + # + # hash.stringify_keys + # #=> { "name" => "Rob", "age" => "28" } def stringify_keys transform_keys{ |key| key.to_s } end @@ -37,8 +41,10 @@ class Hash # Return a new hash with all keys converted to symbols, as long as # they respond to +to_sym+. # - # { 'name' => 'Rob', 'years' => '28' }.symbolize_keys - # #=> { :name => "Rob", :years => "28" } + # hash = { 'name' => 'Rob', 'age' => '28' } + # + # hash.symbolize_keys + # #=> { name: "Rob", age: "28" } def symbolize_keys transform_keys{ |key| key.to_sym rescue key } end @@ -69,8 +75,10 @@ class Hash # 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" } } + # hash = { person: { name: 'Rob', age: '28' } } + # + # hash.deep_transform_keys{ |key| key.to_s.upcase } + # # => { "PERSON" => { "NAME" => "Rob", "AGE" => "28" } } def deep_transform_keys(&block) result = {} each do |key, value| -- cgit v1.2.3