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


57
58
59
60
# File 'activemodel/lib/active_model/serializers/xml.rb', line 57

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options


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

def options
  @options
end

Instance Method Details

#serializable_collectionObject


66
67
68
69
70
71
72
73
74
75
76
# File 'activemodel/lib/active_model/serializers/xml.rb', line 66

def serializable_collection
  methods = Array(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


62
63
64
# File 'activemodel/lib/active_model/serializers/xml.rb', line 62

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

#serializeObject


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'activemodel/lib/active_model/serializers/xml.rb', line 78

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