diff options
author | David Lee <davidomundo@gmail.com> | 2011-05-08 02:21:06 -0700 |
---|---|---|
committer | David Lee <davidomundo@gmail.com> | 2011-05-08 03:40:51 -0700 |
commit | 099eb2b3fd9526e06c30b9878780f31091e011aa (patch) | |
tree | fc6e89f7a0459ff376af0d8535f4aa270bab5a46 /activesupport/test/core_ext/hash_ext_test.rb | |
parent | 30db3a82f653e7d7215e41bec525932cf5b17de1 (diff) | |
download | rails-099eb2b3fd9526e06c30b9878780f31091e011aa.tar.gz rails-099eb2b3fd9526e06c30b9878780f31091e011aa.tar.bz2 rails-099eb2b3fd9526e06c30b9878780f31091e011aa.zip |
indifferent access should recurse Hash subclasses
This commit makes Hash subclasses convert to HWIA by default for nested
objects of subclasses of Hash, but allows certain subclasses to prevent nested
conversion by introducing Hash#nested_under_indifferent_access that subclasses
can overwrite.
ActiveSupport::OrderedHash is one such subclass that overwrites
+nested_under_indifferent_access+, since implicitly converting it to HWIA would
remove the ordering of keys and values in Ruby 1.8.
This change is necessary because commit ce9456e broke nested indifferent access
conversion for all subclasses of Hash.
Diffstat (limited to 'activesupport/test/core_ext/hash_ext_test.rb')
-rw-r--r-- | activesupport/test/core_ext/hash_ext_test.rb | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 3ef080e1cb..4557a10688 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -16,6 +16,12 @@ class HashExtTest < Test::Unit::TestCase class SubclassingHash < Hash end + class NonIndifferentHash < Hash + def nested_under_indifferent_access + self + end + end + def setup @strings = { 'a' => 1, 'b' => 2 } @symbols = { :a => 1, :b => 2 } @@ -109,9 +115,12 @@ class HashExtTest < Test::Unit::TestCase assert_equal @strings, @mixed.with_indifferent_access.dup.stringify_keys! end - def test_hash_subclass - flash = { "foo" => SubclassingHash.new.tap { |h| h["bar"] = "baz" } }.with_indifferent_access - assert_kind_of SubclassingHash, flash["foo"] + def test_nested_under_indifferent_access + foo = { "foo" => SubclassingHash.new.tap { |h| h["bar"] = "baz" } }.with_indifferent_access + assert_kind_of ActiveSupport::HashWithIndifferentAccess, foo["foo"] + + foo = { "foo" => NonIndifferentHash.new.tap { |h| h["bar"] = "baz" } }.with_indifferent_access + assert_kind_of NonIndifferentHash, foo["foo"] end def test_indifferent_assorted |