class Unimatrix::Activist::Serializer

Public Class Methods

new( payload = [], options = {} ) click to toggle source
# File lib/unimatrix/activist/serializer.rb, line 5
def initialize( payload = [], options = {} )
  @payload = [ payload ].flatten
  @options = options
end

Public Instance Methods

serialize( node, options = {} ) click to toggle source
# 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