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.
- 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 +26 -2
- 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 +36 -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 +94 -0
- data/lib/serega/plan/selection_planner.rb +50 -0
- data/lib/serega/plan.rb +41 -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 +36 -67
- 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 +94 -30
- data/sig/serega.rbs +23 -7
- metadata +25 -5
- 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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6438f2b6fe41afa2e0abbe8797f0fc6a6310ef1054dbf8015bfc1dfd1f2c08e6
|
|
4
|
+
data.tar.gz: 66cb93142c582a79976b34091dc8bf0a032274ec526c77d9384aacf8590f013a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bf3e63bd4060496f23a566464cb2b8b30485c5590aaf1cf2523d9afdd357725adbb0919a0301b2c45d1d4aadc9706c868e7e9001a9200d28b9b6bf97b159485f
|
|
7
|
+
data.tar.gz: 87f6e1149f650de7f4571cc1a10d8b9f59a09af343c52945a242442402bc982ad5feaf1cd9af9b5c40915b1f6b9d1a916cd29e905cb613c9385bf6806f42528c
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
0.51.0
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
class SeregaAttribute # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# The :serializer option of an attribute: a serializer class, the name
|
|
7
|
+
# of a serializer class, or a Proc that returns a serializer class. The
|
|
8
|
+
# name and the Proc let serializers refer to classes defined later.
|
|
9
|
+
#
|
|
10
|
+
class SerializerReference # :nodoc:
|
|
11
|
+
def initialize(serializer)
|
|
12
|
+
@serializer = serializer
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Returns the serializer class. Resolves a name or a Proc once, and
|
|
16
|
+
# keeps the class. A frozen reference, shared between Ractors,
|
|
17
|
+
# resolves it on each call.
|
|
18
|
+
def resolve
|
|
19
|
+
serializer = @serializer
|
|
20
|
+
return serializer if serializer.is_a?(Class) && serializer < Serega
|
|
21
|
+
|
|
22
|
+
serializer = serializer.is_a?(String) ? Object.const_get(serializer, false) : serializer.call
|
|
23
|
+
frozen? ? serializer : (@serializer = serializer)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
class SeregaAttribute # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# Ruby code that reads the value of an attribute from the `source`
|
|
7
|
+
# variable of generated code. The `:const` and `:default` values and
|
|
8
|
+
# the formatters come from the
|
|
9
|
+
# `CONSTANTS`, `DEFAULTS`, `FORMATTERS` and `FORMATTER_ARGS` of
|
|
10
|
+
# SeregaAttributeValues, which `constants_prefix` names.
|
|
11
|
+
#
|
|
12
|
+
class ValueCode # :nodoc:
|
|
13
|
+
def initialize(name, value_block, default, formatter)
|
|
14
|
+
@name = name
|
|
15
|
+
@value_block = value_block
|
|
16
|
+
@default = default
|
|
17
|
+
@formatter = formatter
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Code that reads the value of the source with a resolver, nil
|
|
21
|
+
# for a :value callable and for a resolver without code
|
|
22
|
+
def plain(constants_prefix = "")
|
|
23
|
+
resolver = @value_block
|
|
24
|
+
return unless resolver.is_a?(AttributeValueResolvers::Base)
|
|
25
|
+
|
|
26
|
+
code = resolver.const? ? "#{constants_prefix}CONSTANTS[#{@name.inspect}]" : resolver.code("source")
|
|
27
|
+
code && finish(code, constants_prefix)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Code that calls the :value callable, which `callable_variable`
|
|
31
|
+
# holds. `batches_variable` holds the loaded batches. nil without the
|
|
32
|
+
# :value option.
|
|
33
|
+
def callable(callable_variable, batches_variable, constants_prefix)
|
|
34
|
+
callable = @value_block
|
|
35
|
+
return unless callable.is_a?(AttributeValueResolvers::Callable)
|
|
36
|
+
|
|
37
|
+
finish(callable.code("source", callable_variable, batches_variable), constants_prefix)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Code that reads the value of a :batch attribute without the :value
|
|
41
|
+
# option from the loaded batch, which `batch_variable` holds. nil for
|
|
42
|
+
# other attributes.
|
|
43
|
+
def batch(batch_variable, constants_prefix)
|
|
44
|
+
resolver = @value_block
|
|
45
|
+
return unless resolver.is_a?(AttributeValueResolvers::Batch)
|
|
46
|
+
|
|
47
|
+
finish(resolver.batch_code("source", batch_variable), constants_prefix)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
# Adds the `:default` value and the formatter to the code that reads
|
|
53
|
+
# the value. nil for a formatter that takes the context.
|
|
54
|
+
def finish(code, constants_prefix)
|
|
55
|
+
name = @name.inspect
|
|
56
|
+
code = "(value = #{code}).nil? ? #{constants_prefix}DEFAULTS[#{name}] : value" unless @default.nil?
|
|
57
|
+
return code unless @formatter
|
|
58
|
+
|
|
59
|
+
@formatter.code(code, "#{constants_prefix}FORMATTERS[#{name}]", "#{constants_prefix}FORMATTER_ARGS[#{name}]")
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
data/lib/serega/attribute.rb
CHANGED
|
@@ -14,6 +14,9 @@ class Serega
|
|
|
14
14
|
# Attribute name
|
|
15
15
|
attr_reader :name
|
|
16
16
|
|
|
17
|
+
# Serialized key, a Symbol or a String
|
|
18
|
+
attr_reader :key
|
|
19
|
+
|
|
17
20
|
# Where the attribute is defined
|
|
18
21
|
def location
|
|
19
22
|
initials[:location]
|
|
@@ -22,6 +25,10 @@ class Serega
|
|
|
22
25
|
# Attribute :many option
|
|
23
26
|
attr_reader :many
|
|
24
27
|
|
|
28
|
+
# Preset definition of the :preset, :only, :with and :except options of
|
|
29
|
+
# a relation, nil without these options
|
|
30
|
+
attr_reader :selection
|
|
31
|
+
|
|
25
32
|
# Attribute :default option
|
|
26
33
|
attr_reader :default
|
|
27
34
|
|
|
@@ -53,18 +60,67 @@ class Serega
|
|
|
53
60
|
)
|
|
54
61
|
|
|
55
62
|
normalizer = SeregaAttributeNormalizer.new(serializer_class, initials)
|
|
56
|
-
|
|
57
|
-
@
|
|
63
|
+
@name = normalizer.name
|
|
64
|
+
@key = normalizer.key
|
|
65
|
+
@many = normalizer.many
|
|
66
|
+
@default = normalizer.default
|
|
67
|
+
@value_block = normalizer.value_block
|
|
68
|
+
@value_block_signature = normalizer.value_block_signature
|
|
69
|
+
@value_callable = @value_block.is_a?(AttributeValueResolvers::Callable) ? @value_block.callable : nil
|
|
70
|
+
@hide = normalizer.hide
|
|
71
|
+
serializer = normalizer.serializer
|
|
72
|
+
@serializer = serializer && SerializerReference.new(serializer)
|
|
73
|
+
@selection = normalizer.selection
|
|
74
|
+
@preloads = normalizer.preloads
|
|
75
|
+
@batch_loaders = normalizer.batch_loaders
|
|
76
|
+
@inline_batch_loader = normalizer.inline_batch_loader
|
|
77
|
+
@formatter = normalizer.formatter
|
|
78
|
+
@value_code = ValueCode.new(@name, @value_block, @default, @formatter)
|
|
79
|
+
conditions = normalizer.conditions
|
|
80
|
+
@conditions = conditions && SeregaConditions.new(self, conditions)
|
|
81
|
+
store_values(serializer_class)
|
|
82
|
+
@reads_presenter = false
|
|
58
83
|
end
|
|
59
84
|
|
|
60
85
|
# Method of the serialized object that gives the value, `nil` for a value
|
|
61
86
|
# from :value, :const, :delegate, :hash_access or :batch
|
|
62
87
|
def object_method
|
|
63
|
-
@value_block.
|
|
88
|
+
@value_block.object_method if resolver?
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Ruby code that reads the value in generated code
|
|
92
|
+
attr_reader :value_code
|
|
93
|
+
|
|
94
|
+
# The :value callable, nil without the :value option
|
|
95
|
+
attr_reader :value_callable
|
|
96
|
+
|
|
97
|
+
# Methods that reading the value and checking the :if and :unless
|
|
98
|
+
# conditions call on the object, nil when a callable or a batch loader
|
|
99
|
+
# gets the object
|
|
100
|
+
def source_methods
|
|
101
|
+
return unless resolver?
|
|
102
|
+
|
|
103
|
+
methods = @value_block.source_methods
|
|
104
|
+
return methods unless methods && @conditions
|
|
105
|
+
|
|
106
|
+
condition_methods = @conditions.source_methods
|
|
107
|
+
condition_methods && (methods + condition_methods)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Notes whether the attribute reads the object through the presenter: it
|
|
111
|
+
# calls a presenter method, or it gives the object to a callable or a
|
|
112
|
+
# batch loader. The serializer calls it when it is locked, because
|
|
113
|
+
# presenter methods can be added after the attribute.
|
|
114
|
+
def check_presenter(presenter_class)
|
|
115
|
+
methods = source_methods
|
|
116
|
+
@reads_presenter = methods.nil? || presenter_class.presents_any?(methods)
|
|
64
117
|
end
|
|
65
118
|
|
|
66
|
-
#
|
|
67
|
-
|
|
119
|
+
# Whether the attribute reads the object through the presenter, known
|
|
120
|
+
# after the serializer is locked
|
|
121
|
+
def reads_presenter?
|
|
122
|
+
@reads_presenter
|
|
123
|
+
end
|
|
68
124
|
|
|
69
125
|
# Whether the attribute has an :if, :unless, :if_value or :unless_value condition
|
|
70
126
|
def conditional?
|
|
@@ -76,70 +132,29 @@ class Serega
|
|
|
76
132
|
!@serializer.nil?
|
|
77
133
|
end
|
|
78
134
|
|
|
79
|
-
#
|
|
80
|
-
#
|
|
135
|
+
# Serializer class of the :serializer option or the block, nil without
|
|
136
|
+
# a relation
|
|
81
137
|
def serializer
|
|
82
|
-
|
|
83
|
-
return serializer if (serializer.is_a?(Class) && (serializer < Serega)) || !serializer
|
|
84
|
-
|
|
85
|
-
serializer = serializer.is_a?(String) ? Object.const_get(serializer, false) : serializer.call
|
|
86
|
-
frozen? ? serializer : (@serializer = serializer)
|
|
138
|
+
@serializer&.resolve
|
|
87
139
|
end
|
|
88
140
|
|
|
89
141
|
# Finds attribute value
|
|
90
142
|
#
|
|
91
|
-
# Generated code reads plain attributes
|
|
92
|
-
# their #value_code.
|
|
143
|
+
# Generated code reads plain attributes and :value callables without
|
|
144
|
+
# this method, with their #value_code.
|
|
93
145
|
def value(object, context, batches: nil)
|
|
94
|
-
# Signatures should match allowed signatures in CheckOptValue
|
|
95
146
|
result =
|
|
96
147
|
case @value_block_signature
|
|
97
|
-
when "1" then @value_block.call(object)
|
|
98
|
-
when "
|
|
99
|
-
when "
|
|
100
|
-
|
|
101
|
-
when "2" then @value_block.call(object, context)
|
|
102
|
-
when "2_batches_ctx" then @value_block.call(object, context, ctx: context, batches: batches)
|
|
103
|
-
else @value_block.call # signature is "0" - no parameters
|
|
148
|
+
when "1" then @value_block.call(object) # resolvers that read the object
|
|
149
|
+
when "1_batches" then @value_block.call(object, batches: batches) # the Batch resolver
|
|
150
|
+
when "1_batches_ctx" then @value_block.call(object, ctx: context, batches: batches) # Callable
|
|
151
|
+
else @value_block.call # the Const resolver
|
|
104
152
|
end
|
|
105
153
|
|
|
106
154
|
result = @default if result.nil?
|
|
107
155
|
@formatter ? @formatter.call(result, context) : result
|
|
108
156
|
end
|
|
109
157
|
|
|
110
|
-
# Ruby code that reads the attribute value of the source. `:const` and
|
|
111
|
-
# `:default` values and formatters come from the `CONSTANTS`,
|
|
112
|
-
# `DEFAULTS`, `FORMATTERS` and `FORMATTER_ARGS` of SeregaAttributeValues.
|
|
113
|
-
def value_code(source_variable, constants_prefix = "")
|
|
114
|
-
code =
|
|
115
|
-
case @value_block
|
|
116
|
-
when AttributeValueResolvers::Keyword,
|
|
117
|
-
AttributeValueResolvers::Delegate,
|
|
118
|
-
AttributeValueResolvers::DelegateAllowNil,
|
|
119
|
-
AttributeValueResolvers::HashAccessKeyword,
|
|
120
|
-
AttributeValueResolvers::HashAccessDelegate,
|
|
121
|
-
AttributeValueResolvers::HashAccessDelegateAllowNil
|
|
122
|
-
@value_block.code(source_variable)
|
|
123
|
-
when AttributeValueResolvers::Const
|
|
124
|
-
"#{constants_prefix}CONSTANTS[#{name.inspect}]"
|
|
125
|
-
end
|
|
126
|
-
return unless code
|
|
127
|
-
|
|
128
|
-
code = "(value = #{code}).nil? ? #{constants_prefix}DEFAULTS[#{name.inspect}] : value" unless @default.nil?
|
|
129
|
-
return code unless @formatter
|
|
130
|
-
|
|
131
|
-
@formatter.code(code, "#{constants_prefix}FORMATTERS[#{name.inspect}]", "#{constants_prefix}FORMATTER_ARGS[#{name.inspect}]")
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
# Checks if attribute must be added to serialized response
|
|
135
|
-
def visible?(only, except, with, only_limits)
|
|
136
|
-
return false if except.member?(name) && except[name].empty?
|
|
137
|
-
return true if only.member?(name) || with.member?(name)
|
|
138
|
-
return false if only_limits
|
|
139
|
-
|
|
140
|
-
!hide
|
|
141
|
-
end
|
|
142
|
-
|
|
143
158
|
# Defines the methods of the code on the owner, at the file and line of
|
|
144
159
|
# the attribute, thus their backtraces point to the attribute
|
|
145
160
|
def define_methods(owner, code)
|
|
@@ -149,58 +164,21 @@ class Serega
|
|
|
149
164
|
|
|
150
165
|
private
|
|
151
166
|
|
|
152
|
-
#
|
|
153
|
-
#
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
return unless code
|
|
167
|
+
# Whether the value comes from a resolver that Serega built from the
|
|
168
|
+
# options, and not from a `:value` callable of the user
|
|
169
|
+
def resolver?
|
|
170
|
+
@value_block.is_a?(AttributeValueResolvers::Base)
|
|
171
|
+
end
|
|
158
172
|
|
|
173
|
+
# Stores the values of the attribute that generated code reads in the
|
|
174
|
+
# SeregaAttributeValues of the serializer: the `:const` and `:default`
|
|
175
|
+
# values and the formatter
|
|
176
|
+
def store_values(serializer_class)
|
|
159
177
|
values_class = serializer_class::SeregaAttributeValues
|
|
160
|
-
values_class::CONSTANTS[name] = @value_block.call if @value_block.
|
|
178
|
+
values_class::CONSTANTS[name] = @value_block.call if resolver? && @value_block.const?
|
|
161
179
|
values_class::DEFAULTS[name] = @default unless @default.nil?
|
|
162
180
|
values_class::FORMATTERS[name] = @formatter.callable if @formatter&.callable
|
|
163
181
|
values_class::FORMATTER_ARGS[name] = @formatter.args if @formatter&.args
|
|
164
|
-
|
|
165
|
-
method_name = value_method_name(values_class)
|
|
166
|
-
define_methods(values_class, "def #{method_name}(source) = #{code}")
|
|
167
|
-
method_name
|
|
168
|
-
end
|
|
169
|
-
|
|
170
|
-
# Plain method name of the attribute, for example `read_first_name` for
|
|
171
|
-
# `first-name`. The name keeps a final `?` or `!`. A name taken by
|
|
172
|
-
# another attribute gets a number, for example `read_first_name_2`. The
|
|
173
|
-
# same attribute keeps its name.
|
|
174
|
-
def value_method_name(values_class)
|
|
175
|
-
method_names = values_class::METHOD_NAMES
|
|
176
|
-
method_name = method_names[name]
|
|
177
|
-
return method_name if method_name
|
|
178
|
-
|
|
179
|
-
base, mark = name.to_s.match(/\A(.*?)([?!]?)\z/).captures
|
|
180
|
-
base = "read_#{base.gsub(/\W/, "_")}"
|
|
181
|
-
method_name = :"#{base}#{mark}"
|
|
182
|
-
number = 1
|
|
183
|
-
while method_names.value?(method_name)
|
|
184
|
-
number += 1
|
|
185
|
-
method_name = :"#{base}_#{number}#{mark}"
|
|
186
|
-
end
|
|
187
|
-
method_names[name] = method_name
|
|
188
|
-
end
|
|
189
|
-
|
|
190
|
-
def set_normalized_vars(normalizer)
|
|
191
|
-
@name = normalizer.name
|
|
192
|
-
@many = normalizer.many
|
|
193
|
-
@default = normalizer.default
|
|
194
|
-
@value_block = normalizer.value_block
|
|
195
|
-
@value_block_signature = normalizer.value_block_signature
|
|
196
|
-
@hide = normalizer.hide
|
|
197
|
-
@serializer = normalizer.serializer
|
|
198
|
-
@preloads = normalizer.preloads
|
|
199
|
-
@batch_loaders = normalizer.batch_loaders
|
|
200
|
-
@inline_batch_loader = normalizer.inline_batch_loader
|
|
201
|
-
@formatter = normalizer.formatter
|
|
202
|
-
conditions = normalizer.conditions
|
|
203
|
-
@conditions = conditions && SeregaConditions.new(self, conditions)
|
|
204
182
|
end
|
|
205
183
|
end
|
|
206
184
|
end
|
|
@@ -15,11 +15,17 @@ class Serega
|
|
|
15
15
|
@init_opts = initials[:opts]
|
|
16
16
|
@init_block = initials[:block]
|
|
17
17
|
@method_name = nil
|
|
18
|
+
@key = nil
|
|
18
19
|
end
|
|
19
20
|
|
|
20
|
-
#
|
|
21
|
+
# Attribute name, the serialized key as a Symbol
|
|
21
22
|
def name
|
|
22
|
-
@name ||=
|
|
23
|
+
@name ||= key.to_sym
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Serialized key, the name changed by `config.key_transform`
|
|
27
|
+
def key
|
|
28
|
+
@key ||= prepare_key
|
|
23
29
|
end
|
|
24
30
|
|
|
25
31
|
# Symbolized initial attribute method name
|
|
@@ -59,6 +65,15 @@ class Serega
|
|
|
59
65
|
@serializer = prepare_serializer
|
|
60
66
|
end
|
|
61
67
|
|
|
68
|
+
# Preset definition of the :preset, :only, :with and :except options of
|
|
69
|
+
# a relation, nil without these options
|
|
70
|
+
def selection
|
|
71
|
+
preset, only, with, except = init_opts.values_at(:preset, :only, :with, :except)
|
|
72
|
+
return unless preset || only || with || except
|
|
73
|
+
|
|
74
|
+
SeregaPlan::Presets.definition(preset, only, with, except)
|
|
75
|
+
end
|
|
76
|
+
|
|
62
77
|
# Shows the default attribute value. It is a value that replaces found nils.
|
|
63
78
|
#
|
|
64
79
|
# When custom :default is not specified, we set empty array as default when `many: true` specified
|
|
@@ -89,15 +104,11 @@ class Serega
|
|
|
89
104
|
SeregaBatchLoader.new(name: name, block: batch.use)
|
|
90
105
|
end
|
|
91
106
|
|
|
92
|
-
#
|
|
93
|
-
#
|
|
107
|
+
# The :if, :unless, :if_value and :unless_value conditions, nil without
|
|
108
|
+
# conditions
|
|
94
109
|
def conditions
|
|
95
110
|
conditions = init_opts.slice(:if, :unless, :if_value, :unless_value)
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
conditions.transform_values do |condition|
|
|
99
|
-
condition.is_a?(Symbol) ? AttributeValueResolvers::Keyword.new(condition) : condition
|
|
100
|
-
end
|
|
111
|
+
conditions unless conditions.empty?
|
|
101
112
|
end
|
|
102
113
|
|
|
103
114
|
# Formatter of the :format option. A Symbol option is the name of a
|
|
@@ -106,17 +117,26 @@ class Serega
|
|
|
106
117
|
format = init_opts[:format]
|
|
107
118
|
return unless format
|
|
108
119
|
|
|
109
|
-
format.is_a?(Symbol) ? @serializer_class.formatters.fetch(format) :
|
|
120
|
+
format.is_a?(Symbol) ? @serializer_class.formatters.fetch(format) : SeregaFormatters.build(format)
|
|
110
121
|
end
|
|
111
122
|
|
|
112
123
|
private
|
|
113
124
|
|
|
114
|
-
def
|
|
115
|
-
|
|
125
|
+
def prepare_key
|
|
126
|
+
transform = @serializer_class.config.key_transform
|
|
127
|
+
name = init_name.to_sym
|
|
128
|
+
return name unless transform
|
|
129
|
+
return SeregaUtils::CamelCase.call(name) if transform == :camel_case
|
|
130
|
+
|
|
131
|
+
key = transform.call(name)
|
|
132
|
+
return key if key.is_a?(Symbol)
|
|
133
|
+
return -key if key.is_a?(String)
|
|
134
|
+
|
|
135
|
+
raise SeregaError, "config.key_transform must return a Symbol or a String, #{key.inspect} returned for #{name.inspect}"
|
|
116
136
|
end
|
|
117
137
|
|
|
118
138
|
def prepare_value_block
|
|
119
|
-
|
|
139
|
+
prepare_callable_block \
|
|
120
140
|
|| prepare_const_block \
|
|
121
141
|
|| prepare_delegate_block \
|
|
122
142
|
|| prepare_batch_loader_block \
|
|
@@ -141,9 +161,9 @@ class Serega
|
|
|
141
161
|
# data on demand — those declared with `:preload` or an explicit `:batch`.
|
|
142
162
|
#
|
|
143
163
|
# This is intentionally narrower than a "goes through the batch mechanism"
|
|
144
|
-
# check
|
|
145
|
-
#
|
|
146
|
-
#
|
|
164
|
+
# check: a plain relation (`serializer:` without `:preload`) is
|
|
165
|
+
# batch-processed too, but it only serializes an already-loaded nested
|
|
166
|
+
# object, so it stays visible by default.
|
|
147
167
|
def preload_or_batch?
|
|
148
168
|
!!(preloads || init_opts.key?(:batch))
|
|
149
169
|
end
|
|
@@ -167,6 +187,11 @@ class Serega
|
|
|
167
187
|
(init_opts[:method] || init_name).to_sym
|
|
168
188
|
end
|
|
169
189
|
|
|
190
|
+
def prepare_callable_block
|
|
191
|
+
callable = init_opts[:value]
|
|
192
|
+
callable && AttributeValueResolvers::Callable.new(callable)
|
|
193
|
+
end
|
|
194
|
+
|
|
170
195
|
def prepare_const_block
|
|
171
196
|
return unless init_opts.key?(:const)
|
|
172
197
|
|
|
@@ -214,7 +239,8 @@ class Serega
|
|
|
214
239
|
end
|
|
215
240
|
|
|
216
241
|
def prepare_preloads
|
|
217
|
-
|
|
242
|
+
relation = !serializer.nil?
|
|
243
|
+
AttributeNormalizers::Preloads.call(init_opts, relation, method_name, config)
|
|
218
244
|
end
|
|
219
245
|
end
|
|
220
246
|
end
|
|
@@ -7,12 +7,14 @@ class Serega
|
|
|
7
7
|
#
|
|
8
8
|
class Preloads # :nodoc:
|
|
9
9
|
class << self
|
|
10
|
-
# Preloads of the attribute with options `opts`
|
|
10
|
+
# Preloads of the attribute with options `opts`. `relation` is true
|
|
11
|
+
# for an attribute with a nested serializer: the :serializer option
|
|
12
|
+
# or a block.
|
|
11
13
|
#
|
|
12
14
|
# The value is passed through as provided (Symbol, Array, Hash, or any
|
|
13
15
|
# custom value an ORM understands) — it is the data handed to the
|
|
14
16
|
# serializer's `preload_with` handler.
|
|
15
|
-
def call(opts,
|
|
17
|
+
def call(opts, relation, method_name, config)
|
|
16
18
|
# Explicit preload option (false or nil disables preloading)
|
|
17
19
|
return opts[:preload] || nil if opts.key?(:preload)
|
|
18
20
|
|
|
@@ -22,8 +24,7 @@ class Serega
|
|
|
22
24
|
end
|
|
23
25
|
|
|
24
26
|
# Auto-preload the nested serializer's association
|
|
25
|
-
|
|
26
|
-
if config.auto_preload.fetch(:has_serializer_option) && (opts[:serializer] || block) && !opts.key?(:batch)
|
|
27
|
+
if config.auto_preload.fetch(:has_serializer_option) && relation && !opts.key?(:batch)
|
|
27
28
|
return auto_preload_value(method_name, config)
|
|
28
29
|
end
|
|
29
30
|
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
#
|
|
5
|
+
# Attribute value resolvers
|
|
6
|
+
#
|
|
7
|
+
module AttributeValueResolvers # :nodoc:
|
|
8
|
+
#
|
|
9
|
+
# Parent class of the value resolvers that Serega builds from the
|
|
10
|
+
# attribute options. A `:value` callable of the user is no resolver, thus
|
|
11
|
+
# SeregaAttribute asks only resolvers for their code.
|
|
12
|
+
#
|
|
13
|
+
class Base # :nodoc:
|
|
14
|
+
# Ruby code that reads the value of the source, nil when the value
|
|
15
|
+
# has no code
|
|
16
|
+
def code(_source_variable)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Methods that the value read calls on the source, nil when the
|
|
20
|
+
# source reaches other code
|
|
21
|
+
def source_methods
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Method of the object that gives the value, nil when no method of
|
|
25
|
+
# the object gives it
|
|
26
|
+
def object_method
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Whether the value is the `:const` option
|
|
30
|
+
def const?
|
|
31
|
+
false
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -9,7 +9,7 @@ class Serega
|
|
|
9
9
|
# Value resolver of attributes with the :batch option and without the
|
|
10
10
|
# :value option. Reads the value of the object from the loaded batch.
|
|
11
11
|
#
|
|
12
|
-
class Batch # :nodoc:
|
|
12
|
+
class Batch < Base # :nodoc:
|
|
13
13
|
def initialize(loader_name, id_method)
|
|
14
14
|
@loader_name = loader_name
|
|
15
15
|
@id_method = id_method
|
|
@@ -19,6 +19,19 @@ class Serega
|
|
|
19
19
|
def call(obj, batches:)
|
|
20
20
|
batches.fetch(@loader_name)[obj.public_send(@id_method)]
|
|
21
21
|
end
|
|
22
|
+
|
|
23
|
+
# Ruby code that reads the value of the source from the loaded batch
|
|
24
|
+
# of the loader, which `batch_variable` holds
|
|
25
|
+
def batch_code(source_variable, batch_variable)
|
|
26
|
+
id_method = @id_method
|
|
27
|
+
id_code =
|
|
28
|
+
if SeregaResultCode::PLAIN_METHOD_NAME.match?(id_method)
|
|
29
|
+
"#{source_variable}.#{id_method}"
|
|
30
|
+
else
|
|
31
|
+
"#{source_variable}.public_send(#{id_method.inspect})"
|
|
32
|
+
end
|
|
33
|
+
"#{batch_variable}[#{id_code}]"
|
|
34
|
+
end
|
|
22
35
|
end
|
|
23
36
|
end
|
|
24
37
|
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
#
|
|
5
|
+
# Attribute value resolvers
|
|
6
|
+
#
|
|
7
|
+
module AttributeValueResolvers # :nodoc:
|
|
8
|
+
#
|
|
9
|
+
# Value of attributes with the :value option. Calls the callable with
|
|
10
|
+
# the arguments that its parameters take. It is no resolver: generated
|
|
11
|
+
# code calls the callable itself, with #code.
|
|
12
|
+
#
|
|
13
|
+
class Callable # :nodoc:
|
|
14
|
+
# The :value callable
|
|
15
|
+
attr_reader :callable
|
|
16
|
+
|
|
17
|
+
def initialize(callable)
|
|
18
|
+
@callable = callable
|
|
19
|
+
@signature = SeregaUtils::MethodSignature.call(callable, pos_limit: 2, keyword_args: [:ctx])
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Calls the callable with the object, the context and the batches that
|
|
23
|
+
# its parameters take
|
|
24
|
+
def call(object, ctx:, batches:)
|
|
25
|
+
case @signature
|
|
26
|
+
when "1" then @callable.call(object)
|
|
27
|
+
when "1_ctx" then @callable.call(object, ctx: ctx)
|
|
28
|
+
when "1_batches" then @callable.call(object, batches: batches)
|
|
29
|
+
when "1_batches_ctx" then @callable.call(object, ctx: ctx, batches: batches)
|
|
30
|
+
when "2" then @callable.call(object, ctx)
|
|
31
|
+
when "2_batches_ctx" then @callable.call(object, ctx, ctx: ctx, batches: batches)
|
|
32
|
+
else @callable.call # no parameters
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Ruby code that calls the callable, which `callable_variable` holds,
|
|
37
|
+
# the same way as #call. `batches_variable` holds the loaded batches.
|
|
38
|
+
def code(source_variable, callable_variable, batches_variable)
|
|
39
|
+
arguments =
|
|
40
|
+
case @signature
|
|
41
|
+
when "1" then source_variable
|
|
42
|
+
when "1_ctx" then "#{source_variable}, ctx: context"
|
|
43
|
+
when "1_batches" then "#{source_variable}, batches: #{batches_variable}"
|
|
44
|
+
when "1_batches_ctx" then "#{source_variable}, ctx: context, batches: #{batches_variable}"
|
|
45
|
+
when "2" then "#{source_variable}, context"
|
|
46
|
+
when "2_batches_ctx" then "#{source_variable}, context, ctx: context, batches: #{batches_variable}"
|
|
47
|
+
end
|
|
48
|
+
arguments ? "#{callable_variable}.call(#{arguments})" : "#{callable_variable}.call"
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -8,7 +8,7 @@ class Serega
|
|
|
8
8
|
#
|
|
9
9
|
# Value resolver class for attributes with :const option
|
|
10
10
|
#
|
|
11
|
-
class Const # :nodoc:
|
|
11
|
+
class Const < Base # :nodoc:
|
|
12
12
|
def initialize(const_value)
|
|
13
13
|
@const_value = const_value
|
|
14
14
|
end
|
|
@@ -17,6 +17,16 @@ class Serega
|
|
|
17
17
|
def call
|
|
18
18
|
@const_value
|
|
19
19
|
end
|
|
20
|
+
|
|
21
|
+
# A constant reads no source
|
|
22
|
+
def source_methods
|
|
23
|
+
FROZEN_EMPTY_ARRAY
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# The value is the `:const` option
|
|
27
|
+
def const?
|
|
28
|
+
true
|
|
29
|
+
end
|
|
20
30
|
end
|
|
21
31
|
end
|
|
22
32
|
end
|
|
@@ -18,12 +18,17 @@ class Serega
|
|
|
18
18
|
#
|
|
19
19
|
# Value resolver class for attributes with :delegate (with :allow_nil) option
|
|
20
20
|
#
|
|
21
|
-
class DelegateAllowNil # :nodoc:
|
|
21
|
+
class DelegateAllowNil < Base # :nodoc:
|
|
22
22
|
def initialize(delegate_to, method_name)
|
|
23
23
|
@delegate_to = delegate_to
|
|
24
24
|
@method_name = method_name
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
+
# Methods that the value read calls on the source: the delegate target
|
|
28
|
+
def source_methods
|
|
29
|
+
[@delegate_to]
|
|
30
|
+
end
|
|
31
|
+
|
|
27
32
|
# Delegates method call to another object with safe navigation
|
|
28
33
|
def call(object)
|
|
29
34
|
object.public_send(@delegate_to)&.public_send(@method_name)
|
|
@@ -40,12 +45,17 @@ class Serega
|
|
|
40
45
|
#
|
|
41
46
|
# Value resolver class for attributes with :delegate (without :allow_nil) option
|
|
42
47
|
#
|
|
43
|
-
class Delegate # :nodoc:
|
|
48
|
+
class Delegate < Base # :nodoc:
|
|
44
49
|
def initialize(delegate_to, method_name)
|
|
45
50
|
@delegate_to = delegate_to
|
|
46
51
|
@method_name = method_name
|
|
47
52
|
end
|
|
48
53
|
|
|
54
|
+
# Methods that the value read calls on the source: the delegate target
|
|
55
|
+
def source_methods
|
|
56
|
+
[@delegate_to]
|
|
57
|
+
end
|
|
58
|
+
|
|
49
59
|
# Delegates method call to another object
|
|
50
60
|
def call(object)
|
|
51
61
|
object.public_send(@delegate_to).public_send(@method_name)
|