aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/hash
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/core_ext/hash')
-rw-r--r--activesupport/lib/active_support/core_ext/hash/conversions.rb39
1 files changed, 39 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb
new file mode 100644
index 0000000000..3218f6302c
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -0,0 +1,39 @@
+module ActiveSupport #:nodoc:
+ module CoreExtensions #:nodoc:
+ module Hash #:nodoc:
+ module Conversions
+ XML_TYPE_NAMES = {
+ "String" => "string",
+ "Fixnum" => "integer",
+ "Date" => "date",
+ "Time" => "datetime"
+ }
+
+ XML_FORMATTING = {
+ "date" => Proc.new { |date| date.to_s(:db) },
+ "datetime" => Proc.new { |time| time.to_s(:db) }
+ }
+
+ def to_xml(options = {})
+ options.reverse_merge!({ :builder => Builder::XmlMarkup.new, :root => "hash" })
+
+ options[:builder].__send__(options[:root]) do
+ for key in keys
+ value = self[key]
+
+ if value.is_a?(self.class)
+ value.to_xml(:builder => options[:builder], :root => key)
+ else
+ type_name = XML_TYPE_NAMES[value.class.to_s]
+ options[:builder].__send__(key.to_s.dasherize,
+ XML_FORMATTING[type_name] ? XML_FORMATTING[type_name].call(value) : value,
+ value.nil? ? { } : { :type => type_name }
+ )
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+end