Class: Delineate::MapSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/delineate/map_serializer.rb

Overview

The MapSerializer class serves as the base class for processing the reading and writing of ActiveRecord model attributes through an attribute map. Each serializer class supports its own external format for the input/output of the attributes. The format handled by MapSerializer is a hash.

Instance Method Summary collapse

Constructor Details

#initialize(record, attribute_map, options = nil) ⇒ MapSerializer

Creates a serializer for a single record.

The attribute_map parameter can be an AttributeMap instance or the name of the record’s attribute map. The options hash is used to filter which attributes and associations are to be serialized for output, and can have the following keys:

:include  Specifies which optional attributes and associations to output.
:only     Restricts the attributes and associations to only those specified.
:except   Processes attributes and associations except those specified.

See the description for mapped_attributes for more info about options.



23
24
25
26
27
28
# File 'lib/delineate/map_serializer.rb', line 23

def initialize(record, attribute_map, options = nil)
  @record = record
  attribute_map = record.send(:attribute_map, attribute_map) if attribute_map.is_a?(Symbol)
  @attribute_map = attribute_map
  @options = options ? options.dup : {}
end

Instance Method Details

#serializable_recordObject

Returns the record’s mapped attributes in the serializer’s “internal” format, usually this is a hash.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/delineate/map_serializer.rb', line 53

def serializable_record
  returning(serializable_record = Hash.new) do
    serializable_attribute_names.each do |name|
      serializable_record[name] = @attribute_map.attribute_value(@record, name)
    end

    add_includes do |association, records, opts|
      polymorphic = @attribute_map.associations[association][:options][:polymorphic]
      assoc_map = association_attribute_map(association)

      if records.is_a?(Enumerable)
        serializable_record[association] = records.collect do |r|
          assoc_map = attribute_map_for_record(r) if polymorphic
          self.class.new(r, assoc_map, opts).serializable_record
        end
      else
        assoc_map = attribute_map_for_record(records) if polymorphic
        serializable_record[association] = self.class.new(records, assoc_map, opts).serializable_record
      end
    end
  end
end

#serialize(options = {}) ⇒ Object

Returns the record’s mapped attributes in the serializer’s intrinsic format.

For the MapSerializer class the attributes are returned as a hash, and the options parameter is ignored.



34
35
36
37
# File 'lib/delineate/map_serializer.rb', line 34

def serialize(options = {})
  @attribute_map.resolve! unless @attribute_map.resolved?
  serializable_record
end

#serialize_in(attributes, options = {}) ⇒ Object

Takes a record’s attributes in the serializer’s intrinsic format, and returns a hash suitable for direct assignment to the record’s collection of attributes. For example:

s = ActiveRecord::AttributeMap::MapSerializer.new(record, :api)
record.attributes = s.serialize_in(attrs_hash)


46
47
48
49
# File 'lib/delineate/map_serializer.rb', line 46

def serialize_in(attributes, options = {})
  @attribute_map.resolve! unless @attribute_map.resolved?
  @attribute_map.map_attributes_for_write(attributes, options)
end