serega 0.50.0 → 0.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/lib/serega/attribute/serializer_reference.rb +27 -0
  4. data/lib/serega/attribute/value_code.rb +63 -0
  5. data/lib/serega/attribute.rb +80 -102
  6. data/lib/serega/attribute_normalizer.rb +43 -17
  7. data/lib/serega/attribute_normalizers/preloads.rb +5 -4
  8. data/lib/serega/attribute_value_resolvers/base.rb +35 -0
  9. data/lib/serega/attribute_value_resolvers/batch.rb +14 -1
  10. data/lib/serega/attribute_value_resolvers/callable.rb +52 -0
  11. data/lib/serega/attribute_value_resolvers/const.rb +11 -1
  12. data/lib/serega/attribute_value_resolvers/delegate.rb +12 -2
  13. data/lib/serega/attribute_value_resolvers/hash_access.rb +19 -4
  14. data/lib/serega/attribute_value_resolvers/keyword.rb +10 -3
  15. data/lib/serega/attribute_values.rb +4 -18
  16. data/lib/serega/conditions.rb +26 -1
  17. data/lib/serega/config.rb +45 -6
  18. data/lib/serega/engine/pulls.rb +3 -16
  19. data/lib/serega/engine/run.rb +4 -9
  20. data/lib/serega/errors.rb +26 -2
  21. data/lib/serega/formatters/callable.rb +44 -0
  22. data/lib/serega/formatters/method.rb +55 -0
  23. data/lib/serega/formatters.rb +27 -0
  24. data/lib/serega/plan/cache.rb +83 -60
  25. data/lib/serega/plan/depth_limit.rb +36 -0
  26. data/lib/serega/plan/per_ractor_cache.rb +11 -6
  27. data/lib/serega/plan/point.rb +67 -0
  28. data/lib/serega/plan/presets.rb +116 -0
  29. data/lib/serega/plan/selection.rb +94 -0
  30. data/lib/serega/plan/selection_planner.rb +50 -0
  31. data/lib/serega/plan.rb +41 -55
  32. data/lib/serega/plugins/metadata/meta_attribute.rb +17 -1
  33. data/lib/serega/plugins/metadata/metadata.rb +1 -10
  34. data/lib/serega/plugins/root/root.rb +11 -8
  35. data/lib/serega/presenter.rb +20 -0
  36. data/lib/serega/result_builder.rb +1 -1
  37. data/lib/serega/result_code/attribute_point_code.rb +117 -0
  38. data/lib/serega/result_code/point_code.rb +54 -0
  39. data/lib/serega/result_code/relation_point_code.rb +112 -0
  40. data/lib/serega/result_code.rb +41 -85
  41. data/lib/serega/source_group.rb +36 -67
  42. data/lib/serega/utils/camel_case.rb +22 -0
  43. data/lib/serega/utils/collection_detector.rb +3 -2
  44. data/lib/serega/utils/{parse_string_modifiers.rb → parse_string_selection.rb} +2 -2
  45. data/lib/serega/utils/ruby_literal.rb +28 -0
  46. data/lib/serega/validations/attribute/check_opt_preset.rb +34 -0
  47. data/lib/serega/validations/attribute/check_opt_selection.rb +41 -0
  48. data/lib/serega/validations/check_attribute_params.rb +4 -4
  49. data/lib/serega/validations/check_definition_selection.rb +21 -0
  50. data/lib/serega/validations/check_preset_params.rb +103 -0
  51. data/lib/serega/validations/check_relation_selection.rb +31 -0
  52. data/lib/serega/validations/check_selection.rb +89 -0
  53. data/lib/serega.rb +94 -30
  54. data/sig/serega.rbs +23 -7
  55. metadata +25 -5
  56. data/lib/serega/formatter.rb +0 -98
  57. data/lib/serega/plan_point.rb +0 -58
  58. data/lib/serega/validations/initiate/check_modifiers.rb +0 -87
data/lib/serega/plan.rb CHANGED
@@ -22,25 +22,34 @@ class Serega
22
22
  attr_reader :collection_classes
