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.



17
18
19
# File 'lib/serega/object_serializer.rb', line 17

def context
  @context
end

#level_queueObject (readonly)

Returns the value of attribute level_queue.



17
18
19
# File 'lib/serega/object_serializer.rb', line 17

def level_queue
  @level_queue
end

#manyObject (readonly)

Returns the value of attribute many.



17
18
19
# File 'lib/serega/object_serializer.rb', line 17

def many
  @many
end

#optsObject (readonly)

Returns the value of attribute opts.



17
18
19
# File 'lib/serega/object_serializer.rb', line 17

def opts
  @opts
end

#planObject (readonly)

Returns the value of attribute plan.



17
18
19
# File 'lib/serega/object_serializer.rb', line 17

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:



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

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:



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

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)



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

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