Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

HashWithIndifferentAccess#to_hash converts to a Hash with String keys…

… and the same default value. Fix Hash#reverse_update to be an alias for reverse_merge./script/console More thoroughly test the reverse_* methods.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5725 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 05a5209f823f18e423e320f6bce4a91532c8bd03 1 parent 0fc8056
@jeremy jeremy authored
View
2  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]
View
7 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 @@ def delete(key)
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
View
4 activesupport/lib/active_support/core_ext/hash/reverse_merge.rb
@@ -18,8 +18,8 @@ def reverse_merge!(other_hash)
replace(reverse_merge(other_hash))
end
- alias_method :reverse_update, :reverse_merge
+ alias_method :reverse_update, :reverse_merge!
end
end
end
-end
+end
View
46 activesupport/test/core_ext/hash_ext_test.rb
@@ -171,6 +171,18 @@ def test_indifferent_deleting
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 @@ def test_stringify_and_symbolize_keys_on_indifferent_preserves_hash
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 @@ def test_assert_valid_keys
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 @@ def test_assorted_keys_not_stringified
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
Please sign in to comment.
Something went wrong with that request. Please try again.