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

#contextObject (readonly)

Returns the value of attribute context.



19
20
21
# File 'lib/serega/object_serializer.rb', line 19

def context
  @context
end

#level_queueObject (readonly)

Returns the value of attribute level_queue.



19
20
21
# File 'lib/serega/object_serializer.rb', line 19

def level_queue
  @level_queue
end

#manyObject (readonly)

Returns the value of attribute many.



19
20
21
# File 'lib/serega/object_serializer.rb', line 19

def many
  @many
end

#optsObject (readonly)

Returns the value of attribute opts.



19
20
21
# File 'lib/serega/object_serializer.rb', line 19

def opts
  @opts
end

#planObject (readonly)

Returns the value of attribute plan.



19
20
21
# File 'lib/serega/object_serializer.rb', line 19

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:



27
28
29
30
31
32
33
# File 'lib/serega/object_serializer.rb', line 27

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

#process(level) ⇒ void

This method returns an undefined value.

Resolves every attribute of one level onto its containers. For each point, preloads and batch loaders run once over all of the level's objects; the value is then resolved and assigned per object, in attribute order.

Parameters:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/serega/object_serializer.rb', line 57

def process(level)
  objects = level.objects

  plan.points.each do |point|
    point.run_preloads(objects)
    batches = point.load_batches(level)
    # One child serializer per relation point per level (reused for every object),
    # instead of one per object — child objects are grouped into one child level anyway.
    child_serializer = point.child_serializer(context: context, **opts)

    objects.each_with_index do |object, index|
      resolve_point(object, point, level.containers[index], batches, child_serializer)
    rescue => error
      SeregaUtils::SerializedAttributeError.call(error, point)
    end
  end
end

#serialize(object) ⇒ Hash, ...

Enqueues this level and returns its result container(s). The containers are returned immediately but filled in place once the queue is processed.

Parameters:

  • object (Object)

    Serialized object(s)

Returns:

  • (Hash, Array<Hash>, nil)

    Serialized object(s)



41
42
43
44
45
46
47
48
49
# File 'lib/serega/object_serializer.rb', line 41

def serialize(object)
  return if object.nil?

  case serialize_mode(object)
  when :many then enqueue(object.to_a)
  when :many_for_one then enqueue([object]) # `many` on, but a sole object was given — wrap it, don't raise
  else enqueue([object])[0] # :one
  end
end