Module: Serega::SeregaObjectSerializer::InstanceMethods
- Included in:
- Serega::SeregaObjectSerializer
- Defined in:
- lib/serega/object_serializer.rb
Overview
SeregaObjectSerializer instance methods
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#level_queue ⇒ Object
readonly
Returns the value of attribute level_queue.
-
#many ⇒ Object
readonly
Returns the value of attribute many.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#plan ⇒ Object
readonly
Returns the value of attribute plan.
-
#presenter ⇒ Object
readonly
Returns the value of attribute presenter.
Instance Method Summary collapse
-
#initialize(context:, plan:, many: nil, **opts) ⇒ SeregaObjectSerializer
New SeregaObjectSerializer.
-
#process(level) ⇒ void
Resolves every attribute of one level onto its containers.
-
#serialize(object) ⇒ Hash, ...
Enqueues this level and returns its result container(s).
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
19 20 21 |
# File 'lib/serega/object_serializer.rb', line 19 def context @context end |
#level_queue ⇒ Object (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 |
#many ⇒ Object (readonly)
Returns the value of attribute many.
19 20 21 |
# File 'lib/serega/object_serializer.rb', line 19 def many @many end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
19 20 21 |
# File 'lib/serega/object_serializer.rb', line 19 def opts @opts end |
#plan ⇒ Object (readonly)
Returns the value of attribute plan.
19 20 21 |
# File 'lib/serega/object_serializer.rb', line 19 def plan @plan end |
#presenter ⇒ Object (readonly)
Returns the value of attribute presenter.
19 20 21 |
# File 'lib/serega/object_serializer.rb', line 19 def presenter @presenter end |
Instance Method Details
#initialize(context:, plan:, many: nil, **opts) ⇒ SeregaObjectSerializer
Returns New SeregaObjectSerializer.
27 28 29 30 31 32 33 34 35 |
# 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] # Looked up once here and reused for every enqueued chunk of the level. @presenter = self.class.serializer_class.presenter 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.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/serega/object_serializer.rb', line 59 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.
43 44 45 46 47 48 49 50 51 |
# File 'lib/serega/object_serializer.rb', line 43 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 |