| Module | ActiveSupport::CoreExtensions::Hash::Conversions |
| In: |
vendor/rails/activesupport/lib/active_support/core_ext/hash/conversions.rb
|
| XML_TYPE_NAMES | = | { "Fixnum" => "integer", "Bignum" => "integer", "BigDecimal" => "numeric", "Float" => "float", "Date" => "date", "DateTime" => "datetime", "Time" => "datetime", "TrueClass" => "boolean", "FalseClass" => "boolean" |
| XML_FORMATTING | = | { "date" => Proc.new { |date| date.to_s(:db) }, "datetime" => Proc.new { |time| time.xmlschema }, "binary" => Proc.new { |binary| Base64.encode64(binary) } |
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/conversions.rb, line 47
47: def self.included(klass)
48: klass.extend(ClassMethods)
49: end
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/conversions.rb, line 51
51: def to_xml(options = {})
52: options[:indent] ||= 2
53: options.reverse_merge!({ :builder => Builder::XmlMarkup.new(:indent => options[:indent]),
54: :root => "hash" })
55: options[:builder].instruct! unless options.delete(:skip_instruct)
56: dasherize = !options.has_key?(:dasherize) || options[:dasherize]
57: root = dasherize ? options[:root].to_s.dasherize : options[:root].to_s
58:
59: options[:builder].__send__(:method_missing, root) do
60: each do |key, value|
61: case value
62: when ::Hash
63: value.to_xml(options.merge({ :root => key, :skip_instruct => true }))
64: when ::Array
65: value.to_xml(options.merge({ :root => key, :children => key.to_s.singularize, :skip_instruct => true}))
66: when ::Method, ::Proc
67: # If the Method or Proc takes two arguments, then
68: # pass the suggested child element name. This is
69: # used if the Method or Proc will be operating over
70: # multiple records and needs to create an containing
71: # element that will contain the objects being
72: # serialized.
73: if 1 == value.arity
74: value.call(options.merge({ :root => key, :skip_instruct => true }))
75: else
76: value.call(options.merge({ :root => key, :skip_instruct => true }), key.to_s.singularize)
77: end
78: else
79: if value.respond_to?(:to_xml)
80: value.to_xml(options.merge({ :root => key, :skip_instruct => true }))
81: else
82: type_name = XML_TYPE_NAMES[value.class.name]
83:
84: key = dasherize ? key.to_s.dasherize : key.to_s
85:
86: attributes = options[:skip_types] || value.nil? || type_name.nil? ? { } : { :type => type_name }
87: if value.nil?
88: attributes[:nil] = true
89: end
90:
91: options[:builder].tag!(key,
92: XML_FORMATTING[type_name] ? XML_FORMATTING[type_name].call(value) : value,
93: attributes
94: )
95: end
96: end
97: end
98: end
99:
100: end