diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-03-28 10:37:41 -0500 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-03-28 10:37:41 -0500 |
commit | 74d7df175c83e73347114c004ff861aec0572928 (patch) | |
tree | 66d38072b6524a2acc264380756c2ffd5d6796f3 /activesupport | |
parent | 555ec36522011862c03b483c53be32410594a51e (diff) | |
parent | 03f35a27dc88335550ca0ab1d21e46948c9f5093 (diff) | |
download | rails-74d7df175c83e73347114c004ff861aec0572928.tar.gz rails-74d7df175c83e73347114c004ff861aec0572928.tar.bz2 rails-74d7df175c83e73347114c004ff861aec0572928.zip |
Merge pull request #12550 from Peeja/hash-wia-update-respects-to-hash
HashWithIndifferentAccess#update respects #to_hash
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 7 | ||||
-rw-r--r-- | activesupport/lib/active_support/hash_with_indifferent_access.rb | 7 | ||||
-rw-r--r-- | activesupport/test/core_ext/hash_ext_test.rb | 40 |
3 files changed, 51 insertions, 3 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 7feb820d4f..556e94d184 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,10 @@ +* `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* diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 594a4ca938..a4ebdea598 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -55,9 +55,9 @@ module ActiveSupport end def initialize(constructor = {}) - if constructor.is_a?(Hash) + if constructor.respond_to?(:to_hash) super() - update(constructor) + update(constructor.to_hash) else super(constructor) end @@ -72,6 +72,7 @@ module ActiveSupport 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 @@ module ActiveSupport 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 diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 40c8f03374..69a380c7cb 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -23,6 +23,16 @@ class HashExtTest < ActiveSupport::TestCase 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 @@ class HashExtTest < ActiveSupport::TestCase 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 @@ class HashExtTest < ActiveSupport::TestCase 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 @@ class HashExtTest < ActiveSupport::TestCase 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 @@ class HashExtTest < ActiveSupport::TestCase 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 |