serega 0.11.1 → 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: fe523bff250ef512761ea848c60d5cc5468742509a60e675bb089a666b95ad50
4
- data.tar.gz: 33c44df17578c3026d6b80d9c63152b4d4b238646b94feb93a849f2dd3b4d387
3
+ metadata.gz: 9b4a68e91572d22cf24e20a79cc788f647eb63e16582f0e33497eec459692de1
4
+ data.tar.gz: 8d3a55c8fd783b00c55cb08ed8f62c0f47cb79573ccbdc9f65c6710489d0dd0f
5
5
  SHA512:
6
- metadata.gz: 7f8041f6459fd5560f27c8f069c509df43c129ef87383227d7bee90ebad6489b4db983a5de4e7e7b767dabb22a02012fe8b1fba3cb21dd9963a0adadd444c756
7
- data.tar.gz: 55b4f299cad5da8f009fb0162ce2c558a0c01821db7318e7cf1d7ffc5915bdccfbc843ea924fe8fdcb5352897b1a871b4c8c829b8e585d3bfa6e03fe0a8c4abf
6
+ metadata.gz: 67cb15aafd0dd90dd301a2ae79643c93204c52c26752649f96e904d2e1fe75d37709c91474c891eb73d1ee913002c214fc2c0288a613f82ad082ee385b149338
7
+ data.tar.gz: c421133bba5f4be2227a266bf6eb6fa6013f0320741f4e842575823d3a6d9e93bf37e8029b9fa801b2bcede964679c19fe794a1f7f0dceb10909c98251db588b
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.11.1
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"
@@ -136,97 +143,6 @@ class Serega
136
143
  config.batch.default_key = opts[:default_key] if opts.key?(:default_key)
137
144
  end
138
145
 
139
- #
140
- # Batch loader config
141
- #
142
- class BatchConfig
143
- attr_reader :opts
144
-
145
- def initialize(opts)
146
- @opts = opts
147
- end
148
-
149
- #
150
- # Defines batch loader
151
- #
152
- # @param loader_name [Symbol] Batch loader name, that is used when defining attribute with batch loader.
153
- # @param block [Proc] Block that can accept 3 parameters - keys, context, plan_point
154
- # and returns hash where ids are keys and values are batch loaded objects/
155
- #
156
- # @return [void]
157
- #
158
- def define(loader_name, &block)
159
- unless block
160
- raise SeregaError, "Block must be given to #define method"
161
- end
162
-
163
- params = block.parameters
164
- if params.count > 3 || !params.all? { |param| (param[0] == :req) || (param[0] == :opt) }
165
- raise SeregaError, "Block can have maximum 3 regular parameters"
166
- end
167
-
168
- loaders[loader_name] = block
169
- end
170
-
171
- # Shows defined loaders
172
- # @return [Hash] defined loaders
173
- def loaders
174
- opts[:loaders]
175
- end
176
-
177
- #
178
- # Finds previously defined batch loader by name
179
- #
180
- # @param loader_name [Symbol]
181
- #
182
- # @return [Proc] batch loader block
183
- def fetch_loader(loader_name)
184
- 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| ... }")
185
- end
186
-
187
- # Shows option to auto hide attributes with :batch specified
188
- # @return [Boolean, nil] option value
189
- def auto_hide
190
- opts[:auto_hide]
191
- end
192
-
193
- # @param value [Boolean] New :auto_hide option value
194
- # @return [Boolean] New option value
195
- def auto_hide=(value)
196
- raise SeregaError, "Must have boolean value, #{value.inspect} provided" if (value != true) && (value != false)
197
- opts[:auto_hide] = value
198
- end
199
-
200
- # Shows default key for :batch option
201
- # @return [Symbol, nil] default key for :batch option
202
- def default_key
203
- opts[:default_key]
204
- end
205
-
206
- # @param value [Symbol] New :default_key option value
207
- # @return [Boolean] New option value
208
- def default_key=(value)
209
- raise SeregaError, "Must be a Symbol, #{value.inspect} provided" unless value.is_a?(Symbol)
210
- opts[:default_key] = value
211
- end
212
- end
213
-
214
- #
215
- # Config class additional/patched instance methods
216
- #
217
- # @see Serega::SeregaConfig
218
- #
219
- module ConfigInstanceMethods
220
- #
221
- # Returns all batch loaders registered for current serializer
222
- #
223
- # @return [Serega::SeregaPlugins::Batch::BatchConfig] configuration for batch loaded attributes
224
- #
225
- def batch
226
- @batch ||= BatchConfig.new(opts.fetch(:batch))
227
- end
228
- end
229
-
230
146
  #
231
147
  # Serega class additional/patched class methods
232
148
  #
@@ -248,138 +164,6 @@ class Serega
248
164
  end
