Fixed that parameters from XML should also be presented in a hash with indifferent access [DHH] Hash#with_indifferent_access now also converts hashes kept in arrays to indifferent access (makes it easier to treat HTML and XML parameters the same) [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6532 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson
2007-04-16 22:17:59 +00:00
parent 7722e2bf6c
commit a0e78f7442
6 changed files with 20 additions and 3 deletions

View File

@@ -1,5 +1,7 @@
*SVN*
* Fixed that parameters from XML should also be presented in a hash with indifferent access [DHH]
* Tweak template format rules so that the ACCEPT header is only used if it's text/javascript. This is so ajax actions without a :format param get recognized as Mime::JS. [Rick]
* The default respond_to blocks don't set a specific extension anymore, so that both 'show.rjs' and 'show.js.rjs' will work. [Rick]

View File

@@ -49,7 +49,7 @@ class CGIMethods #:nodoc:
when Proc
strategy.call(raw_post_data)
when :xml_simple, :xml_node
raw_post_data.blank? ? {} : Hash.from_xml(raw_post_data)
raw_post_data.blank? ? {} : Hash.from_xml(raw_post_data).with_indifferent_access
when :yaml
YAML.load(raw_post_data)
end

View File

@@ -1,5 +1,7 @@
*SVN*
* Hash#with_indifferent_access now also converts hashes kept in arrays to indifferent access (makes it easier to treat HTML and XML parameters the same) [DHH]
* Hash#to_xml supports YAML attributes. #7502 [jonathan]
* Refactor ActiveSupport::JSON to be less obtuse. Add support for JSON decoding by way of Syck with ActiveSupport::JSON.decode(json_string). Prevent hash keys that are JavaScript reserved words from being unquoted during encoding. [Sam Stephenson]

View File

@@ -111,7 +111,7 @@ module ActiveSupport #:nodoc:
'forcecontent' => true,
'keeproot' => true,
'contentkey' => '__content__')
))
))
end
def create_from_xml(xml)

View File

@@ -73,8 +73,16 @@ class HashWithIndifferentAccess < Hash
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
case value
when Hash
value.with_indifferent_access
when Array
value.collect { |e| e.is_a?(Hash) ? e.with_indifferent_access : e }
else
value
end
end
end

View File

@@ -183,6 +183,11 @@ class HashExtTest < Test::Unit::TestCase
assert_equal '1234', roundtrip.default
end
def test_indifferent_hash_with_array_of_hashes
hash = { "urls" => { "url" => [ { "address" => "1" }, { "address" => "2" } ] }}.with_indifferent_access
assert_equal "1", hash[:urls][:url].first[:address]
end
def test_stringify_and_symbolize_keys_on_indifferent_preserves_hash
h = HashWithIndifferentAccess.new
h[:first] = 1