23
23
 
24
24
  # Presenter class that wraps the sources in the Ractor of the plan, `nil`
25
- # without presenter methods
25
+ # when no point reads the sources through the presenter
26
26
  attr_reader :presenter
27
27
 
28
- # Instantiate new serialization plan. Raises DepthLimitError when the plan
29
- # nests deeper than the depth limit of the root serializer.
30
- def initialize(serializer_class, parent_plan_point, modifiers)
31
- check_depth_limit(parent_plan_point) if parent_plan_point
28
+ # Levels of child plans the depth limit of the root serializer allows
29
+ # below this plan, `nil` without a limit. See DepthLimit.
30
+ attr_reader :depth_left
31
+
32
+ # Instantiate new serialization plan of the selected attributes of the
33
+ # selection's serializer, with the result builder class of the
34
+ # selection. A child plan gets the point of its parent plan. Raises
35
+ # DepthLimitError when the plan nests deeper than the depth limit of the
36
+ # root serializer.
37
+ def initialize(selection, parent_plan_point = nil)
38
+ serializer_class = selection.serializer_class
32
39
  serializer_class.lock
33
40
  @serializer_class = serializer_class
34
41
  @parent_plan_point = parent_plan_point
35
- @presenter = serializer_class.local_presenter
36
- @points = attributes_points(modifiers)
42
+ @depth_left = DepthLimit.depth_left(serializer_class, parent_plan_point)
43
+ @builder_class = selection.builder_class
44
+ @points = selected_points(selection)
45
+ @presenter = points_presenter(serializer_class)
37
46
  @relation_points = points.select(&:child_plan).freeze
38
47
  @preload_points = points.select(&:preloads).freeze
39
48
  @batch_points = points.any? { |point| !point.batch_loaders.empty? }
40
49
  @flat = relation_points.empty? && preload_points.empty? && !@batch_points && !@presenter
41
50
  @result_builders = {}
42
51
  @collection_classes = {}.compare_by_identity
43
- direct = parent_plan_point.nil? && @flat && !serializer_class.prepare_initial_objects_signature
52
+ direct = parent_plan_point.nil? && @flat && !serializer_class.prepare_initial_objects_signature && @builder_class.equal?(SeregaResultBuilder)
44
53
  @direct_hash_builder = direct ? result_builder(:hash) : nil
45
54
  # :nocov:
46
55
  rescue *RACTOR_ISOLATION_ERRORS
@@ -54,7 +63,8 @@ class Serega
54
63
  attr_reader :preload_points
55
64
 
56
65
  # Hash result builder of a flat root plan whose serializer prepares no
57
- # initial objects. `Serega#call` builds one source with it directly.
66
+ # initial objects and whose builder class is SeregaResultBuilder.
67
+ # `Serega#call` builds one source with it directly.
58
68
  attr_reader :direct_hash_builder
59
69
 
60
70
  # Whether any point uses batch loaders
@@ -68,62 +78,38 @@ class Serega
68
78
  @flat
69
79
  end
70
80
 
71
- # Result builder of this plan in the serialization mode
81
+ # Plan that starts the serialization: this plan, or the root plan of
82
+ # its parent point
83
+ def root_plan
84
+ plan = self
85
+ while (point = plan.parent_plan_point)
86
+ plan = point.plan
87
+ end
88
+ plan
89
+ end
90
+
91
+ # Result builder of this plan in the serialization mode, made with the
92
+ # builder class of the selection
72
93
  def result_builder(mode)
73
- @result_builders[mode] ||= SeregaResultBuilder.new(serializer_class, mode, points)
94
+ @result_builders[mode] ||= @builder_class.new(serializer_class, mode, points)
74
95
  end
75
96
 
76
97
  private
77
98
 
