Module: Serega::SeregaObjectSerializer::InstanceMethods

Included in:
Serega::SeregaObjectSerializer
Defined in:
lib/serega/object_serializer.rb

Overview

SeregaObjectSerializer instance methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#batch_loadersObject (readonly)

Returns the value of attribute batch_loaders.



13
14
15
# File 'lib/serega/object_serializer.rb', line 13

def batch_loaders
  @batch_loaders
end

#contextObject (readonly)

Returns the value of attribute context.



13
14
15
# File 'lib/serega/object_serializer.rb', line 13

def context
  @context
end

#manyObject (readonly)

Returns the value of attribute many.



13
14
15
# File 'lib/serega/object_serializer.rb', line 13

def many
  @many
end

#optsObject (readonly)

Returns the value of attribute opts.



13
14
15
# File 'lib/serega/object_serializer.rb', line 13

def opts
  @opts
end

#planObject (readonly)

Returns the value of attribute plan.



13
14
15
# File 'lib/serega/object_serializer.rb', line 13

def plan
  @plan
end

Instance Method Details

#initialize(context:, plan:, many: nil, **opts) ⇒ SeregaObjectSerializer

Returns New SeregaObjectSerializer.

Parameters:

  • plan (SeregaPlan)

    Serialization plan

  • context (Hash)

    Serialization context

  • many (Boolean) (defaults to: nil)

    is object is enumerable

  • opts (Hash)

    Any custom options

Returns:



21
22
23
24
25
26
27
# File 'lib/serega/object_serializer.rb', line 21

def initialize(context:, plan:, many: nil, **opts)
  @context = context
  @plan = plan
  @many = many
  @opts = opts
  @batch_loaders = opts[:batch_loaders]
end

#serialize(object) ⇒ Hash, ...

Serializes object(s)

Parameters:

  • object (Object)

    Serialized object

Returns:

  • (Hash, Array<Hash>, nil)

    Serialized object(s)



34
35
36
37
38
# File 'lib/serega/object_serializer.rb', line 34

def serialize(object)
  return if object.nil?

  array?(object, many) ? serialize_array(object) : serialize_object(object)
end