serega 0.50.0 → 0.51.1
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.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/serega/attribute/serializer_reference.rb +27 -0
- data/lib/serega/attribute/value_code.rb +63 -0
- data/lib/serega/attribute.rb +80 -102
- data/lib/serega/attribute_normalizer.rb +43 -17
- data/lib/serega/attribute_normalizers/preloads.rb +5 -4
- data/lib/serega/attribute_value_resolvers/base.rb +35 -0
- data/lib/serega/attribute_value_resolvers/batch.rb +14 -1
- data/lib/serega/attribute_value_resolvers/callable.rb +52 -0
- data/lib/serega/attribute_value_resolvers/const.rb +11 -1
- data/lib/serega/attribute_value_resolvers/delegate.rb +12 -2
- data/lib/serega/attribute_value_resolvers/hash_access.rb +19 -4
- data/lib/serega/attribute_value_resolvers/keyword.rb +10 -3
- data/lib/serega/attribute_values.rb +4 -18
- data/lib/serega/conditions.rb +26 -1
- data/lib/serega/config.rb +45 -6
- data/lib/serega/engine/pulls.rb +3 -16
- data/lib/serega/engine/run.rb +4 -9
- data/lib/serega/errors.rb +28 -3
- data/lib/serega/formatters/callable.rb +44 -0
- data/lib/serega/formatters/method.rb +55 -0
- data/lib/serega/formatters.rb +27 -0
- data/lib/serega/plan/cache.rb +83 -60
- data/lib/serega/plan/depth_limit.rb +55 -0
- data/lib/serega/plan/per_ractor_cache.rb +11 -6
- data/lib/serega/plan/point.rb +67 -0
- data/lib/serega/plan/presets.rb +116 -0
- data/lib/serega/plan/selection.rb +104 -0
- data/lib/serega/plan/selection_planner.rb +50 -0
- data/lib/serega/plan.rb +45 -55
- data/lib/serega/plugins/metadata/meta_attribute.rb +17 -1
- data/lib/serega/plugins/metadata/metadata.rb +1 -10
- data/lib/serega/plugins/root/root.rb +11 -8
- data/lib/serega/presenter.rb +20 -0
- data/lib/serega/result_builder.rb +1 -1
- data/lib/serega/result_code/attribute_point_code.rb +117 -0
- data/lib/serega/result_code/point_code.rb +54 -0
- data/lib/serega/result_code/relation_point_code.rb +112 -0
- data/lib/serega/result_code.rb +41 -85
- data/lib/serega/source_group.rb +37 -68
- data/lib/serega/utils/camel_case.rb +22 -0
- data/lib/serega/utils/collection_detector.rb +3 -2
- data/lib/serega/utils/{parse_string_modifiers.rb → parse_string_selection.rb} +2 -2
- data/lib/serega/utils/ruby_literal.rb +28 -0
- data/lib/serega/validations/attribute/check_opt_preset.rb +34 -0
- data/lib/serega/validations/attribute/check_opt_selection.rb +41 -0
- data/lib/serega/validations/check_attribute_params.rb +4 -4
- data/lib/serega/validations/check_definition_selection.rb +21 -0
- data/lib/serega/validations/check_preset_params.rb +103 -0
- data/lib/serega/validations/check_relation_selection.rb +31 -0
- data/lib/serega/validations/check_selection.rb +89 -0
- data/lib/serega.rb +118 -50
- data/sig/serega.rbs +23 -7
- metadata +32 -10
- data/lib/serega/formatter.rb +0 -98
- data/lib/serega/plan_point.rb +0 -58
- data/lib/serega/validations/initiate/check_modifiers.rb +0 -87
data/lib/serega/plan.rb
CHANGED
|
@@ -9,6 +9,9 @@ class Serega
|
|
|
9
9
|
# Serializer of the plan
|
|
10
10
|
attr_reader :serializer_class
|
|
11
11
|
|
|
12
|
+
# Selection of the attributes of the plan
|
|
13
|
+
attr_reader :selection
|
|
14
|
+
|
|
12
15
|
# Parent plan point
|
|
13
16
|
attr_reader :parent_plan_point
|
|
14
17
|
|
|
@@ -22,25 +25,35 @@ class Serega
|
|
|
22
25
|
attr_reader :collection_classes
|
|
23
26
|
|
|
24
27
|
# Presenter class that wraps the sources in the Ractor of the plan, `nil`
|
|
25
|
-
#
|
|
28
|
+
# when no point reads the sources through the presenter
|
|
26
29
|
attr_reader :presenter
|
|
27
30
|
|
|
28
|
-
#
|
|
29
|
-
#
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
# Levels of child plans the depth limit of the root serializer allows
|
|
32
|
+
# below this plan, `nil` without a limit. See DepthLimit.
|
|
33
|
+
attr_reader :depth_left
|
|
34
|
+
|
|
35
|
+
# Instantiate new serialization plan of the selected attributes of the
|
|
36
|
+
# selection's serializer, with the result builder class of the
|
|
37
|
+
# selection. A child plan gets the point of its parent plan. Raises
|
|
38
|
+
# DepthLimitError when the plan nests deeper than the depth limit of the
|
|
39
|
+
# root serializer, or repeats a parent plan without a depth limit.
|
|
40
|
+
def initialize(selection, parent_plan_point = nil)
|
|
41
|
+
serializer_class = selection.serializer_class
|
|
32
42
|
serializer_class.lock
|
|
33
43
|
@serializer_class = serializer_class
|
|
44
|
+
@selection = selection
|
|
34
45
|
@parent_plan_point = parent_plan_point
|
|
35
|
-
@
|
|
36
|
-
@
|
|
46
|
+
@depth_left = DepthLimit.depth_left(selection, parent_plan_point)
|
|
47
|
+
@builder_class = selection.builder_class
|
|
48
|
+
@points = selected_points(selection)
|
|
49
|
+
@presenter = points_presenter(serializer_class)
|
|
37
50
|
@relation_points = points.select(&:child_plan).freeze
|
|
38
51
|
@preload_points = points.select(&:preloads).freeze
|
|
39
52
|
@batch_points = points.any? { |point| !point.batch_loaders.empty? }
|
|
40
53
|
@flat = relation_points.empty? && preload_points.empty? && !@batch_points && !@presenter
|
|
41
54
|
@result_builders = {}
|
|
42
55
|
@collection_classes = {}.compare_by_identity
|
|
43
|
-
direct = parent_plan_point.nil? && @flat && !serializer_class.prepare_initial_objects_signature
|
|
56
|
+
direct = parent_plan_point.nil? && @flat && !serializer_class.prepare_initial_objects_signature && @builder_class.equal?(SeregaResultBuilder)
|
|
44
57
|
@direct_hash_builder = direct ? result_builder(:hash) : nil
|
|
45
58
|
# :nocov:
|
|
46
59
|
rescue *RACTOR_ISOLATION_ERRORS
|
|
@@ -54,7 +67,8 @@ class Serega
|
|
|
54
67
|
attr_reader :preload_points
|
|
55
68
|
|
|
56
69
|
# Hash result builder of a flat root plan whose serializer prepares no
|
|
57
|
-
# initial objects
|
|
70
|
+
# initial objects and whose builder class is SeregaResultBuilder.
|
|
71
|
+
# `Serega#call` builds one source with it directly.
|
|
58
72
|
attr_reader :direct_hash_builder
|
|
59
73
|
|
|
60
74
|
# Whether any point uses batch loaders
|
|
@@ -68,62 +82,38 @@ class Serega
|
|
|
68
82
|
@flat
|
|
69
83
|
end
|
|
70
84
|
|
|
71
|
-
#
|
|
85
|
+
# Plan that starts the serialization: this plan, or the root plan of
|
|
86
|
+
# its parent point
|
|
87
|
+
def root_plan
|
|
88
|
+
plan = self
|
|
89
|
+
while (point = plan.parent_plan_point)
|
|
90
|
+
plan = point.plan
|
|
91
|
+
end
|
|
92
|
+
plan
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Result builder of this plan in the serialization mode, made with the
|
|
96
|
+
# builder class of the selection
|
|
72
97
|
def result_builder(mode)
|
|
73
|
-
@result_builders[mode] ||=
|
|
98
|
+
@result_builders[mode] ||= @builder_class.new(serializer_class, mode, points)
|
|
74
99
|
end
|
|
75
100
|
|
|
76
101
|
private
|
|
77
102
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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(" -> ")}")
|
|
103
|
+
# Presenter class of the Ractor, when a point reads the sources through it
|
|
104
|
+
def points_presenter(serializer_class)
|
|
105
|
+
presenter = serializer_class.local_presenter
|
|
106
|
+
presenter if presenter && points.any? { |point| point.attribute.reads_presenter? }
|
|
94
107
|
end
|
|
95
108
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
|
109
|
+
# Makes a point of each selected attribute, with the preloads and child
|
|
110
|
+
# selection the selection gives it
|
|
111
|
+
def selected_points(selection)
|
|
112
|
+
points = selection.attributes.map do |attribute|
|
|
113
|
+
Point.new(self, attribute, selection.preloads(attribute), selection.child(attribute)).freeze
|
|
114
114
|
end
|
|
115
115
|
|
|
116
116
|
points.freeze
|
|
117
117
|
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
118
|
end
|
|
129
119
|
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
|
-
|
|
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 =
|
|
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,
|
|
42
|
-
new(only: only, with: with, except: except,
|
|
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,
|
|
47
|
-
new(only: only, with: with, except: except,
|
|
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,
|
|
52
|
-
new(only: only, with: with, except: except,
|
|
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,
|
|
57
|
-
new(only: only, with: with, except: except,
|
|
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
|
|
data/lib/serega/presenter.rb
CHANGED
|
@@ -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
|