diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index d3f194c65e..9153603927 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed Hash#indifferent_access to also deal with include? and fetch and nested hashes #726 [Nicholas Seckar] + * Added Object#blank? -- see http://redhanded.hobix.com/inspect/objectBlank.html #783 [_why the lucky stiff] * Added inflection rules for "sh" words, like "wish" and "fish" #755 [phillip@pjbsoftware.com] 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 d8594db739..00cb0b0b51 100644 --- a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb +++ b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb @@ -1,3 +1,4 @@ +# This implementation is HODEL-HASH-9600 compliant class HashWithIndifferentAccess < Hash def initialize(constructor = {}) if constructor.is_a?(Hash) @@ -21,8 +22,35 @@ class HashWithIndifferentAccess < Hash alias_method :regular_writer, :[]= unless method_defined?(:regular_writer) def []=(key, value) - regular_writer(key.is_a?(Symbol) ? key.to_s : key, value) + regular_writer(convert_key(key), convert_value(value)) end + def update(hash) + hash.each {|key, value| self[key] = value} + end + + def key?(key) + super(convert_key(key)) + end + + alias_method :include?, :key? + alias_method :has_key?, :key? + alias_method :member?, :key? + + def fetch(key, *extras) + super(convert_key(key), *extras) + end + + def values_at(*indices) + indices.collect {|key| self[convert_key(key)]} + end + + protected + def convert_key(key) + key.kind_of?(Symbol) ? key.to_s : key + end + def convert_value(value) + value.is_a?(Hash) ? value.with_indifferent_access : value + end end module ActiveSupport #:nodoc: diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index d74a32e892..0a7a7a41b2 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -56,6 +56,26 @@ class HashExtTest < Test::Unit::TestCase assert_equal @symbols[:a], @symbols['a'] assert_equal @strings['b'], @mixed['b'] assert_equal @strings[:b], @mixed['b'] + + hashes = { :@strings => @strings, :@symbols => @symbols, :@mixed => @mixed } + + method_map = { :'[]' => 1, :fetch => 1, :index => 1, :values_at => 1, + :has_key? => true, :include? => true, :key => true, + :member? => true } + + hashes.each do |name, hash| + method_map.sort_by { |m| m.to_s }.each do |meth, expected| + assert_equal(expected, hash.send(meth, 'a'), "Calling #{name}.#{meth} 'a'") + assert_equal(expected, hash.send(meth, :a), "Calling #{name}.#{meth} :a") + end + end + + assert_equal [1, 2], @strings.values_at('a', 'b') + assert_equal [1, 2], @strings.values_at(:a, :b) + assert_equal [1, 2], @symbols.values_at('a', 'b') + assert_equal [1, 2], @symbols.values_at(:a, :b) + assert_equal [1, 2], @mixed.values_at('a', 'b') + assert_equal [1, 2], @mixed.values_at(:a, :b) end def test_indifferent_writing @@ -71,6 +91,39 @@ class HashExtTest < Test::Unit::TestCase assert_equal hash[3], 3 end + def test_indifferent_assorted + @strings = @strings.with_indifferent_access + @symbols = @symbols.with_indifferent_access + @mixed = @mixed.with_indifferent_access + + assert_equal 'a', @strings.send(:convert_key, :a) + + assert_equal 1, @strings.fetch('a') + assert_equal 1, @strings.fetch(:a.to_s) + assert_equal 1, @strings.fetch(:a) + + hashes = { :@strings => @strings, :@symbols => @symbols, :@mixed => @mixed } + method_map = { :'[]' => 1, :fetch => 1, :values_at => [1], + :has_key? => true, :include? => true, :key? => true, + :member? => true } + + hashes.each do |name, hash| + method_map.sort_by { |m| m.to_s }.each do |meth, expected| + assert_equal(expected, hash.send(meth, 'a'), + "Calling #{name}.#{meth} 'a'") + assert_equal(expected, hash.send(meth, :a), + "Calling #{name}.#{meth} :a") + end + end + + assert_equal [1, 2], @strings.values_at('a', 'b') + assert_equal [1, 2], @strings.values_at(:a, :b) + assert_equal [1, 2], @symbols.values_at('a', 'b') + assert_equal [1, 2], @symbols.values_at(:a, :b) + assert_equal [1, 2], @mixed.values_at('a', 'b') + assert_equal [1, 2], @mixed.values_at(:a, :b) + end + def test_assert_valid_keys assert_nothing_raised do { :failure => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ]) @@ -80,4 +133,12 @@ class HashExtTest < Test::Unit::TestCase { :failore => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ]) 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 end