diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2006-01-23 05:38:02 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-01-23 05:38:02 +0000 |
commit | 9c24899ab0fad8baa4db543dd45f3d20ae95d3b3 (patch) | |
tree | 16becf9099b0ecfcd238986f8d0fbb3b6d0fa7ea | |
parent | 010e999092c04bd5ef9185db284a8733bc1b6ee6 (diff) | |
download | rails-9c24899ab0fad8baa4db543dd45f3d20ae95d3b3.tar.gz rails-9c24899ab0fad8baa4db543dd45f3d20ae95d3b3.tar.bz2 rails-9c24899ab0fad8baa4db543dd45f3d20ae95d3b3.zip |
Fix merge and dup for hashes with indifferent access (closes #3404) [kenneth.miller@bitfield.net]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3468 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/indifferent_access.rb | 8 | ||||
-rw-r--r-- | activesupport/test/core_ext/hash_ext_test.rb | 19 |
3 files changed, 29 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 2f22573f28..1d517d8862 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fix merge and dup for hashes with indifferent access #3404 [kenneth.miller@bitfield.net] + * Fix the requires in option_merger_test to unbreak AS tests. [Sam Stephenson] * Make HashWithIndifferentAccess#update behave like Hash#update by returning the hash. #3419, #3425 [asnem@student.ethz.ch, JanPrill@blauton.de, Marcel Molina Jr.] diff --git a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb index 458f8961a3..c96e63b169 100644 --- a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb +++ b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb @@ -41,6 +41,14 @@ class HashWithIndifferentAccess < Hash indices.collect {|key| self[convert_key(key)]} end + def dup + HashWithIndifferentAccess.new(self) + end + + def merge(hash) + self.dup.update(hash) + end + protected def convert_key(key) key.kind_of?(Symbol) ? key.to_s : key diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index a62dc3fbca..d74cea0991 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -116,6 +116,25 @@ class HashExtTest < Test::Unit::TestCase assert [updated_with_strings, updated_with_symbols, updated_with_mixed].all? {|hash| hash.keys.size == 2} end + def test_indifferent_merging + hash = HashWithIndifferentAccess.new + hash[:a] = 'failure' + hash['b'] = 'failure' + + other = { 'a' => 1, :b => 2 } + + merged = hash.merge(other) + + assert_equal HashWithIndifferentAccess, merged.class + assert_equal 1, merged[:a] + assert_equal 2, merged['b'] + + hash.update(other) + + assert_equal 1, hash[:a] + assert_equal 2, hash['b'] + end + def test_assert_valid_keys assert_nothing_raised do { :failure => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ]) |