aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/hash/conversions.rb
diff options
context:
space:
mode:
authorNeeraj Singh <neerajdotname@gmail.com>2010-04-27 16:45:26 -0400
committerJosé Valim <jose.valim@gmail.com>2010-04-29 09:04:34 +0200
commit580dd3b052e471aa680b80c0e1d8ebe7bd7a3831 (patch)
tree4e10d448aff1b744346cccf62ce08a793de2d931 /activesupport/lib/active_support/core_ext/hash/conversions.rb
parent1b816d502444ce2b3153d8c689d0057f1c257eee (diff)
downloadrails-580dd3b052e471aa680b80c0e1d8ebe7bd7a3831.tar.gz
rails-580dd3b052e471aa680b80c0e1d8ebe7bd7a3831.tar.bz2
rails-580dd3b052e471aa680b80c0e1d8ebe7bd7a3831.zip
array.to_xml should be able to handle all types of data elements [#4490 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activesupport/lib/active_support/core_ext/hash/conversions.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/hash/conversions.rb55
1 files changed, 8 insertions, 47 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb
index c882434f78..1b2f69f573 100644
--- a/activesupport/lib/active_support/core_ext/hash/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -3,6 +3,7 @@ require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/hash/reverse_merge'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/string/inflections'
+require 'active_support/core_ext/hash/conversions_xml_value'
class Hash
# This module exists to decorate files deserialized using Hash.from_xml with
@@ -19,6 +20,8 @@ class Hash
end
end
+ include XmlValue
+
XML_TYPE_NAMES = {
"Symbol" => "symbol",
"Fixnum" => "integer",
@@ -29,7 +32,9 @@ class Hash
"FalseClass" => "boolean",
"Date" => "date",
"DateTime" => "datetime",
- "Time" => "datetime"
+ "Time" => "datetime",
+ "Array" => "array",
+ "Hash" => "hash"
} unless defined?(XML_TYPE_NAMES)
XML_FORMATTING = {
@@ -135,57 +140,13 @@ class Hash
:root => "hash" })
options[:builder].instruct! unless options.delete(:skip_instruct)
root = rename_key(options[:root].to_s, options)
-
+ # common upto this point
options[:builder].__send__(:method_missing, root) do
each do |key, value|
- case value
- when ::Hash
- value.to_xml(options.merge({ :root => key, :skip_instruct => true }))
- when ::Array
- value.to_xml(options.merge({ :root => key, :children => key.to_s.singularize, :skip_instruct => true}))
- when ::Method, ::Proc
- # If the Method or Proc takes two arguments, then
- # pass the suggested child element name. This is
- # used if the Method or Proc will be operating over
- # multiple records and needs to create an containing
- # element that will contain the objects being
- # serialized.
- if 1 == value.arity
- value.call(options.merge({ :root => key, :skip_instruct => true }))
- else
- value.call(options.merge({ :root => key, :skip_instruct => true }), key.to_s.singularize)
- end
- else
- if value.respond_to?(:to_xml)
- value.to_xml(options.merge({ :root => key, :skip_instruct => true }))
- else
- type_name = XML_TYPE_NAMES[value.class.name]
-
- key = rename_key(key.to_s, options)
-
- attributes = options[:skip_types] || value.nil? || type_name.nil? ? { } : { :type => type_name }
- if value.nil?
- attributes[:nil] = true
- end
-
- options[:builder].tag!(key,
- XML_FORMATTING[type_name] ? XML_FORMATTING[type_name].call(value) : value,
- attributes
- )
- end
- end
+ xml_value(key, value, options)
end
-
yield options[:builder] if block_given?
end
-
- end
-
- def rename_key(key, options = {})
- camelize = options.has_key?(:camelize) && options[:camelize]
- dasherize = !options.has_key?(:dasherize) || options[:dasherize]
- key = key.camelize if camelize
- dasherize ? key.dasherize : key
end
class << self