Class: ActiveModel::Serializers::Xml::Serializer

Inherits:
Object
  • Object
show all
Defined in:
activemodel/lib/active_model/serializers/xml.rb

Overview

:nodoc:

Direct Known Subclasses

ActiveRecord::XmlSerializer

Defined Under Namespace

Classes: Attribute, MethodAttribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serializable, options = nil) ⇒ Serializer

Returns a new instance of Serializer.



49
50
51
52
# File 'activemodel/lib/active_model/serializers/xml.rb', line 49

def initialize(serializable, options = nil)
  @serializable = serializable
  @options = options ? options.dup : {}
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options



47
48
49
# File 'activemodel/lib/active_model/serializers/xml.rb', line 47

def options
  @options
end

Instance Method Details

#serializable_collectionObject



58
59
60
61
62
63
64
65
66
67
68
# File 'activemodel/lib/active_model/serializers/xml.rb', line 58

def serializable_collection
  methods = Array.wrap(options[:methods]).map(&:to_s)
  serializable_hash.map do |name, value|
    name = name.to_s
    if methods.include?(name)
      self.class::MethodAttribute.new(name, @serializable, value)
    else
      self.class::Attribute.new(name, @serializable, value)
    end
  end
end

#serializable_hashObject



54
55
56
# File 'activemodel/lib/active_model/serializers/xml.rb', line 54

def serializable_hash
  @serializable.serializable_hash(@options.except(:include))
end

#serializeObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'activemodel/lib/active_model/serializers/xml.rb', line 70

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_includes
    add_extra_behavior
    add_procs
    yield @builder if block_given?
  end
end