fix HashWithIndifferentAccess#to_hash behaviour

This commit is contained in:
Vipul A M 2013-04-19 13:20:11 +05:30
parent 87fddba98f
commit df24b8790f
2 changed files with 11 additions and 3 deletions

@ -229,7 +229,11 @@ def to_options!; self end
# Convert to a regular hash with string keys.
def to_hash
Hash.new(default).merge!(self)
_new_hash= {}
each do |key, value|
_new_hash[convert_key(key)] = convert_value(value,true)
end
Hash.new(default).merge!(_new_hash)
end
protected
@ -237,9 +241,9 @@ def convert_key(key)
key.kind_of?(Symbol) ? key.to_s : key
end
def convert_value(value)
def convert_value(value, _convert_for_to_hash = false)
if value.is_a? Hash
value.nested_under_indifferent_access
_convert_for_to_hash ? value.to_hash : value.nested_under_indifferent_access
elsif value.is_a?(Array)
value = value.dup if value.frozen?
value.map! { |e| convert_value(e) }

@ -490,6 +490,10 @@ def test_indifferent_to_hash
roundtrip = mixed_with_default.with_indifferent_access.to_hash
assert_equal @strings, roundtrip
assert_equal '1234', roundtrip.default
new_to_hash = @nested_mixed.with_indifferent_access.to_hash
assert_not new_to_hash.instance_of?(HashWithIndifferentAccess)
assert_not new_to_hash["a"].instance_of?(HashWithIndifferentAccess)
assert_not new_to_hash["a"]["b"].instance_of?(HashWithIndifferentAccess)
end
def test_lookup_returns_the_same_object_that_is_stored_in_hash_indifferent_access