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
53
54
55
# File 'activemodel/lib/active_model/serializers/xml.rb', line 49

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

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

#attributes_hashObject

To replicate the behavior in ActiveRecord#attributes, :except takes precedence over :only. If :only is not set for a N level model but is set for the N+1 level models, then because :except is set to a default value, the second level model can have both :except and :only set. So if :only is set, always delete :except.



63
64
65
66
67
68
69
70
71
72
# File 'activemodel/lib/active_model/serializers/xml.rb', line 63

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

#serializable_attributesObject



74
75
76
77
78
# File 'activemodel/lib/active_model/serializers/xml.rb', line 74

def serializable_attributes
  attributes_hash.map do |name, value|
    self.class::Attribute.new(name, @serializable, value)
  end
end

#serializable_methodsObject



80
81
82
83
84
# File 'activemodel/lib/active_model/serializers/xml.rb', line 80

def serializable_methods
  Array.wrap(options[:methods]).map do |name|
    self.class::MethodAttribute.new(name.to_s, @serializable) if @serializable.respond_to?(name.to_s)
  end.compact
end

#serializeObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'activemodel/lib/active_model/serializers/xml.rb', line 86

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