78
- def check_depth_limit(parent_plan_point)
79
- depth = 2
80
- root_plan = parent_plan_point.plan
81
- while (point = root_plan.parent_plan_point)
82
- depth += 1
83
- root_plan = point.plan
84
- end
85
-
86
- root_serializer = root_plan.serializer_class
87
- limit = root_serializer.config.depth_limit
88
- return if limit.nil? || depth <= limit
89
-
90
- names = [parent_plan_point.name]
91
- point = parent_plan_point
92
- names.unshift(point.name) while (point = point.plan.parent_plan_point)
93
- raise DepthLimitError.new("Depth limit was exceeded", "#{root_serializer} (depth limit: #{limit}) -> #{names.join(" -> ")}")
99
+ # Presenter class of the Ractor, when a point reads the sources through it
100
+ def points_presenter(serializer_class)
101
+ presenter = serializer_class.local_presenter
102
+ presenter if presenter && points.any? { |point| point.attribute.reads_presenter? }
94
103
  end
95
104
 
96
- def attributes_points(modifiers)
97
- only = modifiers[:only] || FROZEN_EMPTY_HASH
98
- except = modifiers[:except] || FROZEN_EMPTY_HASH
99
- with = modifiers[:with] || FROZEN_EMPTY_HASH
100
- only_limits = only_limits?(only, except, with)
101
- points = []
102
-
103
- serializer_class.attributes.each_value do |attribute|
104
- next unless attribute.visible?(only, except, with, only_limits)
105
-
106
- child_fields =
107
- if attribute.relation?
108
- name = attribute.name
109
- {only: only[name], with: with[name], except: except[name]}
110
- end
111
-
112
- point = SeregaPlanPoint.new(self, attribute, child_fields)
113
- points << point.freeze
105
+ # Makes a point of each selected attribute, with the preloads and child
106
+ # selection the selection gives it
107
+ def selected_points(selection)
108
+ points = selection.attributes.map do |attribute|
109
+ Point.new(self, attribute, selection.preloads(attribute), selection.child(attribute)).freeze
114
110
  end
115
111
 
116
112
  points.freeze
117
113
  end
118
-
119
- # `only` hides the fields it does not name when it names a field of
120
- # this level without nested fields, or when `except` hides no field and
121
- # `with` adds no field of this level
122
- def only_limits?(only, except, with)
123
- return false if only.empty?
124
- return true if only.each_value.any?(&:empty?)
125
-
126
- with.empty? && except.each_value.none?(&:empty?)
127
- end
128
114
  end
129
115
  end
@@ -27,6 +27,8 @@ class Serega
27
27
  @path = SeregaUtils::EnumDeepDup.call(path)
28
28
  @opts = SeregaUtils::EnumDeepDup.call(opts)
29
29
  @block = block
30
+ @hide_nil = opts[:hide_nil] == true
31
+ @hide_empty = opts[:hide_empty] == true
30
32
  @normalized_block = normalize_block(opts[:value], opts[:const], block)
31
33
  @normalized_block_signature =
32
34
  SeregaUtils::MethodSignature.call(@normalized_block, pos_limit: 2, keyword_args: [])
@@ -41,8 +43,22 @@ class Serega
41
43
  end
42
44
  end
43
45
 
46
+ # Whether the value is hidden: `hide_nil` hides nil, `hide_empty`
47
+ # hides nil and empty values
44
48
  def hide?(value)
45
- (!!opts[:hide_nil] && value.nil?) || (!!opts[:hide_empty] && (value.nil? || (value.respond_to?(:empty?) && value.empty?)))
49
+ return @hide_nil || @hide_empty if value.nil?
50
+
51
+ @hide_empty && value.respond_to?(:empty?) && value.empty?
52
+ end
53
+
54
+ # Value nested under the path, for example `{foo: {bar: value}}` for
55
+ # the path `[:foo, :bar]`, or nil for a hidden value
56
+ def nested_value(object, context)
57
+ value = value(object, context)
58
+ return if hide?(value)
59
+
60
+ path.reverse_each { |key| value = {key => value} }
61
+ value
46
62
  end
47
63
 
48
64
  private
@@ -85,7 +85,7 @@ class Serega
85
85
 
86
86
  def add_metadata(object, context, hash)
87
87
  self.class.meta_attributes.each_value do |meta_attribute|
