Class: Unimatrix::Activist::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/unimatrix/activist/serializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(payload = [], options = {}) ⇒ Serializer

Returns a new instance of Serializer.



5
6
7
8
# File 'lib/unimatrix/activist/serializer.rb', line 5

def initialize( payload = [], options = {} )
  @payload = [ payload ].flatten
  @options = options
end

Instance Method Details

#serialize(node, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/unimatrix/activist/serializer.rb', line 10

def serialize( node, options = {} )
  result = {}
  result[ node ] = @payload.map do | object |
    node_object = {}
    node_object[ :type_name ] = (
      object.respond_to?( :type_name ) ?
        object.type_name :
        object.class.name.gsub( /Unimatrix::Activist/, '' ).underscore
    )
    if object.respond_to?( :fields )
      object.fields.each do | name, options |
        node_object[ name.to_sym ] = object.send( name )
      end
    end
    node_object
  end
  result
end