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.
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.
17 18 19 |
# File 'lib/serega/object_serializer.rb', line 17 def context @context end |
#level_queue ⇒ Object (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 |
#many ⇒ Object (readonly)
Returns the value of attribute many.
17 18 19 |
# File 'lib/serega/object_serializer.rb', line 17 def many @many end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
17 18 19 |
# File 'lib/serega/object_serializer.rb', line 17 def opts @opts end |
#plan ⇒ Object (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.
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.
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.
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 |