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