diff options
author | Peter Jaros <peter.a.jaros@gmail.com> | 2013-10-15 15:45:07 -0400 |
---|---|---|
committer | Peter Jaros <peter.a.jaros@gmail.com> | 2014-03-28 10:25:06 -0400 |
commit | 03f35a27dc88335550ca0ab1d21e46948c9f5093 (patch) | |
tree | c1ba24a651170c407b93f0d43696ba8622e551d6 /activesupport/lib/active_support | |
parent | dd3ea17191e316aeebddaa7b176f6cfeee7a6365 (diff) | |
download | rails-03f35a27dc88335550ca0ab1d21e46948c9f5093.tar.gz rails-03f35a27dc88335550ca0ab1d21e46948c9f5093.tar.bz2 rails-03f35a27dc88335550ca0ab1d21e46948c9f5093.zip |
HashWithIndifferentAccess better respects #to_hash
In particular, `.new`, `#update`, `#merge`, `#replace` all accept
objects which respond to `#to_hash`, even if those objects are not
Hashes directly.
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/hash_with_indifferent_access.rb | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 594a4ca938..a4ebdea598 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -55,9 +55,9 @@ module ActiveSupport end def initialize(constructor = {}) - if constructor.is_a?(Hash) + if constructor.respond_to?(:to_hash) super() - update(constructor) + update(constructor.to_hash) else super(constructor) end @@ -72,6 +72,7 @@ module ActiveSupport end def self.new_from_hash_copying_default(hash) + hash = hash.to_hash new(hash).tap do |new_hash| new_hash.default = hash.default end @@ -125,7 +126,7 @@ module ActiveSupport if other_hash.is_a? HashWithIndifferentAccess super(other_hash) else - other_hash.each_pair do |key, value| + other_hash.to_hash.each_pair do |key, value| if block_given? && key?(key) value = yield(convert_key(key), self[key], value) end |