serega 0.11.0 → 0.11.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 54cf18301cae5fd9b8351603916e033bd31c3f5e36728b815e1ab4008f032023
4
- data.tar.gz: bf35bd1f1f98439e47b493dcf99aa31ce4d0cd6ccf0bda9d3c9bfb82a8a064f0
3
+ metadata.gz: 9b4a68e91572d22cf24e20a79cc788f647eb63e16582f0e33497eec459692de1
4
+ data.tar.gz: 8d3a55c8fd783b00c55cb08ed8f62c0f47cb79573ccbdc9f65c6710489d0dd0f
5
5
  SHA512:
6
- metadata.gz: b97a1bd42e8abcf3f8f7069388bb7ddc0e4e2328b1d5f945597fae19b852526ace0dc1d2c033e806d6537a00e35467d59d68ac9c42e5dc2a324b79e4ee93ffce
7
- data.tar.gz: 9bdea1cf52a44c74137b2af216babc771fc454f165224539a58f3f2a010f4f798af7e6b8e978cdff38e83281d720fda450ea3a51372f7a2b84e2acfde2a5651c
6
+ metadata.gz: 67cb15aafd0dd90dd301a2ae79643c93204c52c26752649f96e904d2e1fe75d37709c91474c891eb73d1ee913002c214fc2c0288a613f82ad082ee385b149338
7
+ data.tar.gz: c421133bba5f4be2227a266bf6eb6fa6013f0320741f4e842575823d3a6d9e93bf37e8029b9fa801b2bcede964679c19fe794a1f7f0dceb10909c98251db588b
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.11.0
1
+ 0.11.2
@@ -79,8 +79,15 @@ class Serega
79
79
  # @return [void]
80
80
  #
81
81
  def self.load_plugin(serializer_class, **_opts)
82
+ require_relative "./lib/batch_config"
82
83
  require_relative "./lib/loader"
83
84
  require_relative "./lib/loaders"
85
+ require_relative "./lib/modules/attribute"
86
+ require_relative "./lib/modules/attribute_normalizer"
87
+ require_relative "./lib/modules/check_attribute_params"
88
+ require_relative "./lib/modules/config"
89
+ require_relative "./lib/modules/object_serializer"
90
+ require_relative "./lib/modules/plan_point"
84
91
  require_relative "./lib/validations/check_batch_opt_key"
85
92
  require_relative "./lib/validations/check_batch_opt_loader"
86
93
  require_relative "./lib/validations/check_opt_batch"
@@ -133,97 +140,7 @@ class Serega
133
140
  config.attribute_keys << :batch
134
141
  config.opts[:batch] = {loaders: {}, default_key: nil, auto_hide: false}
135
142
  config.batch.auto_hide = opts[:auto_hide] if opts.key?(:auto_hide)
