1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Only convert direct hash instances in hash with indifferent access.

This commit is contained in:
José Valim 2010-11-26 11:55:56 +01:00
parent 9332cc582e
commit ce9456eca0
2 changed files with 11 additions and 4 deletions

View file

@ -140,11 +140,10 @@ module ActiveSupport
end end
def convert_value(value) def convert_value(value)
case value if value.class == Hash
when Hash
self.class.new_from_hash_copying_default(value) self.class.new_from_hash_copying_default(value)
when Array elsif value.is_a?(Array)
value.dup.replace(value.collect { |e| e.is_a?(Hash) ? self.class.new_from_hash_copying_default(e) : e }) value.dup.replace(value.map { |e| convert_value(e) })
else else
value value
end end

View file

@ -12,6 +12,9 @@ class HashExtTest < Test::Unit::TestCase
class SubclassingArray < Array class SubclassingArray < Array
end end
class SubclassingHash < Hash
end
def setup def setup
@strings = { 'a' => 1, 'b' => 2 } @strings = { 'a' => 1, 'b' => 2 }
@symbols = { :a => 1, :b => 2 } @symbols = { :a => 1, :b => 2 }
@ -105,6 +108,11 @@ class HashExtTest < Test::Unit::TestCase
assert_equal @strings, @mixed.with_indifferent_access.dup.stringify_keys! assert_equal @strings, @mixed.with_indifferent_access.dup.stringify_keys!
end end
def test_hash_subclass
flash = { "foo" => SubclassingHash.new.tap { |h| h["bar"] = "baz" } }.with_indifferent_access
assert_kind_of SubclassingHash, flash["foo"]
end
def test_indifferent_assorted def test_indifferent_assorted
@strings = @strings.with_indifferent_access @strings = @strings.with_indifferent_access
@symbols = @symbols.with_indifferent_access @symbols = @symbols.with_indifferent_access