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

Fixed Hash#indifferent_access to also deal with include? and fetch and nested hashes #726 [Nicholas Seckar]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@872 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2005-03-07 01:21:58 +00:00
parent 239ec08c00
commit 71742114f8
3 changed files with 92 additions and 1 deletions

View file

@ -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]

View file

@ -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:

View file

@ -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