| Module | ActiveSupport::CoreExtensions::Hash::Conversions |
| In: |
vendor/rails/activesupport/lib/active_support/core_ext/hash/conversions.rb
|
| XML_TYPE_NAMES | = | { ::Fixnum => "integer", ::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 24
24: def self.included(klass)
25: klass.extend(ClassMethods)
26: end
# File vendor/rails/activesupport/lib/active_support/core_ext/hash/conversions.rb, line 28
28: def to_xml(options = {})
29: options[:indent] ||= 2
30: options.reverse_merge!({ :builder => Builder::XmlMarkup.new(:indent => options[:indent]),
31: :root => "hash" })
32: options[:builder].instruct! unless options.delete(:skip_instruct)
33: dasherize = !options.has_key?(:dasherize) || options[:dasherize]
34: root = dasherize ? options[:root].to_s.dasherize : options[:root].to_s
35:
36: options[:builder].__send__(root) do
37: each do |key, value|
38: case value
39: when ::Hash
40: value.to_xml(options.merge({ :root => key, :skip_instruct => true }))
41: when ::Array
42: value.to_xml(options.merge({ :root => key, :children => key.to_s.singularize, :skip_instruct => true}))
43: when ::Method, ::Proc
44: # If the Method or Proc takes two arguments, then
45: # pass the suggested child element name. This is
46: # used if the Method or Proc will be operating over
47: # multiple records and needs to create an containing
48: # element that will contain the objects being
49: # serialized.
50: if 1 == value.arity
51: value.call(options.merge({ :root => key, :skip_instruct => true }))
52: else
53: value.call(options.merge({ :root => key, :skip_instruct => true }), key.to_s.singularize)
54: end
55: else
56: if value.respond_to?(:to_xml)
57: value.to_xml(options.merge({ :root => key, :skip_instruct => true }))
58: else
59: type_name = XML_TYPE_NAMES[value.class]
60:
61: key = dasherize ? key.to_s.dasherize : key.to_s
62:
63: attributes = options[:skip_types] || value.nil? || type_name.nil? ? { } : { :type => type_name }
64: if value.nil?
65: attributes[:nil] = true
66: end
67:
68: options[:builder].tag!(key,
69: XML_FORMATTING[type_name] ? XML_FORMATTING[type_name].call(value) : value,
70: attributes
71: )
72: end
73: end
74: end
75: end
76:
77: end