From 27ba5edef1c4264a8d1c0e54675723d37a391dd8 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 18 Mar 2007 07:30:09 +0000 Subject: Hash#to_xml supports YAML attributes; ActiveRecord::Base#to_xml support serialized attributes. Closes #7502. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6444 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/CHANGELOG | 2 ++ activesupport/lib/active_support/core_ext/hash/conversions.rb | 6 ++++-- activesupport/test/core_ext/hash_ext_test.rb | 8 +++++--- 3 files changed, 11 insertions(+), 5 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 368989877e..e0108f27bc 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* 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] * alias_method_chain preserves the original method's visibility. #7854 [Jonathan Viney] diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb index f0ce27508f..bbfc2502a1 100644 --- a/activesupport/lib/active_support/core_ext/hash/conversions.rb +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -38,7 +38,8 @@ module ActiveSupport #:nodoc: XML_FORMATTING = { "date" => Proc.new { |date| date.to_s(:db) }, "datetime" => Proc.new { |time| time.xmlschema }, - "binary" => Proc.new { |binary| Base64.encode64(binary) } + "binary" => Proc.new { |binary| Base64.encode64(binary) }, + "yaml" => Proc.new { |yaml| yaml.to_yaml } } unless defined? XML_FORMATTING def self.included(klass) @@ -105,7 +106,7 @@ module ActiveSupport #:nodoc: module ClassMethods def from_xml(xml) # TODO: Refactor this into something much cleaner that doesn't rely on XmlSimple - undasherize_keys(typecast_xml_value(XmlSimple.xml_in(xml, + typecast_xml_value(undasherize_keys(XmlSimple.xml_in(xml, 'forcearray' => false, 'forcecontent' => true, 'keeproot' => true, @@ -129,6 +130,7 @@ module ActiveSupport #:nodoc: when "boolean" then content.strip == "true" when "datetime" then ::Time.parse(content).utc when "date" then ::Date.parse(content) + when "yaml" then YAML::load(content) rescue content else content end else diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 64d550aaf5..5a5bd7fe5a 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -376,7 +376,7 @@ class HashToXmlTest < Test::Unit::TestCase 2592000000 2003-07-16 2003-07-16T09:28:00+0000 - Have a nice day + --- \n1: should be an integer\n:message: Have a nice day\narray: \n- should-have-dashes: true\n should_have_underscores: true\n david@loudthinking.com @@ -391,7 +391,7 @@ class HashToXmlTest < Test::Unit::TestCase :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", + :content => { :message => "Have a nice day", 1 => "should be an integer", "array" => [{ "should-have-dashes" => true, "should_have_underscores" => true }] }, :author_email_address => "david@loudthinking.com", :parent_id => nil }.stringify_keys @@ -407,6 +407,7 @@ class HashToXmlTest < Test::Unit::TestCase + EOT @@ -416,7 +417,8 @@ class HashToXmlTest < Test::Unit::TestCase :id => nil, :approved => nil, :written_on => nil, - :viewed_at => nil, + :viewed_at => nil, + :content => nil, :parent_id => nil }.stringify_keys -- cgit v1.2.3