From cb978baafa68c9bb4f1311df417947249586f47d Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 29 Sep 2006 22:23:16 +0000 Subject: 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 --- activesupport/CHANGELOG | 2 ++ .../lib/active_support/core_ext/hash/conversions.rb | 18 ++++++++++-------- activesupport/test/core_ext/array_ext_test.rb | 18 +++++++++++------- activesupport/test/core_ext/hash_ext_test.rb | 8 +++++++- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 5784e6d922..bd70d2cde4 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -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] diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb index de67b0b00d..65047b2d3c 100644 --- a/activesupport/lib/active_support/core_ext/hash/conversions.rb +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -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 diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 64c9a8b4ff..957c56c92d 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -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 "", xml.first(17) - assert xml.include?(%(26)) - assert xml.include?(%(David)) - assert xml.include?(%(31)) - assert xml.include?(%(Jason)) + assert_equal "", xml.first(17), xml + assert xml.include?(%(26)), xml + assert xml.include?(%(820497600000)), xml + assert xml.include?(%(David)), xml + assert xml.include?(%(31)), xml + assert xml.include?(%(1.0)), xml + assert xml.include?(%(Jason)), 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 "", xml.first(16) diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index df4aadfbcc..020e383c1a 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -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 "", xml.first(8) assert xml.include?(%(Paulina)) assert xml.include?(%(David)) assert xml.include?(%(26)) + assert xml.include?(%(820497600000)) assert xml.include?(%(2005-11-15)) end @@ -315,6 +316,7 @@ class HashToXmlTest < Test::Unit::TestCase 1 true 0 + 2592000000 2003-07-16 2003-07-16T09:28:00+0000 Have a nice day @@ -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 1 false 0 + 2592000000 2003-07-16 2003-07-16T09:28:00+0000 Have a nice day @@ -384,6 +388,7 @@ class HashToXmlTest < Test::Unit::TestCase 1 false 0 + 2592000000 2003-07-16 2003-07-16T09:28:00+0000 Have a nice day @@ -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", -- cgit v1.2.3