Class: ActiveObject::XmlSerializer

Inherits:
Serialization::Serializer show all
Defined in:
lib/active_object/serializers/xml_serializer.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Attribute, MethodAttribute

Instance Attribute Summary

Attributes inherited from Serialization::Serializer

#options

Instance Method Summary collapse

Methods inherited from Serialization::Serializer

#add_includes, #initialize, #serializable_attribute_names, #serializable_method_names, #serializable_names, #serializable_object, #to_s

Constructor Details

This class inherits a constructor from ActiveObject::Serialization::Serializer

Instance Method Details

#add_associations(association, objects, opts) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/active_object/serializers/xml_serializer.rb', line 217

def add_associations(association, objects, opts)
  if objects.is_a?(Enumerable)
    tag = association.to_s
    tag = tag.dasherize if dasherize?
    if objects.empty?
      builder.tag!(tag, :type => :array)
    else
      builder.tag!(tag, :type => :array) do
        association_name = association.to_s.singularize
        objects.each do |object|
          object.to_xml opts.merge(
            :root => association_name,
            :type => (object.class.to_s.underscore == association_name ? nil : object.class.name)
          )
        end
      end
    end
  else
    if object = @object.send(association)
      object.to_xml(opts.merge(:root => association))
    end
  end
end

#add_attributesObject



195
196
197
198
199
# File 'lib/active_object/serializers/xml_serializer.rb', line 195

def add_attributes
  (serializable_attributes + serializable_method_attributes).each do |attribute|
    add_tag(attribute)
  end
end

#add_procsObject



201
202
203
204
205
206
207
# File 'lib/active_object/serializers/xml_serializer.rb', line 201

def add_procs
  if procs = options.delete(:procs)
    [ *procs ].each do |proc|
      proc.call(options)
    end
  end
end

#add_tag(attribute) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/active_object/serializers/xml_serializer.rb', line 209

def add_tag(attribute)
  builder.tag!(
    dasherize? ? attribute.name.dasherize : attribute.name,
    attribute.value.to_s,
    attribute.decorations(!options[:skip_types])
  )
end

#builderObject



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/active_object/serializers/xml_serializer.rb', line 161

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

#dasherize?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/active_object/serializers/xml_serializer.rb', line 180

def dasherize?
  !options.has_key?(:dasherize) || options[:dasherize]
end

#rootObject



175
176
177
178
# File 'lib/active_object/serializers/xml_serializer.rb', line 175

def root
  root = (options[:root] || @object.class.to_s.underscore).to_s
  dasherize? ? root.dasherize : root
end

#serializable_attributesObject



184
185
186
# File 'lib/active_object/serializers/xml_serializer.rb', line 184

def serializable_attributes
  serializable_attribute_names.collect { |name| Attribute.new(name.to_s, @object) }
end

#serializable_method_attributesObject



188
189
190
191
192
193
# File 'lib/active_object/serializers/xml_serializer.rb', line 188

def serializable_method_attributes
  Array(options[:methods]).inject([]) do |method_attributes, name|
    method_attributes << MethodAttribute.new(name.to_s, @object) if @object.respond_to?(name.to_s)
    method_attributes
  end
end

#serializeObject



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/active_object/serializers/xml_serializer.rb', line 241

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, objects, opts| add_associations(association, objects, opts) }
    options[:procs] = procs
    add_procs
    yield builder if block_given?
  end
end