diff options
Diffstat (limited to 'activesupport')
4 files changed, 47 insertions, 12 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index bec1edf6c1..9f5c98a1cd 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* HashWithIndifferentAccess#to_hash converts to a Hash with String keys and the same default value. [Jeremy Kemper] + * Fix remove_constant to correctly handle constant names of the form "::A::...". References #6720. [Nicholas Seckar] * Fixed Array#to_xml when it contains a series of hashes (each piece would get its own XML declaration) #6610 [thkarcher/cyu] 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 9a3b02e5d5..6c7e6d76ac 100644 --- a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb +++ b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb @@ -1,4 +1,4 @@ -# this class has dubious semantics and we only have it so that +# This class has dubious semantics and we only have it so that # people can write params[:key] instead of params['key'] class HashWithIndifferentAccess < Hash @@ -64,6 +64,11 @@ class HashWithIndifferentAccess < Hash def stringify_keys!; self end def symbolize_keys!; self end + # Convert to a Hash with String keys. + def to_hash + Hash.new(default).merge(self) + end + protected def convert_key(key) key.kind_of?(Symbol) ? key.to_s : key diff --git a/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb b/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb index 46c53871d8..3c4908ac9e 100644 --- a/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb +++ b/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb @@ -18,8 +18,8 @@ module ActiveSupport #:nodoc: replace(reverse_merge(other_hash)) end - alias_method :reverse_update, :reverse_merge + alias_method :reverse_update, :reverse_merge! end end end -end
\ No newline at end of file +end diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 3ede4893a5..8e20f15518 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -171,6 +171,18 @@ class HashExtTest < Test::Unit::TestCase assert_equal hash.delete('a'), nil end + def test_indifferent_to_hash + # Should convert to a Hash with String keys. + assert_equal @strings, @mixed.with_indifferent_access.to_hash + + # Should preserve the default value. + mixed_with_default = @mixed.dup + mixed_with_default.default = '1234' + roundtrip = mixed_with_default.with_indifferent_access.to_hash + assert_equal @strings, roundtrip + assert_equal '1234', roundtrip.default + end + def test_stringify_and_symbolize_keys_on_indifferent_preserves_hash h = HashWithIndifferentAccess.new h[:first] = 1 @@ -182,6 +194,14 @@ class HashExtTest < Test::Unit::TestCase assert_equal 1, h[:first] end + def test_indifferent_subhashes + h = {'user' => {'id' => 5}}.with_indifferent_access + ['user', :user].each {|user| [:id, 'id'].each {|id| assert_equal 5, h[user][id], "h[#{user.inspect}][#{id.inspect}] should be 5"}} + + h = {:user => {:id => 5}}.with_indifferent_access + ['user', :user].each {|user| [:id, 'id'].each {|id| assert_equal 5, h[user][id], "h[#{user.inspect}][#{id.inspect}] should be 5"}} + end + def test_assert_valid_keys assert_nothing_raised do { :failure => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ]) @@ -194,14 +214,6 @@ class HashExtTest < Test::Unit::TestCase end end - def test_indifferent_subhashes - h = {'user' => {'id' => 5}}.with_indifferent_access - ['user', :user].each {|user| [:id, 'id'].each {|id| assert_equal 5, h[user][id], "h[#{user.inspect}][#{id.inspect}] should be 5"}} - - h = {:user => {:id => 5}}.with_indifferent_access - ['user', :user].each {|user| [:id, 'id'].each {|id| assert_equal 5, h[user][id], "h[#{user.inspect}][#{id.inspect}] should be 5"}} - end - def test_assorted_keys_not_stringified original = {Object.new => 2, 1 => 2, [] => true} indiff = original.with_indifferent_access @@ -209,7 +221,23 @@ class HashExtTest < Test::Unit::TestCase end def test_reverse_merge - assert_equal({ :a => 1, :b => 2, :c => 10 }, { :a => 1, :b => 2 }.reverse_merge({:a => "x", :b => "y", :c => 10}) ) + defaults = { :a => "x", :b => "y", :c => 10 }.freeze + options = { :a => 1, :b => 2 } + expected = { :a => 1, :b => 2, :c => 10 } + + # Should merge defaults into options, creating a new hash. + assert_equal expected, options.reverse_merge(defaults) + assert_not_equal expected, options + + # Should merge! defaults into options, replacing options. + merged = options.dup + assert_equal expected, merged.reverse_merge!(defaults) + assert_equal expected, merged + + # Should be an alias for reverse_merge! + merged = options.dup + assert_equal expected, merged.reverse_update(defaults) + assert_equal expected, merged end def test_diff |