Sha256: 295229e0a6914218079c79dae77a98e052010cb786956cabf5614a0ec5121f6d

Contents?: true

Size: 1.08 KB

Versions: 11

Compression:

Stored size: 1.08 KB

Contents

module Unimatrix

  class Serializer

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

    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.split( '::' ).last.underscore
        )
        if object.respond_to?( :fields )
          object.fields.each do | name, options |
            value = object.send( name ) if object.respond_to?( name )
            if value.is_a?( Struct )
              nested_attributes = value.members
              nested_attributes.each do | nested_attribute |
                key = "#{ name }.#{ nested_attribute }"
                node_object[ key.to_sym ] = value.send( nested_attribute )
              end
            else
              node_object[ name.to_sym ] = value
            end
          end
        end
        node_object
      end
      result
    end

  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
unimatrix-2.3.1 lib/unimatrix/serializer.rb
unimatrix-2.2.1 lib/unimatrix/serializer.rb
unimatrix-2.1.1 lib/unimatrix/serializer.rb
unimatrix-2.0.1 lib/unimatrix/serializer.rb
unimatrix-2.0.0 lib/unimatrix/serializer.rb
unimatrix-1.4.3 lib/unimatrix/serializer.rb
unimatrix-1.4.1 lib/unimatrix/serializer.rb
unimatrix-1.4.0 lib/unimatrix/serializer.rb
unimatrix-1.2.0 lib/unimatrix/serializer.rb
unimatrix-1.1.0 lib/unimatrix/serializer.rb
unimatrix-1.0.0 lib/unimatrix/serializer.rb