serega 0.20.1 → 0.30.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/README.md +113 -226
- data/VERSION +1 -1
- data/lib/serega/attribute.rb +31 -3
- data/lib/serega/attribute_normalizer.rb +143 -42
- data/lib/serega/batch/attribute_loader.rb +70 -0
- data/lib/serega/batch/attribute_loaders.rb +59 -0
- data/lib/serega/batch/auto_resolver.rb +24 -0
- data/lib/serega/batch/auto_resolver_factory.rb +48 -0
- data/lib/serega/batch/loader.rb +67 -0
- data/lib/serega/config.rb +59 -1
- data/lib/serega/object_serializer.rb +11 -9
- data/lib/serega/plan.rb +16 -0
- data/lib/serega/plan_point.rb +31 -5
- data/lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb +42 -23
- data/lib/serega/plugins/activerecord_preloads/lib/active_record_objects.rb +31 -0
- data/lib/serega/plugins/camel_case/camel_case.rb +1 -1
- data/lib/serega/plugins/formatters/formatters.rb +79 -22
- data/lib/serega/plugins/if/if.rb +41 -22
- data/lib/serega/plugins/if/validations/check_opt_if.rb +27 -6
- data/lib/serega/plugins/if/validations/check_opt_if_value.rb +27 -6
- data/lib/serega/plugins/if/validations/check_opt_unless.rb +27 -6
- data/lib/serega/plugins/if/validations/check_opt_unless_value.rb +27 -6
- data/lib/serega/plugins/metadata/meta_attribute.rb +9 -10
- data/lib/serega/plugins/metadata/validations/check_block.rb +2 -4
- data/lib/serega/plugins/metadata/validations/check_opt_value.rb +2 -3
- data/lib/serega/plugins/presenter/presenter.rb +1 -1
- data/lib/serega/plugins/string_modifiers/parse_string_modifiers.rb +43 -30
- data/lib/serega/utils/format_user_preloads.rb +58 -0
- data/lib/serega/utils/method_signature.rb +89 -0
- data/lib/serega/utils/preload_paths.rb +53 -0
- data/lib/serega/utils/preloads_constructor.rb +77 -0
- data/lib/serega/validations/attribute/check_block.rb +38 -6
- data/lib/serega/validations/attribute/check_opt_batch.rb +101 -0
- data/lib/serega/validations/attribute/check_opt_const.rb +1 -0
- data/lib/serega/validations/attribute/check_opt_delegate.rb +1 -0
- data/lib/serega/validations/attribute/check_opt_method.rb +1 -0
- data/lib/serega/{plugins/preloads/validations → validations/attribute}/check_opt_preload.rb +2 -2
- data/lib/serega/{plugins/preloads/validations → validations/attribute}/check_opt_preload_path.rb +3 -3
- data/lib/serega/validations/attribute/check_opt_value.rb +35 -9
- data/lib/serega/validations/check_attribute_params.rb +3 -2
- data/lib/serega/validations/check_batch_loader_params.rb +80 -0
- data/lib/serega/validations/initiate/check_modifiers.rb +1 -1
- data/lib/serega.rb +110 -11
- metadata +17 -37
- data/lib/serega/plugins/batch/batch.rb +0 -168
- data/lib/serega/plugins/batch/lib/batch_config.rb +0 -77
- data/lib/serega/plugins/batch/lib/loader.rb +0 -113
- data/lib/serega/plugins/batch/lib/loaders.rb +0 -45
- data/lib/serega/plugins/batch/lib/modules/attribute.rb +0 -26
- data/lib/serega/plugins/batch/lib/modules/attribute_normalizer.rb +0 -78
- data/lib/serega/plugins/batch/lib/modules/check_attribute_params.rb +0 -22
- data/lib/serega/plugins/batch/lib/modules/config.rb +0 -23
- data/lib/serega/plugins/batch/lib/modules/object_serializer.rb +0 -46
- data/lib/serega/plugins/batch/lib/modules/plan_point.rb +0 -22
- data/lib/serega/plugins/batch/lib/plugins_extensions/activerecord_preloads.rb +0 -43
- data/lib/serega/plugins/batch/lib/plugins_extensions/formatters.rb +0 -54
- data/lib/serega/plugins/batch/lib/plugins_extensions/if.rb +0 -31
- data/lib/serega/plugins/batch/lib/plugins_extensions/preloads.rb +0 -34
- data/lib/serega/plugins/batch/lib/validations/check_batch_opt_id_method.rb +0 -43
- data/lib/serega/plugins/batch/lib/validations/check_batch_opt_loader.rb +0 -59
- data/lib/serega/plugins/batch/lib/validations/check_opt_batch.rb +0 -62
- data/lib/serega/plugins/preloads/lib/format_user_preloads.rb +0 -60
- data/lib/serega/plugins/preloads/lib/modules/attribute.rb +0 -28
- data/lib/serega/plugins/preloads/lib/modules/attribute_normalizer.rb +0 -99
- data/lib/serega/plugins/preloads/lib/modules/check_attribute_params.rb +0 -22
- data/lib/serega/plugins/preloads/lib/modules/config.rb +0 -19
- data/lib/serega/plugins/preloads/lib/modules/plan_point.rb +0 -41
- data/lib/serega/plugins/preloads/lib/preload_paths.rb +0 -53
- data/lib/serega/plugins/preloads/lib/preloads_config.rb +0 -62
- data/lib/serega/plugins/preloads/lib/preloads_constructor.rb +0 -79
- data/lib/serega/plugins/preloads/preloads.rb +0 -162
- data/lib/serega/utils/params_count.rb +0 -50
- data/lib/serega/validations/utils/check_extra_keyword_arg.rb +0 -33
data/lib/serega/{plugins/preloads/validations → validations/attribute}/check_opt_preload_path.rb
RENAMED
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Serega
|
4
|
-
module
|
5
|
-
module
|
4
|
+
module SeregaValidations
|
5
|
+
module Attribute
|
6
6
|
#
|
7
7
|
# Validator for attribute :preload_path option
|
8
8
|
#
|
@@ -34,7 +34,7 @@ class Serega
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def check_allowed(path, opts)
|
37
|
-
allowed_paths = PreloadPaths.call(opts[:preload])
|
37
|
+
allowed_paths = SeregaUtils::PreloadPaths.call(opts[:preload])
|
38
38
|
check_required_when_many_allowed(path, allowed_paths)
|
39
39
|
check_in_allowed(path, allowed_paths)
|
40
40
|
end
|
@@ -35,23 +35,49 @@ class Serega
|
|
35
35
|
|
36
36
|
def check_value(value)
|
37
37
|
check_value_type(value)
|
38
|
-
|
39
|
-
|
40
|
-
params_count = SeregaUtils::ParamsCount.call(value, max_count: 2)
|
41
|
-
|
42
|
-
raise SeregaError, params_count_error if params_count > 2
|
38
|
+
signature = SeregaUtils::MethodSignature.call(value, pos_limit: 2, keyword_args: %i[ctx batches])
|
39
|
+
raise SeregaError, signature_error unless valid_signature?(signature)
|
43
40
|
end
|
44
41
|
|
45
42
|
def check_value_type(value)
|
46
43
|
raise SeregaError, type_error if !value.is_a?(Proc) && !value.respond_to?(:call)
|
47
44
|
end
|
48
45
|
|
49
|
-
def
|
50
|
-
|
46
|
+
def valid_signature?(signature)
|
47
|
+
case signature
|
48
|
+
when "0" # no parameters
|
49
|
+
true
|
50
|
+
when "1" # call(object)
|
51
|
+
true
|
52
|
+
when "1_ctx" # call(object, ctx:)
|
53
|
+
true
|
54
|
+
when "1_batches" # call(object, batches:)
|
55
|
+
true
|
56
|
+
when "1_batches_ctx" # call(object, ctx:, batches:)
|
57
|
+
true
|
58
|
+
when "2" # call(object, context)
|
59
|
+
true
|
60
|
+
when "2_batches_ctx" # call(object, context, ctx:, batches:) (proc with no params)
|
61
|
+
true
|
62
|
+
else
|
63
|
+
false
|
64
|
+
end
|
51
65
|
end
|
52
66
|
|
53
|
-
def
|
54
|
-
|
67
|
+
def signature_error
|
68
|
+
<<~ERROR.strip
|
69
|
+
Invalid attribute :value option parameters, valid parameters signatures:
|
70
|
+
- () # no parameters
|
71
|
+
- (object) # one positional parameter
|
72
|
+
- (object, ctx:) # one positional parameter and :ctx keyword
|
73
|
+
- (object, batches:) # one positional parameter and :batches keyword
|
74
|
+
- (object, ctx:, batches:) # one positional parameter, :ctx, and :batches keywords
|
75
|
+
- (object, context) # two positional parameters
|
76
|
+
ERROR
|
77
|
+
end
|
78
|
+
|
79
|
+
def type_error
|
80
|
+
"Option :value value must be a Proc or respond to #call"
|
55
81
|
end
|
56
82
|
end
|
57
83
|
end
|
@@ -53,11 +53,9 @@ class Serega
|
|
53
53
|
end
|
54
54
|
|
55
55
|
# Patched in:
|
56
|
-
# - plugin :batch (checks :batch option)
|
57
56
|
# - plugin :context_metadata (checks context metadata option which is :meta by default)
|
58
57
|
# - plugin :formatters (checks :format option)
|
59
58
|
# - plugin :if (checks :if, :if_value, :unless, :unless_value options)
|
60
|
-
# - plugin :preloads (checks :preload option)
|
61
59
|
def check_opts
|
62
60
|
Utils::CheckAllowedKeys.call(opts, allowed_opts_keys, :attribute)
|
63
61
|
|
@@ -66,8 +64,11 @@ class Serega
|
|
66
64
|
Attribute::CheckOptHide.call(opts)
|
67
65
|
Attribute::CheckOptMethod.call(opts, block)
|
68
66
|
Attribute::CheckOptMany.call(opts)
|
67
|
+
Attribute::CheckOptPreload.call(opts)
|
68
|
+
Attribute::CheckOptPreloadPath.call(opts)
|
69
69
|
Attribute::CheckOptSerializer.call(opts)
|
70
70
|
Attribute::CheckOptValue.call(opts, block)
|
71
|
+
Attribute::CheckOptBatch.call(self.class.serializer_class, opts, block)
|
71
72
|
end
|
72
73
|
|
73
74
|
def check_block
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Serega
|
4
|
+
module SeregaValidations
|
5
|
+
#
|
6
|
+
# Batch loader parameters validators
|
7
|
+
#
|
8
|
+
class CheckBatchLoaderParams
|
9
|
+
#
|
10
|
+
# batch_loader parameters validation instance methods
|
11
|
+
#
|
12
|
+
module InstanceMethods
|
13
|
+
# @return [Symbol] validated batch_loader name
|
14
|
+
attr_reader :name
|
15
|
+
|
16
|
+
# @return [nil, #call] validated batch_loader value or block
|
17
|
+
attr_reader :batch_loader
|
18
|
+
|
19
|
+
# Instantiates batch loader params checker
|
20
|
+
# @param name [Symbol, String] Batch loader name
|
21
|
+
# @param batch_loader [Proc, #call] Batch loader
|
22
|
+
def initialize(name, batch_loader)
|
23
|
+
@name = name
|
24
|
+
@batch_loader = batch_loader
|
25
|
+
end
|
26
|
+
|
27
|
+
#
|
28
|
+
# Checks batch loader parameters
|
29
|
+
#
|
30
|
+
# @raise [SeregaError] SeregaError that batch loader has invalid arguments
|
31
|
+
#
|
32
|
+
# @return [void]
|
33
|
+
#
|
34
|
+
def validate
|
35
|
+
check_name
|
36
|
+
check_loader
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def check_name
|
42
|
+
raise SeregaError, name_type_error if !name.is_a?(Symbol) && !name.is_a?(String)
|
43
|
+
end
|
44
|
+
|
45
|
+
def check_loader
|
46
|
+
check_batch_loader_type
|
47
|
+
check_batch_loader_args
|
48
|
+
end
|
49
|
+
|
50
|
+
def check_batch_loader_type
|
51
|
+
raise SeregaError, type_error if !batch_loader.is_a?(Proc) && !batch_loader.respond_to?(:call)
|
52
|
+
end
|
53
|
+
|
54
|
+
def check_batch_loader_args
|
55
|
+
signature = SeregaUtils::MethodSignature.call(batch_loader, pos_limit: 2, keyword_args: [:ctx])
|
56
|
+
raise SeregaError, arguments_error unless %w[1 2 1_ctx].include?(signature)
|
57
|
+
end
|
58
|
+
|
59
|
+
def name_type_error
|
60
|
+
"Batch loader name must be a Symbol or String"
|
61
|
+
end
|
62
|
+
|
63
|
+
def type_error
|
64
|
+
"Batch loader value must be a Proc or respond to #call"
|
65
|
+
end
|
66
|
+
|
67
|
+
def arguments_error
|
68
|
+
<<~ERR.strip
|
69
|
+
Batch loader arguments should have one of this signatures:
|
70
|
+
- (objects) # one argument
|
71
|
+
- (objects, :ctx) # one argument and one :ctx keyword argument
|
72
|
+
ERR
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
include InstanceMethods
|
77
|
+
extend Serega::SeregaHelpers::SerializerClassHelper
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -71,7 +71,7 @@ class Serega
|
|
71
71
|
|
72
72
|
def build_full_attribute_name(*names)
|
73
73
|
head, *nested = *names
|
74
|
-
result = head.to_s # names are symbols, we need not frozen string
|
74
|
+
result = +head.to_s # names are symbols, we need not frozen string
|
75
75
|
nested.each { |nested_name| result << "(" << nested_name.to_s }
|
76
76
|
nested.each { result << ")" }
|
77
77
|
result
|
data/lib/serega.rb
CHANGED
@@ -17,15 +17,22 @@ require_relative "serega/errors"
|
|
17
17
|
require_relative "serega/helpers/serializer_class_helper"
|
18
18
|
require_relative "serega/utils/enum_deep_dup"
|
19
19
|
require_relative "serega/utils/enum_deep_freeze"
|
20
|
-
require_relative "serega/utils/
|
20
|
+
require_relative "serega/utils/format_user_preloads"
|
21
|
+
require_relative "serega/utils/method_signature"
|
22
|
+
require_relative "serega/utils/preload_paths"
|
23
|
+
require_relative "serega/utils/preloads_constructor"
|
21
24
|
require_relative "serega/utils/symbol_name"
|
22
25
|
require_relative "serega/utils/to_hash"
|
23
26
|
require_relative "serega/json/adapter"
|
24
27
|
|
25
28
|
require_relative "serega/attribute"
|
26
29
|
require_relative "serega/attribute_normalizer"
|
30
|
+
require_relative "serega/batch/attribute_loader"
|
31
|
+
require_relative "serega/batch/attribute_loaders"
|
32
|
+
require_relative "serega/batch/auto_resolver"
|
33
|
+
require_relative "serega/batch/auto_resolver_factory"
|
34
|
+
require_relative "serega/batch/loader"
|
27
35
|
require_relative "serega/validations/utils/check_allowed_keys"
|
28
|
-
require_relative "serega/validations/utils/check_extra_keyword_arg"
|
29
36
|
require_relative "serega/validations/utils/check_opt_is_bool"
|
30
37
|
require_relative "serega/validations/utils/check_opt_is_hash"
|
31
38
|
require_relative "serega/validations/utils/check_opt_is_string_or_symbol"
|
@@ -34,13 +41,17 @@ require_relative "serega/validations/attribute/check_name"
|
|
34
41
|
require_relative "serega/validations/attribute/check_opt_const"
|
35
42
|
require_relative "serega/validations/attribute/check_opt_hide"
|
36
43
|
require_relative "serega/validations/attribute/check_opt_delegate"
|
44
|
+
require_relative "serega/validations/attribute/check_opt_batch"
|
37
45
|
require_relative "serega/validations/attribute/check_opt_many"
|
38
46
|
require_relative "serega/validations/attribute/check_opt_method"
|
47
|
+
require_relative "serega/validations/attribute/check_opt_preload"
|
48
|
+
require_relative "serega/validations/attribute/check_opt_preload_path"
|
39
49
|
require_relative "serega/validations/attribute/check_opt_serializer"
|
40
50
|
require_relative "serega/validations/attribute/check_opt_value"
|
41
51
|
require_relative "serega/validations/initiate/check_modifiers"
|
42
52
|
require_relative "serega/validations/check_attribute_params"
|
43
53
|
require_relative "serega/validations/check_initiate_params"
|
54
|
+
require_relative "serega/validations/check_batch_loader_params"
|
44
55
|
require_relative "serega/validations/check_serialize_params"
|
45
56
|
|
46
57
|
require_relative "serega/config"
|
@@ -67,6 +78,21 @@ class Serega
|
|
67
78
|
check_serialize_params_class.serializer_class = self
|
68
79
|
const_set(:CheckSerializeParams, check_serialize_params_class)
|
69
80
|
|
81
|
+
# Validates `Serializer.batch_loader` params
|
82
|
+
check_batch_loader_params_class = Class.new(SeregaValidations::CheckBatchLoaderParams)
|
83
|
+
check_batch_loader_params_class.serializer_class = self
|
84
|
+
const_set(:CheckBatchLoaderParams, check_batch_loader_params_class)
|
85
|
+
|
86
|
+
# Assigns `SeregaBatchLoader` constant to current class
|
87
|
+
batch_loader_class = Class.new(SeregaBatch::Loader)
|
88
|
+
batch_loader_class.serializer_class = self
|
89
|
+
const_set(:SeregaBatchLoader, batch_loader_class)
|
90
|
+
|
91
|
+
# Assigns `SeregaBatchAttributeLoader` constant to current class
|
92
|
+
batch_attribute_loader_class = Class.new(SeregaBatch::AttributeLoader)
|
93
|
+
batch_attribute_loader_class.serializer_class = self
|
94
|
+
const_set(:SeregaBatchAttributeLoader, batch_attribute_loader_class)
|
95
|
+
|
70
96
|
#
|
71
97
|
# Serializers class methods
|
72
98
|
#
|
@@ -128,6 +154,15 @@ class Serega
|
|
128
154
|
@attributes ||= {}
|
129
155
|
end
|
130
156
|
|
157
|
+
#
|
158
|
+
# Lists batch loaders
|
159
|
+
#
|
160
|
+
# @return [Hash] batch loaders list
|
161
|
+
#
|
162
|
+
def batch_loaders
|
163
|
+
@batch_loaders ||= {}
|
164
|
+
end
|
165
|
+
|
131
166
|
#
|
132
167
|
# Adds attribute
|
133
168
|
#
|
@@ -145,6 +180,41 @@ class Serega
|
|
145
180
|
attributes[attribute.name] = attribute
|
146
181
|
end
|
147
182
|
|
183
|
+
#
|
184
|
+
# Defines a batch loader
|
185
|
+
#
|
186
|
+
# @example
|
187
|
+
# batch_loader :tags, PostTagsLoader
|
188
|
+
#
|
189
|
+
# @example with block
|
190
|
+
# batch_loader(:tags) do |posts|
|
191
|
+
# Tags.where(post: posts).group(:post_id).pluck(:post_id, Arel.sql('ARRAY_AGG(tags.tag ORDER BY tag)')).to_h
|
192
|
+
# end
|
193
|
+
#
|
194
|
+
# attribute :tags, batch: :tags, value: { |post, batch:| batch[:tags][post.id] }
|
195
|
+
#
|
196
|
+
# @example with context
|
197
|
+
# batch_loader(:tags) do |posts, ctx:|
|
198
|
+
# next {} if ctx[:bot]
|
199
|
+
#
|
200
|
+
# Tags.where(post: posts).group(:post_id).pluck(:post_id, Arel.sql('ARRAY_AGG(tags.tag ORDER BY tag)')).to_h
|
201
|
+
# end
|
202
|
+
#
|
203
|
+
# attribute :tags, batch: :tags, value: { |post, batch:| batch[:tags][post.id] }
|
204
|
+
#
|
205
|
+
# @param name [Symbol] A batch loader name
|
206
|
+
# @param value [#call] Batch loader
|
207
|
+
# @param block [Proc] Batch loader
|
208
|
+
#
|
209
|
+
# @return [#call] Batch loader
|
210
|
+
#
|
211
|
+
def batch_loader(name, value = nil, &block)
|
212
|
+
raise SeregaError, "Batch loader must be defined with a callable value or block" if (value && block) || (!value && !block)
|
213
|
+
|
214
|
+
batch_loader = self::SeregaBatchLoader.new(name: name, block: value || block)
|
215
|
+
batch_loaders[batch_loader.name] = batch_loader
|
216
|
+
end
|
217
|
+
|
148
218
|
#
|
149
219
|
# Serializes provided object to Hash
|
150
220
|
#
|
@@ -167,6 +237,7 @@ class Serega
|
|
167
237
|
modifiers_opts = FROZEN_EMPTY_HASH
|
168
238
|
serialize_opts = nil
|
169
239
|
else
|
240
|
+
opts.transform_keys!(&:to_sym)
|
170
241
|
serialize_opts = opts.except(*initiate_keys)
|
171
242
|
modifiers_opts = opts.slice(*initiate_keys)
|
172
243
|
end
|
@@ -231,7 +302,6 @@ class Serega
|
|
231
302
|
private
|
232
303
|
|
233
304
|
# Patched in:
|
234
|
-
# - plugin :batch (defines SeregaBatchLoaders, SeregaBatchLoader)
|
235
305
|
# - plugin :metadata (defines MetaAttribute and copies meta_attributes to subclasses)
|
236
306
|
# - plugin :presenter (defines Presenter)
|
237
307
|
def inherited(subclass)
|
@@ -256,6 +326,14 @@ class Serega
|
|
256
326
|
plan_point_class.serializer_class = subclass
|
257
327
|
subclass.const_set(:SeregaPlanPoint, plan_point_class)
|
258
328
|
|
329
|
+
batch_loader_class = Class.new(self::SeregaBatchLoader)
|
330
|
+
batch_loader_class.serializer_class = subclass
|
331
|
+
subclass.const_set(:SeregaBatchLoader, batch_loader_class)
|
332
|
+
|
333
|
+
batch_attribute_loader_class = Class.new(self::SeregaBatchAttributeLoader)
|
334
|
+
batch_attribute_loader_class.serializer_class = subclass
|
335
|
+
subclass.const_set(:SeregaBatchAttributeLoader, batch_attribute_loader_class)
|
336
|
+
|
259
337
|
object_serializer_class = Class.new(self::SeregaObjectSerializer)
|
260
338
|
object_serializer_class.serializer_class = subclass
|
261
339
|
subclass.const_set(:SeregaObjectSerializer, object_serializer_class)
|
@@ -272,12 +350,21 @@ class Serega
|
|
272
350
|
check_serialize_params_class.serializer_class = subclass
|
273
351
|
subclass.const_set(:CheckSerializeParams, check_serialize_params_class)
|
274
352
|
|
353
|
+
check_batch_loader_params_class = Class.new(self::CheckBatchLoaderParams)
|
354
|
+
check_batch_loader_params_class.serializer_class = self
|
355
|
+
subclass.const_set(:CheckBatchLoaderParams, check_batch_loader_params_class)
|
356
|
+
|
275
357
|
# Assign same attributes
|
276
358
|
attributes.each_value do |attr|
|
277
359
|
params = attr.initials
|
278
360
|
subclass.attribute(params[:name], **params[:opts], ¶ms[:block])
|
279
361
|
end
|
280
362
|
|
363
|
+
# Assign same batch loaders
|
364
|
+
batch_loaders.each_value do |loader|
|
365
|
+
subclass.batch_loader(loader.name, loader.block)
|
366
|
+
end
|
367
|
+
|
281
368
|
super
|
282
369
|
end
|
283
370
|
end
|
@@ -296,7 +383,14 @@ class Serega
|
|
296
383
|
# @option opts [Boolean] :validate Validates provided modifiers (Default is true)
|
297
384
|
#
|
298
385
|
def initialize(opts = nil)
|
299
|
-
@opts =
|
386
|
+
@opts =
|
387
|
+
if opts.nil? || opts.empty?
|
388
|
+
FROZEN_EMPTY_HASH
|
389
|
+
else
|
390
|
+
opts.transform_keys!(&:to_sym)
|
391
|
+
parse_modifiers(opts)
|
392
|
+
end
|
393
|
+
|
300
394
|
self.class::CheckInitiateParams.new(@opts).validate if opts&.fetch(:check_initiate_params) { config.check_initiate_params }
|
301
395
|
|
302
396
|
@plan = self.class::SeregaPlan.call(@opts)
|
@@ -320,10 +414,10 @@ class Serega
|
|
320
414
|
# @return [Hash] Serialization result
|
321
415
|
#
|
322
416
|
def call(object, opts = nil)
|
323
|
-
|
324
|
-
opts
|
325
|
-
opts[:context] ||= {}
|
417
|
+
opts = opts ? opts.transform_keys!(&:to_sym) : {}
|
418
|
+
self.class::CheckSerializeParams.new(opts).validate unless opts.empty?
|
326
419
|
|
420
|
+
opts[:context] ||= {}
|
327
421
|
serialize(object, opts)
|
328
422
|
end
|
329
423
|
|
@@ -332,6 +426,11 @@ class Serega
|
|
332
426
|
call(object, opts)
|
333
427
|
end
|
334
428
|
|
429
|
+
# @return [Hash] merged preloads of all serialized attributes
|
430
|
+
def preloads
|
431
|
+
@preloads ||= SeregaUtils::PreloadsConstructor.call(plan)
|
432
|
+
end
|
433
|
+
|
335
434
|
#
|
336
435
|
# Serializes provided object to JSON string
|
337
436
|
#
|
@@ -389,14 +488,14 @@ class Serega
|
|
389
488
|
|
390
489
|
# Patched in:
|
391
490
|
# - plugin :activerecord_preloads (loads defined :preloads to object)
|
392
|
-
# - plugin :batch (runs serialization of collected batches)
|
393
491
|
# - plugin :root (wraps result `{ root => result }`)
|
394
492
|
# - plugin :context_metadata (adds context metadata to final result)
|
395
493
|
# - plugin :metadata (adds metadata to final result)
|
396
494
|
def serialize(object, opts)
|
397
|
-
|
398
|
-
|
399
|
-
|
495
|
+
batch_loaders = plan.has_batch_points ? SeregaBatch::AttributeLoaders.new : nil
|
496
|
+
result = self.class::SeregaObjectSerializer.new(**opts, batch_loaders: batch_loaders, plan: plan).serialize(object)
|
497
|
+
batch_loaders&.load_all(opts[:context])
|
498
|
+
result
|
400
499
|
end
|
401
500
|
end
|
402
501
|
|
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serega
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.30.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Glushkov
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
12
11
|
dependencies: []
|
13
12
|
description: |
|
14
13
|
JSON Serializer
|
@@ -30,6 +29,11 @@ files:
|
|
30
29
|
- lib/serega.rb
|
31
30
|
- lib/serega/attribute.rb
|
32
31
|
- lib/serega/attribute_normalizer.rb
|
32
|
+
- lib/serega/batch/attribute_loader.rb
|
33
|
+
- lib/serega/batch/attribute_loaders.rb
|
34
|
+
- lib/serega/batch/auto_resolver.rb
|
35
|
+
- lib/serega/batch/auto_resolver_factory.rb
|
36
|
+
- lib/serega/batch/loader.rb
|
33
37
|
- lib/serega/config.rb
|
34
38
|
- lib/serega/errors.rb
|
35
39
|
- lib/serega/helpers/serializer_class_helper.rb
|
@@ -41,24 +45,8 @@ files:
|
|
41
45
|
- lib/serega/plan_point.rb
|
42
46
|
- lib/serega/plugins.rb
|
43
47
|
- lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb
|
48
|
+
- lib/serega/plugins/activerecord_preloads/lib/active_record_objects.rb
|
44
49
|
- lib/serega/plugins/activerecord_preloads/lib/preloader.rb
|
45
|
-
- lib/serega/plugins/batch/batch.rb
|
46
|
-
- lib/serega/plugins/batch/lib/batch_config.rb
|
47
|
-
- lib/serega/plugins/batch/lib/loader.rb
|
48
|
-
- lib/serega/plugins/batch/lib/loaders.rb
|
49
|
-
- lib/serega/plugins/batch/lib/modules/attribute.rb
|
50
|
-
- lib/serega/plugins/batch/lib/modules/attribute_normalizer.rb
|
51
|
-
- lib/serega/plugins/batch/lib/modules/check_attribute_params.rb
|
52
|
-
- lib/serega/plugins/batch/lib/modules/config.rb
|
53
|
-
- lib/serega/plugins/batch/lib/modules/object_serializer.rb
|
54
|
-
- lib/serega/plugins/batch/lib/modules/plan_point.rb
|
55
|
-
- lib/serega/plugins/batch/lib/plugins_extensions/activerecord_preloads.rb
|
56
|
-
- lib/serega/plugins/batch/lib/plugins_extensions/formatters.rb
|
57
|
-
- lib/serega/plugins/batch/lib/plugins_extensions/if.rb
|
58
|
-
- lib/serega/plugins/batch/lib/plugins_extensions/preloads.rb
|
59
|
-
- lib/serega/plugins/batch/lib/validations/check_batch_opt_id_method.rb
|
60
|
-
- lib/serega/plugins/batch/lib/validations/check_batch_opt_loader.rb
|
61
|
-
- lib/serega/plugins/batch/lib/validations/check_opt_batch.rb
|
62
50
|
- lib/serega/plugins/camel_case/camel_case.rb
|
63
51
|
- lib/serega/plugins/context_metadata/context_metadata.rb
|
64
52
|
- lib/serega/plugins/depth_limit/depth_limit.rb
|
@@ -79,42 +67,36 @@ files:
|
|
79
67
|
- lib/serega/plugins/metadata/validations/check_opt_value.rb
|
80
68
|
- lib/serega/plugins/metadata/validations/check_opts.rb
|
81
69
|
- lib/serega/plugins/metadata/validations/check_path.rb
|
82
|
-
- lib/serega/plugins/preloads/lib/format_user_preloads.rb
|
83
|
-
- lib/serega/plugins/preloads/lib/modules/attribute.rb
|
84
|
-
- lib/serega/plugins/preloads/lib/modules/attribute_normalizer.rb
|
85
|
-
- lib/serega/plugins/preloads/lib/modules/check_attribute_params.rb
|
86
|
-
- lib/serega/plugins/preloads/lib/modules/config.rb
|
87
|
-
- lib/serega/plugins/preloads/lib/modules/plan_point.rb
|
88
|
-
- lib/serega/plugins/preloads/lib/preload_paths.rb
|
89
|
-
- lib/serega/plugins/preloads/lib/preloads_config.rb
|
90
|
-
- lib/serega/plugins/preloads/lib/preloads_constructor.rb
|
91
|
-
- lib/serega/plugins/preloads/preloads.rb
|
92
|
-
- lib/serega/plugins/preloads/validations/check_opt_preload.rb
|
93
|
-
- lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
|
94
70
|
- lib/serega/plugins/presenter/presenter.rb
|
95
71
|
- lib/serega/plugins/root/root.rb
|
96
72
|
- lib/serega/plugins/string_modifiers/parse_string_modifiers.rb
|
97
73
|
- lib/serega/plugins/string_modifiers/string_modifiers.rb
|
98
74
|
- lib/serega/utils/enum_deep_dup.rb
|
99
75
|
- lib/serega/utils/enum_deep_freeze.rb
|
100
|
-
- lib/serega/utils/
|
76
|
+
- lib/serega/utils/format_user_preloads.rb
|
77
|
+
- lib/serega/utils/method_signature.rb
|
78
|
+
- lib/serega/utils/preload_paths.rb
|
79
|
+
- lib/serega/utils/preloads_constructor.rb
|
101
80
|
- lib/serega/utils/symbol_name.rb
|
102
81
|
- lib/serega/utils/to_hash.rb
|
103
82
|
- lib/serega/validations/attribute/check_block.rb
|
104
83
|
- lib/serega/validations/attribute/check_name.rb
|
84
|
+
- lib/serega/validations/attribute/check_opt_batch.rb
|
105
85
|
- lib/serega/validations/attribute/check_opt_const.rb
|
106
86
|
- lib/serega/validations/attribute/check_opt_delegate.rb
|
107
87
|
- lib/serega/validations/attribute/check_opt_hide.rb
|
108
88
|
- lib/serega/validations/attribute/check_opt_many.rb
|
109
89
|
- lib/serega/validations/attribute/check_opt_method.rb
|
90
|
+
- lib/serega/validations/attribute/check_opt_preload.rb
|
91
|
+
- lib/serega/validations/attribute/check_opt_preload_path.rb
|
110
92
|
- lib/serega/validations/attribute/check_opt_serializer.rb
|
111
93
|
- lib/serega/validations/attribute/check_opt_value.rb
|
112
94
|
- lib/serega/validations/check_attribute_params.rb
|
95
|
+
- lib/serega/validations/check_batch_loader_params.rb
|
113
96
|
- lib/serega/validations/check_initiate_params.rb
|
114
97
|
- lib/serega/validations/check_serialize_params.rb
|
115
98
|
- lib/serega/validations/initiate/check_modifiers.rb
|
116
99
|
- lib/serega/validations/utils/check_allowed_keys.rb
|
117
|
-
- lib/serega/validations/utils/check_extra_keyword_arg.rb
|
118
100
|
- lib/serega/validations/utils/check_opt_is_bool.rb
|
119
101
|
- lib/serega/validations/utils/check_opt_is_hash.rb
|
120
102
|
- lib/serega/validations/utils/check_opt_is_string_or_symbol.rb
|
@@ -126,7 +108,6 @@ metadata:
|
|
126
108
|
source_code_uri: https://github.com/aglushkov/serega
|
127
109
|
documentation_uri: https://www.rubydoc.info/gems/serega
|
128
110
|
changelog_uri: https://github.com/aglushkov/serega/blob/master/CHANGELOG.md
|
129
|
-
post_install_message:
|
130
111
|
rdoc_options: []
|
131
112
|
require_paths:
|
132
113
|
- lib
|
@@ -141,8 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
122
|
- !ruby/object:Gem::Version
|
142
123
|
version: '0'
|
143
124
|
requirements: []
|
144
|
-
rubygems_version: 3.
|
145
|
-
signing_key:
|
125
|
+
rubygems_version: 3.7.1
|
146
126
|
specification_version: 4
|
147
127
|
summary: JSON Serializer
|
148
128
|
test_files: []
|