249
165
  end
250
166
 
251
- #
252
- # Serega::SeregaValidations::CheckAttributeParams additional/patched class methods
253
- #
254
- # @see Serega::SeregaValidations::CheckAttributeParams
255
- #
256
- module CheckAttributeParamsInstanceMethods
257
- private
258
-
259
- def check_opts
260
- super
261
-
262
- CheckOptBatch.call(opts, block, self.class.serializer_class)
263
- end
264
- end
265
-
266
- #
267
- # Serega::SeregaAttribute additional/patched class methods
268
- #
269
- # @see Serega::SeregaAttribute
270
- #
271
- module AttributeInstanceMethods
272
- #
273
- # @return [nil, Hash] :batch option
274
- #
275
- attr_reader :batch
276
-
277
- private
278
-
279
- def set_normalized_vars(normalizer)
280
- super
281
- @batch = normalizer.batch
282
- end
283
- end
284
-
285
- #
286
- # SeregaAttributeNormalizer additional/patched instance methods
287
- #
288
- # @see SeregaAttributeNormalizer::AttributeInstanceMethods
289
- #
290
- module AttributeNormalizerInstanceMethods
291
- #
292
- # Returns normalized attribute :batch option with prepared :key and
293
- # :default options. Option :loader will be prepared at serialization
294
- # time as loaders are usually defined after attributes.
295
- #
296
- # @return [Hash] attribute :batch normalized options
297
- #
298
- def batch
299
- return @batch if instance_variable_defined?(:@batch)
300
-
301
- @batch = prepare_batch
302
- end
303
-
304
- private
305
-
306
- #
307
- # Patch for original `prepare_hide` method
308
- #
309
- # Marks attribute hidden if auto_hide option was set and attribute has batch loader
310
- #
311
- def prepare_hide
312
- res = super
313
- return res unless res.nil?
314
-
315
- if batch
316
- self.class.serializer_class.config.batch.auto_hide || nil
317
- end
318
- end
319
-
320
- def prepare_batch
321
- batch = init_opts[:batch]
322
- return unless batch
323
-
324
- # take loader
325
- loader = batch[:loader]
326
-
327
- # take key
328
- key = batch[:key] || self.class.serializer_class.config.batch.default_key
329
- proc_key =
330
- if key.is_a?(Symbol)
331
- proc do |object|
332
- handle_no_method_error { object.public_send(key) }
333
- end
334
- else
335
- key
336
- end
337
-
338
- # take default value
339
- default = batch.fetch(:default) { many ? FROZEN_EMPTY_ARRAY : nil }
340
-
341
- {loader: loader, key: proc_key, default: default}
342
- end
343
-
344
- def handle_no_method_error
345
- yield
346
- rescue NoMethodError => error
347
- raise error, "NoMethodError when serializing '#{name}' attribute in #{self.class.serializer_class}\n\n#{error.message}", error.backtrace
348
- end
349
- end
350
-
351
- #
352
- # Serega::SeregaPlanPoint additional/patched class methods
353
- #
354
- # @see SeregaAttribute
355
- #
356
- module PlanPointInstanceMethods
357
- #
358
- # Returns attribute :batch option with prepared loader
359
- # @return [Hash] attribute :batch option
360
- #
361
- attr_reader :batch
362
-
363
- private
364
-
365
- def set_normalized_vars
366
- super
367
- @batch = prepare_batch
368
- end
369
-
370
- def prepare_batch
371
- batch = attribute.batch
372
- if batch
373
- loader = batch[:loader]
374
- if loader.is_a?(Symbol)
375
- batch_config = attribute.class.serializer_class.config.batch
376
- batch[:loader] = batch_config.fetch_loader(loader)
377
- end
378
- end
379
- batch
380
- end
381
- end
382
-
383
167
  #
384
168
  # Serega additional/patched instance methods
385
169
  #
@@ -398,28 +182,6 @@ class Serega
398
182
  result
399
183
  end
400
184
  end
401
-
402
- #
403
- # SeregaObjectSerializer additional/patched class methods
404
- #
405
- # @see Serega::SeregaObjectSerializer
406
- #
407
- module SeregaObjectSerializerInstanceMethods
408
- private
409
-
410
- def attach_value(object, point, container)
411
- batch = point.batch
412
- return super unless batch
413
-
414
- remember_key_for_batch_loading(batch, object, point, container)
415
- end
416
-
417
- def remember_key_for_batch_loading(batch, object, point, container)
418
- key = batch[:key].call(object, context)
419
- opts[:batch_loaders].get(point, self).remember(key, container)
420
- container[point.name] = nil # Reserve attribute place in resulted hash. We will set correct value later
421
- end
422
- end
423
185
  end
424
186
 
425
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.1
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-25 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