88
- metadata = meta_attribute_value(object, context, meta_attribute)
88
+ metadata = meta_attribute.nested_value(object, context)
89
89
  next unless metadata
90
90
 
91
91
  SeregaUtils::DeepMerge.call(hash, metadata)
@@ -96,15 +96,6 @@ class Serega
96
96
  MESSAGE
97
97
  end
98
98
  end
99
-
100
- def meta_attribute_value(object, context, meta_attribute)
101
- value = meta_attribute.value(object, context)
102
- return if meta_attribute.hide?(value)
103
-
104
- # Example: path [:foo, :bar] and value :bazz give { foo: { bar: :bazz } }
105
- meta_attribute.path.reverse_each { |key| value = {key => value} }
106
- value
107
- end
108
99
  end
109
100
  end
110
101
  end
@@ -38,23 +38,23 @@ class Serega
38
38
 
39
39
  # Class methods the plugin adds to serializers
40
40
  module ClassMethods
41
- def call(object, only: nil, with: nil, except: nil, check_initiate_params: nil, context: nil, many: nil, **opts) # :nodoc:
42
- new(only: only, with: with, except: except, check_initiate_params: check_initiate_params)
41
+ def call(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil, **opts) # :nodoc:
42
+ new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
43
43
  .to_h(object, context: context, many: many, **opts)
44
44
  end
45
45
 
46
- def to_data(object, only: nil, with: nil, except: nil, check_initiate_params: nil, context: nil, many: nil, **opts) # :nodoc:
47
- new(only: only, with: with, except: except, check_initiate_params: check_initiate_params)
46
+ def to_data(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil, **opts) # :nodoc:
47
+ new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
48
48
  .to_data(object, context: context, many: many, **opts)
49
49
  end
50
50
 
51
- def to_struct(object, only: nil, with: nil, except: nil, check_initiate_params: nil, context: nil, many: nil, **opts) # :nodoc:
52
- new(only: only, with: with, except: except, check_initiate_params: check_initiate_params)
51
+ def to_struct(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil, **opts) # :nodoc:
52
+ new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
53
53
  .to_struct(object, context: context, many: many, **opts)
54
54
  end
55
55
 
56
- def to_json(object, only: nil, with: nil, except: nil, check_initiate_params: nil, context: nil, many: nil, **opts) # :nodoc:
57
- new(only: only, with: with, except: except, check_initiate_params: check_initiate_params)
56
+ def to_json(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil, **opts) # :nodoc:
57
+ new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
58
58
  .to_json(object, context: context, many: many, **opts)
59
59
  end
60
60
 
@@ -167,6 +167,9 @@ class Serega
167
167
  result
168
168
  end
169
169
 
170
+ # Root key of the serialization, nil without a root. Read by the
171
+ # plugins :metadata and :context_metadata, which add their data only
172
+ # under a root.
170
173
  def build_root(opts, many)
171
174
  return opts[:root] if opts.key?(:root)
172
175
 
@@ -76,6 +76,26 @@ class Serega
76
76
  end
77
77
  end
78
78
 
79
+ # Whether the presenter class answers one of the method names itself:
80
+ # it defines the method, not as a delegator to the wrapped object, or
81
+ # it defines its own `method_missing`, which can answer any name
82
+ def self.presents_any?(names)
83
+ return false if names.empty?
84
+ return true unless instance_method(:method_missing).owner.equal?(SeregaPresenter)
85
+
86
+ names.any? { |name| presents?(name) }
87
+ end
88
+
89
+ # Whether the presenter class defines the method itself, not as a
90
+ # delegator to the wrapped object
91
+ def self.presents?(name)
92
+ return false unless method_defined?(name) || private_method_defined?(name)
93
+
94
+ owner = instance_method(name).owner
95
+ !(owner.instance_of?(Module) && owner.include?(CalleeDelegator))
96
+ end
97
+ private_class_method :presents?
98
+
79
99
  # Defines delegators for the method names that the presenter class does
80
100
  # not define itself, thus presenter methods stay in front of them
