Class: DatastaxRails::XmlSerializer

Inherits:
ActiveModel::Serializers::Xml::Serializer
  • Object
show all
Defined in:
lib/datastax_rails/serializers/xml_serializer.rb

Overview

Implementation of ActiveModel’s XML Serializer

Defined Under Namespace

Classes: Attribute

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ XmlSerializer

Returns a new instance of XmlSerializer.



185
186
187
# File 'lib/datastax_rails/serializers/xml_serializer.rb', line 185

def initialize(*args)
  super
end

Instance Method Details

#add_associations(association, records, opts) ⇒ Object

TODO: This can likely be cleaned up to simple use ActiveSupport::XmlMini.to_tag as well.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/datastax_rails/serializers/xml_serializer.rb', line 202

def add_associations(association, records, opts)
  association_name = association.to_s.singularize
  merged_options   = options.merge(opts).merge!(root: association_name, skip_instruct: true)

  if records.is_a?(Enumerable)
    tag  = ActiveSupport::XmlMini.rename_key(association.to_s, options)
    type = options[:skip_types] ? {} : { type: 'array' }

    if records.empty?
      @builder.tag!(tag, type)
    else
      @builder.tag!(tag, type) do
        records.each do |record|
          if options[:skip_types]
            record_type = {}
          else
            record_class = (record.class.to_s.underscore == association_name) ? nil : record.class.name
            record_type = { type: record_class }
          end

          record.to_xml merged_options.merge(record_type)
        end
      end
    end
  elsif (record = @serializable.send(association))
    record.to_xml(merged_options)
  end
end

#add_extra_behaviorObject



189
190
191
# File 'lib/datastax_rails/serializers/xml_serializer.rb', line 189

def add_extra_behavior
  add_includes
end

#add_includesObject



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

def add_includes
  procs = options.delete(:procs)
  @serializable.send(:serializable_add_includes, options) do |association, records, opts|
    add_associations(association, records, opts)
  end
  options[:procs] = procs
end