Hash#to_xml supports Bignum and BigDecimal. Closes #6313.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5209 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2006-09-29 22:23:16 +00:00
parent 7ac6ed893f
commit cb978baafa
4 changed files with 30 additions and 16 deletions

View File

@ -1,5 +1,7 @@
*SVN*
* Hash#to_xml supports Bignum and BigDecimal. #6313 [edibiase]
* Don't undefine #class in OptionMerger [Rick]
* Hash.create_from_xml has been renamed to Hash.from_xml, alias will exist until Rails 2.0 [DHH]

View File

@ -6,13 +6,15 @@ module ActiveSupport #:nodoc:
module Hash #:nodoc:
module Conversions
XML_TYPE_NAMES = {
::Fixnum => "integer",
::Float => "float",
::Date => "date",
::DateTime => "datetime",
::Time => "datetime",
::TrueClass => "boolean",
::FalseClass => "boolean"
"Fixnum" => "integer",
"Bignum" => "integer",
"BigDecimal" => "numeric",
"Float" => "float",
"Date" => "date",
"DateTime" => "datetime",
"Time" => "datetime",
"TrueClass" => "boolean",
"FalseClass" => "boolean"
} unless defined? XML_TYPE_NAMES
XML_FORMATTING = {
@ -56,7 +58,7 @@ module ActiveSupport #:nodoc:
if value.respond_to?(:to_xml)
value.to_xml(options.merge({ :root => key, :skip_instruct => true }))
else
type_name = XML_TYPE_NAMES[value.class]
type_name = XML_TYPE_NAMES[value.class.name]
key = dasherize ? key.to_s.dasherize : key.to_s

View File

@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/../abstract_unit'
require 'bigdecimal'
class ArrayExtToParamTests < Test::Unit::TestCase
def test_string_array
@ -116,19 +117,22 @@ end
class ArrayToXmlTests < Test::Unit::TestCase
def test_to_xml
xml = [
{ :name => "David", :age => 26 }, { :name => "Jason", :age => 31 }
{ :name => "David", :age => 26, :age_in_millis => 820497600000 },
{ :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') }
].to_xml(:skip_instruct => true, :indent => 0)
assert_equal "<records><record>", xml.first(17)
assert xml.include?(%(<age type="integer">26</age>))
assert xml.include?(%(<name>David</name>))
assert xml.include?(%(<age type="integer">31</age>))
assert xml.include?(%(<name>Jason</name>))
assert_equal "<records><record>", xml.first(17), xml
assert xml.include?(%(<age type="integer">26</age>)), xml
assert xml.include?(%(<age-in-millis type="integer">820497600000</age-in-millis>)), xml
assert xml.include?(%(<name>David</name>)), xml
assert xml.include?(%(<age type="integer">31</age>)), xml
assert xml.include?(%(<age-in-millis type="numeric">1.0</age-in-millis>)), xml
assert xml.include?(%(<name>Jason</name>)), xml
end
def test_to_xml_with_dedicated_name
xml = [
{ :name => "David", :age => 26 }, { :name => "Jason", :age => 31 }
{ :name => "David", :age => 26, :age_in_millis => 820497600000 }, { :name => "Jason", :age => 31 }
].to_xml(:skip_instruct => true, :indent => 0, :root => "people")
assert_equal "<people><person>", xml.first(16)

View File

@ -255,11 +255,12 @@ class HashToXmlTest < Test::Unit::TestCase
end
def test_one_level_with_types
xml = { :name => "David", :street => "Paulina", :age => 26, :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) }.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>))
end
@ -315,6 +316,7 @@ class HashToXmlTest < Test::Unit::TestCase
<id type="integer">1</id>
<approved type="boolean"> true </approved>
<replies-count type="integer">0</replies-count>
<replies-close-in type="integer">2592000000</replies-close-in>
<written-on type="date">2003-07-16</written-on>
<viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
<content>Have a nice day</content>
@ -329,6 +331,7 @@ class HashToXmlTest < Test::Unit::TestCase
:id => 1,
:approved => true,
:replies_count => 0,
:replies_close_in => 2592000000,
:written_on => Date.new(2003, 7, 16),
:viewed_at => Time.utc(2003, 7, 16, 9, 28),
:content => "Have a nice day",
@ -372,6 +375,7 @@ class HashToXmlTest < Test::Unit::TestCase
<id type="integer">1</id>
<approved type="boolean">false</approved>
<replies-count type="integer">0</replies-count>
<replies-close-in type="integer">2592000000</replies-close-in>
<written-on type="date">2003-07-16</written-on>
<viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
<content>Have a nice day</content>
@ -384,6 +388,7 @@ class HashToXmlTest < Test::Unit::TestCase
<id type="integer">1</id>
<approved type="boolean">false</approved>
<replies-count type="integer">0</replies-count>
<replies-close-in type="integer">2592000000</replies-close-in>
<written-on type="date">2003-07-16</written-on>
<viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
<content>Have a nice day</content>
@ -399,6 +404,7 @@ class HashToXmlTest < Test::Unit::TestCase
:id => 1,
:approved => false,
:replies_count => 0,
:replies_close_in => 2592000000,
:written_on => Date.new(2003, 7, 16),
:viewed_at => Time.utc(2003, 7, 16, 9, 28),
:content => "Have a nice day",