81
101
  def self.define_delegators(names)
@@ -31,7 +31,7 @@ class Serega
31
31
  @result_class = result_class(serializer_class)
32
32
  # The generated code reads the `:const`, `:default` and formatter values from its constants
33
33
  singleton_class.const_set(:SeregaAttributeValues, serializer_class::SeregaAttributeValues)
34
- singleton_class.class_eval(SeregaResultCode.new(mode, points).to_s, SeregaUtils::SerializedAttributeError::GENERATED_CODE_FILE, 1)
34
+ singleton_class.class_eval(SeregaResultCode.new(mode, points, compact: serializer_class.config.compact).to_s, SeregaUtils::SerializedAttributeError::GENERATED_CODE_FILE, 1)
35
35
  end
36
36
 
37
37
  private
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ class SeregaResultCode # :nodoc:
5
+ #
6
+ # Code of a point without a child plan. A plain attribute reads its value
7
+ # with the SeregaAttribute::ValueCode#plain of its attribute, an
8
+ # attribute with a `:value` callable calls it with the
9
+ # SeregaAttribute::ValueCode#callable, a :batch attribute reads its
10
+ # loaded batch with the SeregaAttribute::ValueCode#batch, other
11
+ # attributes call SeregaAttribute#value.
12
+ #
13
+ # A relation reads its relation source with this code too, without the
14
+ # value conditions: they check the serialized relation value later.
15
+ #
16
+ class AttributePointCode < PointCode # :nodoc:
17
+ def initialize(point, index, check_value: true)
18
+ super(point, index)
19
+ @check_value = check_value
20
+ value_code = point.attribute.value_code
21
+ @batch_code = value_code.batch("batch_#{index}", "SeregaAttributeValues::")
22
+ @batches_variable = (point.batch_loaders.empty? || @batch_code) ? nil : "batches_#{index}"
23
+ @callable_code = value_code.callable("callable_#{index}", @batches_variable || "nil", "SeregaAttributeValues::")
24
+ @read_code = value_code.plain("SeregaAttributeValues::") || @callable_code || @batch_code
25
+ end
26
+
27
+ # Code that sets the local variables that the value code reads
28
+ def variables
29
+ index = @index
30
+ variables = [value_variable]
31
+ variables << "#{@batches_variable} = batches[#{index}]" if @batches_variable
32
+ variables << "conditions_#{index} = @points[#{index}].attribute.conditions" if conditional?
33
+ variables.compact
34
+ end
35
+
36
+ # Whether the value can be SKIP. Without the value check, only the
37
+ # :if and :unless conditions give SKIP.
38
+ def conditional?
39
+ return super if @check_value
40
+
41
+ conditions = @point.attribute.conditions
42
+ conditions ? conditions.object_conditions? : false
43
+ end
44
+
45
+ # Code that assigns the value of the source to `value_N`. A
46
+ # conditional attribute gets SKIP when it fails its conditions.
47
+ def assign_value
48
+ index = @index
49
+ read_code = rescued_read_code
50
+ return "value_#{index} =\n#{read_code.gsub(/^/, " ")}" unless conditional?
51
+ return conditional_read_code(read_code.gsub(/^/, " ")) unless @check_value
52
+
53
+ conditional_read_code(<<~RUBY.chomp)
54
+ value_#{index} =
55
+ #{read_code.gsub(/^/, " ")}
56
+ conditions_#{index}.satisfy_value?(value_#{index}, context) ? value_#{index} : Serega::SeregaEngine::SKIP
57
+ RUBY
58
+ end
59
+
60
+ # An attribute without batch loaders reads one source alone, thus
61
+ # `#call_one` can read it
62
+ def call_one?
63
+ @point.batch_loaders.empty?
64
+ end
65
+
66
+ private
67
+
68
+ # Code that gives the value of `read_code` when the source passes the
69
+ # :if and :unless conditions, and SKIP otherwise
70
+ def conditional_read_code(read_code)
71
+ <<~RUBY.chomp
72
+ value_#{@index} =
73
+ if conditions_#{@index}.satisfy?(source, context)
74
+ #{read_code}
75
+ else
76
+ Serega::SeregaEngine::SKIP
77
+ end
78
+ RUBY
79
+ end
80
+
81
+ # Code that sets the variable that reads the value: the callable, the
82
+ # loaded batch of the only loader, or the attribute. nil for a plain
83
+ # attribute, which reads the source alone.
84
+ def value_variable
85
+ index = @index
86
+ return "callable_#{index} = @points[#{index}].attribute.value_callable" if @callable_code
87
+ return "batch_#{index} = batches[#{index}].fetch(#{@point.batch_loaders.first.inspect})" if @batch_code
88
+
89
+ "attribute_#{index} = @points[#{index}].attribute" unless @read_code
90
+ end
91
+
92
+ # Code that reads the value. An error raised there gets the attribute
93
+ # name. Only the rescue reads the point.
94
+ def rescued_read_code
95
+ <<~RUBY.chomp
96
+ begin
97
+ #{read_value_code}
98
+ rescue => error
99
+ Serega::SeregaUtils::SerializedAttributeError.call(error, @points[#{@index}])
100
+ end
101
+ RUBY
102
+ end
103
+
104
+ # Code that reads the value: the code of the plain value, the
105
+ # callable or the loaded batch, or a SeregaAttribute#value call. The
106
+ # code reads the `:const`, `:default` and formatter values from the
107
+ # constants of SeregaAttributeValues.
108
+ def read_value_code
109
+ read_code = @read_code
110
+ return read_code if read_code
111
+ return "attribute_#{@index}.value(source, context)" unless @batches_variable
112
+
113
+ "attribute_#{@index}.value(source, context, batches: #{@batches_variable})"
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ class SeregaResultCode # :nodoc:
5
+ #
6
+ # Code of one point in the generated methods. The local variables of the
7
+ # point have its index in their names, for example `value_1`.
8
+ #
9
+ class PointCode # :nodoc:
10
+ def initialize(point, index)
11
+ @point = point
12
+ @index = index
13
+ end
14
+
15
+ # Whether the value of the point can be SKIP
16
+ def conditional?
17
+ @point.conditional?
18
+ end
19
+
20
+ # Pair of the serialized Hash literal
21
+ def hash_pair
22
+ "#{key_code} => value_#{@index}"
23
+ end
24
+
25
+ # Code that adds the value to the serialized Hash. It adds no key for a
26
+ # skipped value, and with `compact` for a `nil` value.
27
+ def hash_assign(compact)
28
+ index = @index
29
+ code = "serialized_hash[#{key_code}] = value_#{index}"
30
+ skips = []
31
+ skips << "value_#{index}.nil?" if compact
32
+ skips << "Serega::SeregaEngine::SKIP.equal?(value_#{index})" if conditional?
33
+ skips.empty? ? code : "#{code} unless #{skips.join(" || ")}"
34
+ end
35
+
36
+ # Code of the Struct or Data argument with the value, nil for a skipped
37
+ # value
38
+ def argument
39
+ return "value_#{@index}" unless conditional?
40
+
41
+ "(Serega::SeregaEngine::SKIP.equal?(value_#{@index}) ? nil : value_#{@index})"
42
+ end
43
+
44
+ private
45
+
46
+ # Code of the key. A String key is a deduplicated frozen literal, thus
47
+ # adding it to a Hash allocates no copy.
48
+ def key_code
49
+ key = @point.key
50
+ key.is_a?(String) ? "-#{key.inspect}" : key.inspect
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ class SeregaResultCode # :nodoc:
5
+ #
6
+ # Code of a point with a child plan. Its values are the relation values
7
+ # of all sources, built by the child group. A relation value skipped by
8
+ # the :if and :unless conditions is SKIP already. The :if_value and
9
+ # :unless_value conditions check the serialized relation value.
10
+ #
11
+ # The point also writes its loop of the `#collect` method: it reads the
12
+ # relation source of each source with the code of an AttributePointCode,
13
+ # appends the sources of the relation source to `child_sources_N`, and
14
+ # writes the pull of each source to `pulls_N`. See SeregaEngine::Pulls.
15
+ #
16
+ class RelationPointCode < PointCode # :nodoc:
17
+ def initialize(point, index)
18
+ super
19
+ @source_code = AttributePointCode.new(point, index, check_value: false)
20
+ conditions = point.attribute.conditions
21
+ @check_value = conditions ? conditions.value_conditions? : false
22
+ end
23
+
24
+ # Code that sets the local variables of the relation values and the
25
+ # value conditions
26
+ def variables
27
+ index = @index
28
+ variables = ["relation_values_#{index} = relations[@points[#{index}]]"]
29
+ variables << "conditions_#{index} = @points[#{index}].attribute.conditions" if @check_value
30
+ variables
31
+ end
32
+
33
+ # Code that assigns the relation value of the source to `value_N`. A
34
+ # value that fails the value conditions gets SKIP.
35
+ def assign_value
36
+ index = @index
37
+ code = "value_#{index} = relation_values_#{index}[source_index]"
38
+ return code unless @check_value
39
+
40
+ "#{code}\n" \
41
+ "value_#{index} = Serega::SeregaEngine::SKIP unless Serega::SeregaEngine::SKIP.equal?(value_#{index}) || " \
42
+ "conditions_#{index}.satisfy_value?(value_#{index}, context)"
43
+ end
44
+
45
+ # A relation needs the source group, thus no `#call_one`
46
+ def call_one?
47
+ false
48
+ end
49
+
50
+ # Code of the `#collect` loop of the point
51
+ def collect
52
+ index = @index
53
+ variables = [*@source_code.variables, "child_sources_#{index} = []"]
54
+ variables << "collection_classes_#{index} = @points[#{index}].collection_classes" unless @point.many == false
55
+ <<~RUBY.chomp
56
+ #{variables.join("\n")}
57
+ pulls_#{index} = Array.new(size)
58
+ source_index = 0
59
+ while source_index < size
60
+ source = sources[source_index]
61
+ #{@source_code.assign_value.gsub("\n", "\n ")}
62
+ pulls_#{index}[source_index] =
63
+ #{pull_code.gsub("\n", "\n ")}
64
+ source_index += 1
65
+ end
66
+ RUBY
67
+ end
68
+
69
+ # Variables that `#collect` returns for the point
70
+ def collected
71
+ "child_sources_#{@index}, pulls_#{@index}"
72
+ end
73
+
74
+ private
75
+
76
+ # Code that appends the sources of `value_N` to `child_sources_N` and
77
+ # gives its pull, as SeregaEngine::Pulls.append does
78
+ def pull_code
79
+ index = @index
80
+ <<~RUBY.chomp
81
+ #{skip_branch}if value_#{index}.nil?
82
+ nil
83
+ #{collection_branch}else
84
+ child_sources_#{index} << value_#{index}
85
+ #{@point.many ? "1" : "Serega::SeregaEngine::Pulls::SINGLE_SOURCE"}
86
+ end
87
+ RUBY
88
+ end
89
+
90
+ # First branch of the pull code of a conditional relation: SKIP is the
91
+ # pull of a skipped relation source
92
+ def skip_branch
93
+ "if Serega::SeregaEngine::SKIP.equal?(value_#{@index})\n value_#{@index}\nels" if @source_code.conditional?
94
+ end
95
+
96
+ # Branch of the pull code that appends a collection, unless the
97
+ # relation has `many: false`. An Array is checked inline, without the
98
+ # call of SeregaUtils::CollectionDetector.
99
+ def collection_branch
100
+ return if @point.many == false
101
+
102
+ index = @index
103
+ <<~RUBY
104
+ elsif value_#{index}.instance_of?(Array) || Serega::SeregaUtils::CollectionDetector.call(value_#{index}, collection_classes_#{index})
105
+ collection = value_#{index}.to_a
106
+ child_sources_#{index}.concat(collection)
107
+ collection.size
108
+ RUBY
109
+ end
110
+ end
111
+ end
112
+ end