Module: Serega::SeregaPlanPoint::InstanceMethods

Included in:
Serega::SeregaPlanPoint
Defined in:
lib/serega/plan_point.rb

Overview

SeregaPlanPoint instance methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributeSeregaAttribute (readonly)

Shows current attribute

Returns:



18
19
20
# File 'lib/serega/plan_point.rb', line 18

def attribute
  @attribute
end

#child_planSeregaPlan? (readonly)

Shows child plan if exists

Returns:

  • (SeregaPlan, nil)

    Attribute serialization plan



22
23
24
# File 'lib/serega/plan_point.rb', line 22

def child_plan
  @child_plan
end

#modifiersHash (readonly)

Child fields to serialize

Returns:

  • (Hash)

    Attributes to serialize



26
27
28
# File 'lib/serega/plan_point.rb', line 26

def modifiers
  @modifiers
end

#planSeregaAttribute (readonly)

Link to current plan this point belongs to

Returns:



14
15
16
# File 'lib/serega/plan_point.rb', line 14

def plan
  @plan
end

Instance Method Details

#batch_loadersObject

Attribute batch_loaders



73
74
75
# File 'lib/serega/plan_point.rb', line 73

def batch_loaders
  attribute.batch_loaders
end

#child_object_serializerClass<SeregaObjectSerializer>

Returns object serializer class for child plan.

Returns:



121
122
123
# File 'lib/serega/plan_point.rb', line 121

def child_object_serializer
  serializer::SeregaObjectSerializer
end

#child_serializer(context:, **opts) ⇒ SeregaObjectSerializer?

Builds the object serializer that serializes this point's relation, or nil when the point has no child plan (a plain attribute). The point owns the static config (child plan, serializer class, many); the caller injects the runtime context and opts.

Parameters:

  • context (Hash)

    serialization context

  • opts (Hash)

    extra object-serializer options (e.g. the level queue)

Returns:



133
134
135
136
137
# File 'lib/serega/plan_point.rb', line 133

def child_serializer(context:, **opts)
  return unless child_plan

  child_object_serializer.new(context: context, plan: child_plan, many: many, **opts)
end

#initialize(plan, attribute, modifiers = nil) ⇒ SeregaPlanPoint

Initializes plan point

Parameters:

  • plan (SeregaPlan)

    Current plan this point belongs to

  • attribute (SeregaAttribute)

    Attribute to construct plan point

  • modifiers (defaults to: nil)

    Serialization parameters

Options Hash (modifiers):

  • :only (Hash)

    The only attributes to serialize

  • :except (Hash)

    Attributes to hide

  • :with (Hash)

    Hidden attributes to serialize additionally

Returns:



40
41
42
43
44
45
# File 'lib/serega/plan_point.rb', line 40

def initialize(plan, attribute, modifiers = nil)
  @plan = plan
  @attribute = attribute
  @modifiers = modifiers
  set_normalized_vars
end

#load_batches(level) ⇒ Hash?

Loads the batch loaders this point's value needs, each once for the whole level, and returns them keyed by loader name for #value to read from.

Parameters:

Returns:

  • (Hash, nil)

    loaded data per loader name, or nil when none are needed



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/serega/plan_point.rb', line 106

def load_batches(level)
  names = batch_loaders
  return if names.empty?

  loaders = self.class.serializer_class.batch_loaders
  names.each_with_object({}) do |name, batches|
    batches[name] = level.fetch(loaders[name])
  end
rescue => error
  SeregaUtils::SerializedAttributeError.call(error, self)
end

#manyObject

Attribute many option



61
62
63
# File 'lib/serega/plan_point.rb', line 61

def many
  attribute.many
end

#nameObject

Attribute name



55
56
57
# File 'lib/serega/plan_point.rb', line 55

def name
  attribute.name
end

#preloadsObject

Attribute preloads



79
80
81
# File 'lib/serega/plan_point.rb', line 79

def preloads
  attribute.preloads
end

#run_preloads(objects) ⇒ void

This method returns an undefined value.

Runs this point's declared preloads over the given objects using the serializer's registered preload handler.

Parameters:

  • objects (Array)

    objects serialized at this point's level



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/serega/plan_point.rb', line 88

def run_preloads(objects)
  return unless preloads

  handler = self.class.serializer_class.preload_with
  unless handler
    raise SeregaError, "The :preload option requires a preload handler. Register one with `preload_with` (the :activerecord_preloads plugin does this for you)."
  end

  handler.call(objects, preloads)
rescue => error
  SeregaUtils::SerializedAttributeError.call(error, self)
end

#serializerObject

Attribute serializer option



67
68
69
# File 'lib/serega/plan_point.rb', line 67

def serializer
  attribute.serializer
end

#value(obj, ctx, batches: nil) ⇒ Object

Attribute value



49
50
51
# File 'lib/serega/plan_point.rb', line 49

def value(obj, ctx, batches: nil)
  attribute.value(obj, ctx, batches: batches)
end