From 4defe1ac8a1689262a9a9f2c030714727f72e06e Mon Sep 17 00:00:00 2001 From: Jeremy Kemper <jeremy@bitsweat.net> Date: Thu, 29 Jun 2006 02:00:44 +0000 Subject: More compatible Hash.create_from_xml. Closes #5523. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4510 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/CHANGELOG | 2 ++ .../active_support/core_ext/hash/conversions.rb | 2 ++ activesupport/test/core_ext/hash_ext_test.rb | 23 ++++++++++++++++++++++ 3 files changed, 27 insertions(+) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index e1d34edb90..94470cf139 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* More compatible Hash.create_from_xml. #5523 [nunemaker@gmail.com] + * Added Enumerable#sum for calculating a sum from the elements [DHH, jonathan@daikini.com]. Examples: [1, 2, 3].sum diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb index ac4a612e49..1e7f8eb68d 100644 --- a/activesupport/lib/active_support/core_ext/hash/conversions.rb +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -113,6 +113,8 @@ module ActiveSupport #:nodoc: when 1 then value.first else value end + when "String" + value else raise "can't typecast #{value.inspect}" end diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 65050de15a..bd197a7e4e 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -357,4 +357,27 @@ class HashToXmlTest < Test::Unit::TestCase assert_equal expected_topic_hash, Hash.create_from_xml(topics_xml)["topics"]["topic"].first end + + def test_single_record_from_xml_with_attributes_other_than_type + topic_xml = <<-EOT + <rsp stat="ok"> + <photos page="1" pages="1" perpage="100" total="16"> + <photo id="175756086" owner="55569174@N00" secret="0279bf37a1" server="76" title="Colored Pencil PhotoBooth Fun" ispublic="1" isfriend="0" isfamily="0"/> + </photos> + </rsp> + EOT + + expected_topic_hash = { + :id => "175756086", + :owner => "55569174@N00", + :secret => "0279bf37a1", + :server => "76", + :title => "Colored Pencil PhotoBooth Fun", + :ispublic => "1", + :isfriend => "0", + :isfamily => "0", + }.stringify_keys + + assert_equal expected_topic_hash, Hash.create_from_xml(topic_xml)["rsp"]["photos"]["photo"] + end end \ No newline at end of file -- cgit v1.2.3