136
- end
137
-
138
- #
139
- # Batch loader config
140
- #
141
- class BatchConfig
142
- attr_reader :opts
143
-
144
- def initialize(opts)
145
- @opts = opts
146
- end
147
-
148
- #
149
- # Defines batch loader
150
- #
151
- # @param loader_name [Symbol] Batch loader name, that is used when defining attribute with batch loader.
152
- # @param block [Proc] Block that can accept 3 parameters - keys, context, plan_point
153
- # and returns hash where ids are keys and values are batch loaded objects/
154
- #
155
- # @return [void]
156
- #
157
- def define(loader_name, &block)
158
- unless block
159
- raise SeregaError, "Block must be given to #define method"
160
- end
161
-
162
- params = block.parameters
163
- if params.count > 3 || !params.all? { |param| (param[0] == :req) || (param[0] == :opt) }
164
- raise SeregaError, "Block can have maximum 3 regular parameters"
165
- end
166
-
167
- loaders[loader_name] = block
168
- end
169
-
170
- # Shows defined loaders
171
- # @return [Hash] defined loaders
172
- def loaders
173
- opts[:loaders]
174
- end
175
-
176
- #
177
- # Finds previously defined batch loader by name
178
- #
179
- # @param loader_name [Symbol]
180
- #
181
- # @return [Proc] batch loader block
182
- def fetch_loader(loader_name)
183
- loaders[loader_name] || (raise SeregaError, "Batch loader with name `#{loader_name.inspect}` was not defined. Define example: config.batch.define(:#{loader_name}) { |keys, ctx, points| ... }")
184
- end
185
-
186
- # Shows option to auto hide attributes with :batch specified
187
- # @return [Boolean, nil] option value
188
- def auto_hide
189
- opts[:auto_hide]
190
- end
191
-
192
- # @param value [Boolean] New :auto_hide option value
193
- # @return [Boolean] New option value
194
- def auto_hide=(value)
195
- raise SeregaError, "Must have boolean value, #{value.inspect} provided" if (value != true) && (value != false)
196
- opts[:auto_hide] = value
197
- end
198
-
199
- # Shows default key for :batch option
200
- # @return [Symbol, nil] default key for :batch option
201
- def default_key
202
- opts[:default_key]
203
- end
204
-
205
- # @param value [Symbol] New :default_key option value
206
- # @return [Boolean] New option value
207
- def default_key=(value)
208
- raise SeregaError, "Must be a Symbol, #{value.inspect} provided" unless value.is_a?(Symbol)
209
- opts[:default_key] = value
210
- end
211
- end
212
-
213
- #
214
- # Config class additional/patched instance methods
215
- #
216
- # @see Serega::SeregaConfig
217
- #
218
- module ConfigInstanceMethods
219
- #
220
- # Returns all batch loaders registered for current serializer
221
- #
222
- # @return [Serega::SeregaPlugins::Batch::BatchConfig] configuration for batch loaded attributes
223
- #
224
- def batch
225
- @batch ||= BatchConfig.new(opts.fetch(:batch))
226
- end
143
+ config.batch.default_key = opts[:default_key] if opts.key?(:default_key)
227
144
  end
228
145
 
229
146
  #
@@ -247,138 +164,6 @@ class Serega
247
164
  end
248
165
  end
249
166
 
250
- #
251
- # Serega::SeregaValidations::CheckAttributeParams additional/patched class methods
252
- #
253
- # @see Serega::SeregaValidations::CheckAttributeParams
254
- #
255
- module CheckAttributeParamsInstanceMethods
256
- private
257
-
258
- def check_opts
259
- super
260
-
261
- CheckOptBatch.call(opts, block, self.class.serializer_class)
262
- end
263
- end
264
-
265
- #
266
- # Serega::SeregaAttribute additional/patched class methods
267
- #
268
- # @see Serega::SeregaAttribute
269
- #
270
- module AttributeInstanceMethods
271
- #
272
- # @return [nil, Hash] :batch option
273
- #
274
- attr_reader :batch
275
-
276
- private
277
-
278
- def set_normalized_vars(normalizer)
279
- super
280
- @batch = normalizer.batch
281
- end
282
- end
283
-
284
- #
285
- # SeregaAttributeNormalizer additional/patched instance methods
286
- #
287
- # @see SeregaAttributeNormalizer::AttributeInstanceMethods
288
- #
289
- module AttributeNormalizerInstanceMethods
290
- #
291
- # Returns normalized attribute :batch option with prepared :key and
292
- # :default options. Option :loader will be prepared at serialization
293
- # time as loaders are usually defined after attributes.
294
- #
295
- # @return [Hash] attribute :batch normalized options
296
- #
297
- def batch
298
- return @batch if instance_variable_defined?(:@batch)
299
-
300
- @batch = prepare_batch
301
- end
302
-
303
- private
304
-
305
- #
306
- # Patch for original `prepare_hide` method
307
- #
308
- # Marks attribute hidden if auto_hide option was set and attribute has batch loader
309
- #
310
- def prepare_hide
311
- res = super
312
- return res unless res.nil?
313
-
314
- if batch
315
- self.class.serializer_class.config.batch.auto_hide || nil
316
- end
317
- end
318
-
319
- def prepare_batch
320
- batch = init_opts[:batch]
321
- return unless batch
322
-
323
- # take loader
324
- loader = batch[:loader]
325
-
326
- # take key
327
- key = batch[:key] || self.class.serializer_class.config.batch.default_key
328
- proc_key =
329
- if key.is_a?(Symbol)
330
- proc do |object|
331
- handle_no_method_error { object.public_send(key) }
332
- end
333
- else
334
- key
335
- end
336
-
337
- # take default value
338
- default = batch.fetch(:default) { many ? FROZEN_EMPTY_ARRAY : nil }
339
-
340
- {loader: loader, key: proc_key, default: default}
341
- end
342
-
343
- def handle_no_method_error
344
- yield
345
- rescue NoMethodError => error
346
- raise error, "NoMethodError when serializing '#{name}' attribute in #{self.class.serializer_class}\n\n#{error.message}", error.backtrace
347
- end
348
- end
349
-
350
- #
351
- # Serega::SeregaPlanPoint additional/patched class methods
352
- #
353
- # @see SeregaAttribute
354
- #
355
- module PlanPointInstanceMethods
356
- #
357
- # Returns attribute :batch option with prepared loader
358
- # @return [Hash] attribute :batch option
359
- #
360
- attr_reader :batch
361
-
362
- private
363
-
364
- def set_normalized_vars
365
- super
366
- @batch = prepare_batch
367
- end
368
-
369
- def prepare_batch
370
- batch = attribute.batch
371
- if batch
372
- loader = batch[:loader]
373
- if loader.is_a?(Symbol)
374
- batch_config = attribute.class.serializer_class.config.batch
375
- batch[:loader] = batch_config.fetch_loader(loader)
376
- end
377
- end
378
- batch
379
- end
380
- end
381
-
382
167
  #
