class ActiveModel::Serializers::Xml::Serializer
:nodoc:
def add_attributes_and_methods
def add_attributes_and_methods (serializable_attributes + serializable_methods).each do |attribute| key = ActiveSupport::XmlMini.rename_key(attribute.name, options) ActiveSupport::XmlMini.to_tag(key, attribute.value, options.merge(attribute.decorations)) end end
def add_extra_behavior
def add_extra_behavior end
def add_procs
def add_procs if procs = options.delete(:procs) Array.wrap(procs).each do |proc| if proc.arity == 1 proc.call(options) else proc.call(options, @serializable) end end end end
def attributes_hash
level model can have both :except and :only set. So if
then because :except is set to a default value, the second
for a N level model but is set for the N+1 level models,
takes precedence over :only. If :only is not set
To replicate the behavior in ActiveRecord#attributes, :except
def attributes_hash attributes = @serializable.attributes if options[:only].any? attributes.slice(*options[:only]) elsif options[:except].any? attributes.except(*options[:except]) else attributes end end
def initialize(serializable, options = nil)
def initialize(serializable, options = nil) @serializable = serializable @options = options ? options.dup : {} @options[:only] = Array.wrap(@options[:only]).map { |n| n.to_s } @options[:except] = Array.wrap(@options[:except]).map { |n| n.to_s } end
def serializable_attributes
def serializable_attributes attributes_hash.map do |name, value| self.class::Attribute.new(name, @serializable, value) end end
def serializable_methods
def serializable_methods Array.wrap(options[:methods]).inject([]) do |methods, name| methods << self.class::MethodAttribute.new(name.to_s, @serializable) if @serializable.respond_to?(name.to_s) methods end end
def serialize
def serialize require 'builder' unless defined? ::Builder options[:indent] ||= 2 options[:builder] ||= ::Builder::XmlMarkup.new(:indent => options[:indent]) @builder = options[:builder] @builder.instruct! unless options[:skip_instruct] root = (options[:root] || @serializable.class.model_name.element).to_s root = ActiveSupport::XmlMini.rename_key(root, options) args = [root] args << {:xmlns => options[:namespace]} if options[:namespace] args << {:type => options[:type]} if options[:type] && !options[:skip_types] @builder.tag!(*args) do add_attributes_and_methods add_extra_behavior add_procs yield @builder if block_given? end end