aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-10-23 00:42:16 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-10-23 00:42:16 +0000
commit24077a1ffc3d7e944ba0e78df9a387523fb4f419 (patch)
tree8043874317d33516318635cdffde32193848eb70 /activesupport
parent902533e6f00114717e13758339299d9acd328b10 (diff)
downloadrails-24077a1ffc3d7e944ba0e78df9a387523fb4f419.tar.gz
rails-24077a1ffc3d7e944ba0e78df9a387523fb4f419.tar.bz2
rails-24077a1ffc3d7e944ba0e78df9a387523fb4f419.zip
Hash#to_xml handles symbol values. Closes #9954.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7997 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/hash/conversions.rb5
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb7
3 files changed, 12 insertions, 2 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 947828064b..cb7a782704 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Hash#to_xml handles symbol values. #9954 [Assaf]
+
* Hash#symbolize_keys behaves well with integer keys. #9890 [PotatoSalad]
* Multibyte: String#slice supports regexp argument. #9646 [yob]
diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb
index 74fe577030..bbe35c25e4 100644
--- a/activesupport/lib/active_support/core_ext/hash/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -47,6 +47,7 @@ module ActiveSupport #:nodoc:
module Hash #:nodoc:
module Conversions
XML_TYPE_NAMES = {
+ "Symbol" => "symbol",
"Fixnum" => "integer",
"Bignum" => "integer",
"BigDecimal" => "decimal",
@@ -59,14 +60,18 @@ module ActiveSupport #:nodoc:
} unless defined?(XML_TYPE_NAMES)
XML_FORMATTING = {
+ "symbol" => Proc.new { |symbol| symbol.to_s },
"date" => Proc.new { |date| date.to_s(:db) },
"datetime" => Proc.new { |time| time.xmlschema },
"binary" => Proc.new { |binary| Base64.encode64(binary) },
"yaml" => Proc.new { |yaml| yaml.to_yaml }
} unless defined?(XML_FORMATTING)
+ # TODO: use Time.xmlschema instead of Time.parse;
+ # use regexp instead of Date.parse
unless defined?(XML_PARSING)
XML_PARSING = {
+ "symbol" => Proc.new { |symbol| symbol.to_sym },
"date" => Proc.new { |date| ::Date.parse(date) },
"datetime" => Proc.new { |time| ::Time.parse(time).utc },
"integer" => Proc.new { |integer| integer.to_i },
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index 359eacaba3..fe728919cd 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -336,13 +336,14 @@ class HashToXmlTest < Test::Unit::TestCase
end
def test_one_level_with_types
- xml = { :name => "David", :street => "Paulina", :age => 26, :age_in_millis => 820497600000, :moved_on => Date.new(2005, 11, 15) }.to_xml(@xml_options)
+ xml = { :name => "David", :street => "Paulina", :age => 26, :age_in_millis => 820497600000, :moved_on => Date.new(2005, 11, 15), :resident => :yes }.to_xml(@xml_options)
assert_equal "<person>", xml.first(8)
assert xml.include?(%(<street>Paulina</street>))
assert xml.include?(%(<name>David</name>))
assert xml.include?(%(<age type="integer">26</age>))
assert xml.include?(%(<age-in-millis type="integer">820497600000</age-in-millis>))
assert xml.include?(%(<moved-on type="date">2005-11-15</moved-on>))
+ assert xml.include?(%(<resident type="symbol">yes</resident>))
end
def test_one_level_with_nils
@@ -416,6 +417,7 @@ class HashToXmlTest < Test::Unit::TestCase
<parent-id></parent-id>
<ad-revenue type="decimal">1.5</ad-revenue>
<optimum-viewing-angle type="float">135</optimum-viewing-angle>
+ <resident type="symbol">yes</resident>
</topic>
EOT
@@ -432,7 +434,8 @@ class HashToXmlTest < Test::Unit::TestCase
:author_email_address => "david@loudthinking.com",
:parent_id => nil,
:ad_revenue => BigDecimal("1.50"),
- :optimum_viewing_angle => 135.0
+ :optimum_viewing_angle => 135.0,
+ :resident => :yes
}.stringify_keys
assert_equal expected_topic_hash, Hash.from_xml(topic_xml)["topic"]