class ActiveRecord::XmlSerializer
:nodoc:
def add_associations(association, records, opts)
def add_associations(association, records, opts) if records.is_a?(Enumerable) tag = reformat_name(association.to_s) type = options[:skip_types] ? {} : {:type => "array"} if records.empty? builder.tag!(tag, type) else builder.tag!(tag, type) do association_name = association.to_s.singularize records.each do |record| if options[:skip_types] record_type = {} else record_class = (record.class.to_s.underscore == association_name) ? nil : record.class.name record_type = {:type => record_class} end record.to_xml opts.merge(:root => association_name).merge(record_type) end end end else if record = @record.send(association) record.to_xml(opts.merge(:root => association)) end end end
def add_attributes
def add_attributes (serializable_attributes + serializable_method_attributes).each do |attribute| add_tag(attribute) end end
def add_procs
def add_procs if procs = options.delete(:procs) [ *procs ].each do |proc| proc.call(options) end end end
def add_tag(attribute)
def add_tag(attribute) builder.tag!( reformat_name(attribute.name), attribute.value.to_s, attribute.decorations(!options[:skip_types]) ) end
def builder
def builder @builder ||= begin options[:indent] ||= 2 builder = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) unless options[:skip_instruct] builder.instruct! options[:skip_instruct] = true end builder end end
def camelize?
def camelize? options.has_key?(:camelize) && options[:camelize] end
def dasherize?
def dasherize? !options.has_key?(:dasherize) || options[:dasherize] end
def reformat_name(name)
def reformat_name(name) name = name.camelize if camelize? dasherize? ? name.dasherize : name end
def root
def root root = (options[:root] || @record.class.model_name.singular).to_s reformat_name(root) end
def serializable_attributes
def serializable_attributes serializable_attribute_names.collect { |name| Attribute.new(name, @record) } end
def serializable_method_attributes
def serializable_method_attributes Array(options[:methods]).inject([]) do |method_attributes, name| method_attributes << MethodAttribute.new(name.to_s, @record) if @record.respond_to?(name.to_s) method_attributes end end
def serialize
def serialize args = [root] if options[:namespace] args << {:xmlns=>options[:namespace]} end if options[:type] args << {:type=>options[:type]} end builder.tag!(*args) do add_attributes procs = options.delete(:procs) add_includes { |association, records, opts| add_associations(association, records, opts) } options[:procs] = procs add_procs yield builder if block_given? end end