383
168
  # Serega additional/patched instance methods
384
169
  #
@@ -397,28 +182,6 @@ class Serega
397
182
  result
398
183
  end
399
184
  end
400
-
401
- #
402
- # SeregaObjectSerializer additional/patched class methods
403
- #
404
- # @see Serega::SeregaObjectSerializer
405
- #
406
- module SeregaObjectSerializerInstanceMethods
407
- private
408
-
409
- def attach_value(object, point, container)
410
- batch = point.batch
411
- return super unless batch
412
-
413
- remember_key_for_batch_loading(batch, object, point, container)
414
- end
415
-
416
- def remember_key_for_batch_loading(batch, object, point, container)
417
- key = batch[:key].call(object, context)
418
- opts[:batch_loaders].get(point, self).remember(key, container)
419
- container[point.name] = nil # Reserve attribute place in resulted hash. We will set correct value later
420
- end
421
- end
422
185
  end
423
186
 
424
187
  register_plugin(Batch.plugin_name, Batch)
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaPlugins
5
+ module Batch
6
+ #
7
+ # Batch plugin config
8
+ #
9
+ class BatchConfig
10
+ attr_reader :opts
11
+
12
+ def initialize(opts)
13
+ @opts = opts
14
+ end
15
+
16
+ #
17
+ # Defines batch loader
18
+ #
19
+ # @param loader_name [Symbol] Batch loader name, that is used when defining attribute with batch loader.
20
+ # @param block [Proc] Block that can accept 3 parameters - keys, context, plan_point
21
+ # and returns hash where ids are keys and values are batch loaded objects/
22
+ #
23
+ # @return [void]
24
+ #
25
+ def define(loader_name, &block)
26
+ unless block
27
+ raise SeregaError, "Block must be given to #define method"
28
+ end
29
+
30
+ params = block.parameters
31
+ if params.count > 3 || !params.all? { |param| (param[0] == :req) || (param[0] == :opt) }
32
+ raise SeregaError, "Block can have maximum 3 regular parameters"
33
+ end
34
+
35
+ loaders[loader_name] = block
36
+ end
37
+
38
+ # Shows defined loaders
39
+ # @return [Hash] defined loaders
40
+ def loaders
41
+ opts[:loaders]
42
+ end
43
+
44
+ #
45
+ # Finds previously defined batch loader by name
46
+ #
47
+ # @param loader_name [Symbol]
48
+ #
49
+ # @return [Proc] batch loader block
50
+ def fetch_loader(loader_name)
51
+ loaders[loader_name] || (raise SeregaError, "Batch loader with name `#{loader_name.inspect}` was not defined. Define example: config.batch.define(:#{loader_name}) { |keys, ctx, points| ... }")
52
+ end
53
+
54
+ # Shows option to auto hide attributes with :batch specified
55
+ # @return [Boolean, nil] option value
56
+ def auto_hide
57
+ opts[:auto_hide]
58
+ end
59
+
60
+ # @param value [Boolean] New :auto_hide option value
61
+ # @return [Boolean] New option value
62
+ def auto_hide=(value)
63
+ raise SeregaError, "Must have boolean value, #{value.inspect} provided" if (value != true) && (value != false)
64
+ opts[:auto_hide] = value
65
+ end
66
+
67
+ # Shows default key for :batch option
68
+ # @return [Symbol, nil] default key for :batch option
69
+ def default_key
70
+ opts[:default_key]
71
+ end
72
+
73
+ # @param value [Symbol] New :default_key option value
74
+ # @return [Boolean] New option value
75
+ def default_key=(value)
76
+ raise SeregaError, "Must be a Symbol, #{value.inspect} provided" unless value.is_a?(Symbol)
77
+ opts[:default_key] = value
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaPlugins
5
+ module Batch
6
+ #
7
+ # Serega::SeregaAttribute additional/patched class methods
8
+ #
9
+ # @see Serega::SeregaAttribute
10
+ #
11
+ module AttributeInstanceMethods
12
+ #
13
+ # @return [nil, Hash] :batch option
14
+ #
15
+ attr_reader :batch
16
+
17
+ private
18
+
19
+ def set_normalized_vars(normalizer)
20
+ super
21
+ @batch = normalizer.batch
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaPlugins
5
+ module Batch
6
+ #
7
+ # SeregaAttributeNormalizer additional/patched instance methods
8
+ #
9
+ # @see SeregaAttributeNormalizer::AttributeInstanceMethods
10
+ #
11
+ module AttributeNormalizerInstanceMethods
12
+ #
13
+ # Returns normalized attribute :batch option with prepared :key and
14
+ # :default options. Option :loader will be prepared at serialization
15
+ # time as loaders are usually defined after attributes.
16
+ #
17
+ # @return [Hash] attribute :batch normalized options
18
+ #
19
+ def batch
20
+ return @batch if instance_variable_defined?(:@batch)
21
+
22
+ @batch = prepare_batch
23
+ end
24
+
25
+ private
26
+
27
+ #
28
+ # Patch for original `prepare_hide` method
29
+ #
30
+ # Marks attribute hidden if auto_hide option was set and attribute has batch loader
31
+ #
32
+ def prepare_hide
33
+ res = super
34
+ return res unless res.nil?
35
+
36
+ if batch
37
+ self.class.serializer_class.config.batch.auto_hide || nil
38
+ end
39
+ end
40
+
41
+ def prepare_batch
42
+ batch = init_opts[:batch]
43
+ return unless batch
44
+
45
+ # take loader
46
+ loader = batch[:loader]
47
+
48
+ # take key
49
+ key = batch[:key] || self.class.serializer_class.config.batch.default_key
50
+ proc_key =
51
+ if key.is_a?(Symbol)
52
+ proc do |object|
53
+ handle_no_method_error { object.public_send(key) }
54
+ end
55
+ else
56
+ key
57
+ end
58
+
59
+ # take default value
60
+ default = batch.fetch(:default) { many ? FROZEN_EMPTY_ARRAY : nil }
61
+
62
+ {loader: loader, key: proc_key, default: default}
63
+ end
64
+
65
+ def handle_no_method_error
66
+ yield
67
+ rescue NoMethodError => error
68
+ raise error, "NoMethodError when serializing '#{name}' attribute in #{self.class.serializer_class}\n\n#{error.message}", error.backtrace
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaPlugins
5
+ module Batch
6
+ #
7
+ # Serega::SeregaValidations::CheckAttributeParams additional/patched class methods
8
+ #
9
+ # @see Serega::SeregaValidations::CheckAttributeParams
10
+ #
11
+ module CheckAttributeParamsInstanceMethods
12
+ private
13
+
14
+ def check_opts
15
+ super
16
+
17
+ CheckOptBatch.call(opts, block, self.class.serializer_class)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaPlugins
5
+ module Batch
6
+ #
7
+ # Config class additional/patched instance methods
8
+ #
9
+ # @see Serega::SeregaConfig
10
+ #
11
+ module ConfigInstanceMethods
12
+ #
13
+ # Returns all batch loaders registered for current serializer
14
+ #
15
+ # @return [Serega::SeregaPlugins::Batch::BatchConfig] configuration for batch loaded attributes
16
+ #
17
+ def batch
18
+ @batch ||= BatchConfig.new(opts.fetch(:batch))
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaPlugins
5
+ module Batch
6
+ #
7
+ # SeregaObjectSerializer additional/patched class methods
8
+ #
9
+ # @see Serega::SeregaObjectSerializer
10
+ #
11
+ module SeregaObjectSerializerInstanceMethods
12
+ private
13
+
14
+ def attach_value(object, point, container)
15
+ batch = point.batch
16
+ return super unless batch
17
+
18
+ remember_key_for_batch_loading(batch, object, point, container)
19
+ end
20
+
21
+ def remember_key_for_batch_loading(batch, object, point, container)
22
+ key = batch[:key].call(object, context)
23
+ batch_loader(point).remember(key, container)
24
+ container[point.name] = nil # Reserve attribute place in resulted hash. We will set correct value later
25
+ end
26
+
27
+ def batch_loader(point)
28
+ batch_loaders = opts[:batch_loaders]
29
+ raise_batch_plugin_for_serializer_not_defined(point) unless batch_loaders
30
+ batch_loaders.get(point, self)
31
+ end
32
+
33
+ def raise_batch_plugin_for_serializer_not_defined(point)
34
+ root_plan = point.plan
35
+ root_plan = plan.parent_plan_point.plan while root_plan.parent_plan_point
36
+ current_serializer = root_plan.serializer_class
37
+ nested_serializer = self.class.serializer_class
38
+
39
+ raise SeregaError,
40
+ "Plugin :batch must be added to current serializer (#{current_serializer})" \
41
+ " to load attributes with :batch option in nested serializer (#{nested_serializer})"
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaPlugins
5
+ module Batch
6
+ #
7
+ # Serega::SeregaPlanPoint additional/patched class methods
8
+ #
9
+ # @see SeregaAttribute
10
+ #
11
+ module PlanPointInstanceMethods
12
+ #
13
+ # Returns attribute :batch option with prepared loader
14
+ # @return [Hash] attribute :batch option
15
+ #
16
+ attr_reader :batch
17
+
18
+ private
19
+
20
+ def set_normalized_vars
21
+ super
22
+ @batch = prepare_batch
23
+ end
24
+
25
+ def prepare_batch
26
+ batch = attribute.batch
27
+ if batch
28
+ loader = batch[:loader]
29
+ if loader.is_a?(Symbol)
30
+ batch_config = attribute.class.serializer_class.config.batch
31
+ batch[:loader] = batch_config.fetch_loader(loader)
32
+ end
33
+ end
34
+ batch
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serega
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.11.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Glushkov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-24 00:00:00.000000000 Z
11
+ date: 2023-04-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  JSON Serializer
@@ -43,8 +43,15 @@ files:
43
43
  - lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb
44
44
  - lib/serega/plugins/activerecord_preloads/lib/preloader.rb
45
45
  - lib/serega/plugins/batch/batch.rb
46
+ - lib/serega/plugins/batch/lib/batch_config.rb
46
47
  - lib/serega/plugins/batch/lib/loader.rb
47
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
48
55
  - lib/serega/plugins/batch/lib/plugins_extensions/activerecord_preloads.rb
49
56
  - lib/serega/plugins/batch/lib/plugins_extensions/formatters.rb
50
57
  - lib/serega/plugins/batch/lib/plugins_extensions/preloads.rb