aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-03-18 07:30:09 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-03-18 07:30:09 +0000
commit27ba5edef1c4264a8d1c0e54675723d37a391dd8 (patch)
tree83e5292708b2452b26d92f4a95dd6094f298c64e /activesupport
parent3202fbabe6df3591d7e2c35727ea9c8b68df8828 (diff)
downloadrails-27ba5edef1c4264a8d1c0e54675723d37a391dd8.tar.gz
rails-27ba5edef1c4264a8d1c0e54675723d37a391dd8.tar.bz2
rails-27ba5edef1c4264a8d1c0e54675723d37a391dd8.zip
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
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/hash/conversions.rb6
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb8
3 files changed, 11 insertions, 5 deletions
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
<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>
+ <content type="yaml">--- \n1: should be an integer\n:message: Have a nice day\narray: \n- should-have-dashes: true\n should_have_underscores: true\n</content>
<author-email-address>david@loudthinking.com</author-email-address>
<parent-id></parent-id>
</topic>
@@ -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
<approved type="boolean"></approved>
<written-on type="date"></written-on>
<viewed-at type="datetime"></viewed-at>
+ <content type="yaml"></content>
<parent-id></parent-id>
</topic>
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