From 099eb2b3fd9526e06c30b9878780f31091e011aa Mon Sep 17 00:00:00 2001 From: David Lee Date: Sun, 8 May 2011 02:21:06 -0700 Subject: indifferent access should recurse Hash subclasses This commit makes Hash subclasses convert to HWIA by default for nested objects of subclasses of Hash, but allows certain subclasses to prevent nested conversion by introducing Hash#nested_under_indifferent_access that subclasses can overwrite. ActiveSupport::OrderedHash is one such subclass that overwrites +nested_under_indifferent_access+, since implicitly converting it to HWIA would remove the ordering of keys and values in Ruby 1.8. This change is necessary because commit ce9456e broke nested indifferent access conversion for all subclasses of Hash. --- .../lib/active_support/core_ext/hash/indifferent_access.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'activesupport/lib/active_support/core_ext/hash/indifferent_access.rb') 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 aad4b61e16..c2a6476604 100644 --- a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb +++ b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb @@ -9,4 +9,16 @@ class Hash def with_indifferent_access ActiveSupport::HashWithIndifferentAccess.new_from_hash_copying_default(self) end + + # Called when object is nested under an object that receives + # #with_indifferent_access. This method with be called on the current object + # by the enclosing object and is aliased to #with_indifferent_access by + # default. Subclasses of Hash may overwrite this method to return +self+ if + # converting to an +ActiveSupport::HashWithIndifferentAccess+ would not be + # desirable. + # + # b = {:b => 1} + # {:a => b}.with_indifferent_access["a"] # calls b.nested_under_indifferent_access + # + alias nested_under_indifferent_access with_indifferent_access end -- cgit v1.2.3