serega 0.51.0 → 0.52.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.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +48 -21
  3. data/VERSION +1 -1
  4. data/docs/README.md +41 -0
  5. data/docs/attributes.md +248 -0
  6. data/docs/batch-loading.md +207 -0
  7. data/docs/camel-case.md +86 -0
  8. data/docs/conditional-attributes.md +165 -0
  9. data/docs/configuration.md +47 -0
  10. data/docs/context.md +34 -0
  11. data/docs/errors.md +38 -0
  12. data/docs/formatters.md +231 -0
  13. data/docs/hash-records.md +93 -0
  14. data/docs/migrating-from-root-and-metadata.md +50 -0
  15. data/docs/preloads.md +125 -0
  16. data/docs/prepare-initial-objects.md +72 -0
  17. data/docs/presenter.md +87 -0
  18. data/docs/presets.md +174 -0
  19. data/docs/ractors.md +54 -0
  20. data/docs/relations.md +233 -0
  21. data/docs/responses.md +548 -0
  22. data/docs/root-and-metadata.md +110 -0
  23. data/docs/selecting-fields.md +157 -0
  24. data/docs/serializing.md +113 -0
  25. data/docs/testing.md +230 -0
  26. data/lib/serega/attribute_normalizer.rb +26 -8
  27. data/lib/serega/attribute_normalizers/block.rb +3 -3
  28. data/lib/serega/config.rb +28 -13
  29. data/lib/serega/errors.rb +5 -4
  30. data/lib/serega/plan/cache.rb +13 -29
  31. data/lib/serega/plan/depth_limit.rb +27 -8
  32. data/lib/serega/plan/per_ractor_cache.rb +2 -2
  33. data/lib/serega/plan/point.rb +13 -2
  34. data/lib/serega/plan/selection.rb +10 -0
  35. data/lib/serega/plan.rb +7 -3
  36. data/lib/serega/plugins/context_metadata/context_metadata.rb +1 -1
  37. data/lib/serega/plugins/root/root.rb +10 -10
  38. data/lib/serega/plugins.rb +13 -0
  39. data/lib/serega/result_code/relation_point_code.rb +3 -2
  40. data/lib/serega/source_group.rb +2 -2
  41. data/lib/serega/validations/attribute/check_block.rb +4 -7
  42. data/lib/serega/validations/attribute/check_opt_hash_access.rb +2 -3
  43. data/lib/serega/validations/check_batch_loader_params.rb +2 -1
  44. data/lib/serega.rb +54 -46
  45. data/sig/serega.rbs +10 -8
  46. metadata +30 -6
data/lib/serega.rb CHANGED
@@ -12,8 +12,8 @@ require_relative "serega/version"
12
12
  # attribute :email, hide: true
13
13
  # end
14
14
  #
15
- # UserSerializer.to_h(user) # => {first_name: "Bruce"}
16
- # UserSerializer.to_h(user, with: :email) # => {first_name: "Bruce", email: "bruce@wayne.com"}
15
+ # UserSerializer.to_h(user) # => {first_name: "Felonious"}
16
+ # UserSerializer.to_h(user, with: :email) # => {first_name: "Felonious", email: "gru@example.com"}
17
17
  # ```
18
18
  #
19
19
  # See the [guides](https://github.com/andreyruby/serega/blob/master/docs/README.md).
@@ -136,19 +136,22 @@ class Serega
136
136
 
137
137
  #
138
138
  # Turns on a built-in feature by name, with its options. Subclasses get it
139
- # too.
139
+ # too. The deprecated `:root`, `:metadata` and `:context_metadata` plugins
140
+ # print a warning, unless `config.deprecation_warnings` is `false`.
140
141
  #
141
142
  # ```ruby
142
- # plugin :root, root_one: :user, root_many: :users
143
+ # plugin :activerecord_preloads
143
144
  # ```
144
145
  #
145
- # See [Root Key and Metadata](https://github.com/andreyruby/serega/blob/master/docs/root-and-metadata.md).
146
+ # See [Preloads](https://github.com/andreyruby/serega/blob/master/docs/preloads.md).
146
147
  #
147
148
  def plugin(name, **opts)
148
149
  check_unlocked
149
150
  raise SeregaError, "This plugin is already loaded" if plugin_used?(name)
150
151
 
151
152
  plugin = SeregaPlugins.find(name)
153
+ deprecation = config.deprecation_warnings && SeregaPlugins.deprecation_warning(self, name)
154
+ warn(deprecation, uplevel: 1) if deprecation
152
155
  plugin.apply(self, **opts)
153
156
  config.plugins << name
154
157
 
@@ -200,7 +203,7 @@ class Serega
200
203
  rescue *RACTOR_ISOLATION_ERRORS
201
204
  raise SeregaError, @unshareable_reason if @unshareable_reason
202
205
 
203
- raise SeregaError, "#{self} can not serialize in other Ractors. Call `make_shareable!` at the end of its definition"
206
+ raise SeregaError, "#{self} can not serialize in other Ractors\n(hint: call `make_shareable!` at the end of its definition)"
204
207
  # :nocov:
205
208
  end
206
209
 
@@ -286,7 +289,7 @@ class Serega
286
289
  # UserSerializer.to_h(user, preset: :profile, except: :avatar)
287
290
  # ```
288
291
  #
289
- # See [Presets](https://github.com/andreyruby/serega/blob/master/docs/selecting-fields.md#presets).
292
+ # See [Presets](https://github.com/andreyruby/serega/blob/master/docs/presets.md).
290
293
  #
291
294
  def preset(name, preset: nil, only: nil, with: nil, except: nil)
292
295
  check_unlocked
@@ -384,17 +387,20 @@ class Serega
384
387
  #
385
388
  # See [Preloads](https://github.com/andreyruby/serega/blob/master/docs/preloads.md).
386
389
  #
387
- def preload_with(value = nil, &block)
388
- return @preload_with if value.nil? && block.nil?
390
+ def preload_with(value = (omitted = true), &block)
391
+ return @preload_with_handler if omitted && block.nil?
389
392
 
390
393
  check_unlocked
391
- handler = checked_handler(:preload_with, value, block)
394
+ handler = omitted ? block : checked_value_handler(:preload_with, value, block)
392
395
  signature = SeregaUtils::MethodSignature.call(handler, pos_limit: 2)
393
396
  raise SeregaError, "preload_with handler must accept two positional arguments: (objects, preloads)" unless signature == "2"
394
397
 
395
- @preload_with = handler
398
+ @preload_with_handler = handler
396
399
  end
397
400
 
401
+ # The handler registered with `preload_with`
402
+ attr_reader :preload_with_handler # :nodoc:
403
+
398
404
  #
399
405
  # Sets the handler that replaces the objects given to the serializer, for
400
406
  # example ids with records. It runs once per serialization, before Serega
@@ -410,18 +416,21 @@ class Serega
410
416
  #
411
417
  # See [Prepare initial objects](https://github.com/andreyruby/serega/blob/master/docs/prepare-initial-objects.md).
412
418
  #
413
- def prepare_initial_objects(value = nil, &block)
414
- return @prepare_initial_objects if value.nil? && block.nil?
419
+ def prepare_initial_objects(value = (omitted = true), &block)
420
+ return @prepare_initial_objects_handler if omitted && block.nil?
415
421
 
416
422
  check_unlocked
417
- handler = checked_handler(:prepare_initial_objects, value, block)
423
+ handler = omitted ? block : checked_value_handler(:prepare_initial_objects, value, block)
418
424
  signature = SeregaUtils::MethodSignature.call(handler, pos_limit: 2, keyword_args: [:ctx])
419
425
  raise SeregaError, prepare_initial_objects_signature_error unless %w[1 2 1_ctx].include?(signature)
420
426
 
421
427
  @prepare_initial_objects_signature = signature
422
- @prepare_initial_objects = handler
428
+ @prepare_initial_objects_handler = handler
423
429
  end
424
430
 
431
+ # The handler registered with `prepare_initial_objects`
432
+ attr_reader :prepare_initial_objects_handler # :nodoc:
433
+
425
434
  # Signature of the registered prepare_initial_objects handler
426
435
  attr_reader :prepare_initial_objects_signature # :nodoc:
427
436
 
@@ -434,24 +443,22 @@ class Serega
434
443
  #
435
444
  # - `only`, `except`, `with`: fields to serialize, to skip and to add,
436
445
  # see [Selecting fields](https://github.com/andreyruby/serega/blob/master/docs/selecting-fields.md);
437
- # - `check_selection`: `false` skips unknown fields, by default
438
- # `config.check_selection`;
439
446
  # - `context`: data for attribute values and conditions;
440
447
  # - `many`: `true` serializes a list, `false` one object. By default an
441
448
  # `Enumerable` that is not a `Hash` or `Struct` is a list.
442
449
  #
443
450
  # ```ruby
444
- # UserSerializer.to_h(user) # => {first_name: "Bruce"}
445
- # UserSerializer.to_h([user], with: :email) # => [{first_name: "Bruce", email: "bruce@wayne.com"}]
451
+ # UserSerializer.to_h(user) # => {first_name: "Felonious"}
452
+ # UserSerializer.to_h([user], with: :email) # => [{first_name: "Felonious", email: "gru@example.com"}]
446
453
  # UserSerializer.to_h(user, context: {admin: true})
447
454
  # ```
448
455
  #
449
456
  # See [Serializing](https://github.com/andreyruby/serega/blob/master/docs/serializing.md).
450
457
  #
451
- def call(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil)
458
+ def call(object, only: nil, with: nil, except: nil, preset: nil, context: nil, many: nil)
452
459
  # The serialization methods list each keyword instead of taking
453
460
  # `**opts`: a `**opts` Hash allocates on every call, even when empty.
454
- new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
461
+ new(only: only, with: with, except: except, preset: preset)
455
462
  .to_h(object, context: context, many: many)
456
463
  end
457
464
 
@@ -462,11 +469,11 @@ class Serega
462
469
  # Serializes like #to_h, to `Data` objects
463
470
  #
464
471
  # ```ruby
465
- # UserSerializer.to_data(user) # => #<data first_name="Bruce">
472
+ # UserSerializer.to_data(user) # => #<data first_name="Felonious">
466
473
  # ```
467
474
  #
468
- def to_data(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil)
469
- new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
475
+ def to_data(object, only: nil, with: nil, except: nil, preset: nil, context: nil, many: nil)
476
+ new(only: only, with: with, except: except, preset: preset)
470
477
  .to_data(object, context: context, many: many)
471
478
  end
472
479
 
@@ -477,11 +484,11 @@ class Serega
477
484
  # Serializes like #to_h, to `Struct` objects
478
485
  #
479
486
  # ```ruby
480
- # UserSerializer.to_struct(user) # => #<struct first_name="Bruce">
487
+ # UserSerializer.to_struct(user) # => #<struct first_name="Felonious">
481
488
  # ```
482
489
  #
483
- def to_struct(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil)
484
- new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
490
+ def to_struct(object, only: nil, with: nil, except: nil, preset: nil, context: nil, many: nil)
491
+ new(only: only, with: with, except: except, preset: preset)
485
492
  .to_struct(object, context: context, many: many)
486
493
  end
487
494
 
@@ -493,11 +500,11 @@ class Serega
493
500
  # result
494
501
  #
495
502
  # ```ruby
496
- # UserSerializer.to_json(user) # => "{\"first_name\":\"Bruce\"}"
503
+ # UserSerializer.to_json(user) # => "{\"first_name\":\"Felonious\"}"
497
504
  # ```
498
505
  #
499
- def to_json(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil)
500
- new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
506
+ def to_json(object, only: nil, with: nil, except: nil, preset: nil, context: nil, many: nil)
507
+ new(only: only, with: with, except: except, preset: preset)
501
508
  .to_json(object, context: context, many: many)
502
509
  end
503
510
 
@@ -601,10 +608,10 @@ class Serega
601
608
  subclass.presets.merge!(presets)
602
609
 
603
610
  # Assign same preload handler
604
- subclass.preload_with(preload_with) if preload_with
611
+ subclass.preload_with(preload_with_handler) if preload_with_handler
605
612
 
606
613
  # Assign same initial objects handler
607
- subclass.prepare_initial_objects(prepare_initial_objects) if prepare_initial_objects
614
+ subclass.prepare_initial_objects(prepare_initial_objects_handler) if prepare_initial_objects_handler
608
615
 
609
616
  # Assign same presenter blocks
610
617
  presenter_blocks.each do |presenter_block|
@@ -628,8 +635,8 @@ class Serega
628
635
  @presenter_key = nil
629
636
  @data_classes = {}
630
637
  @struct_classes = {}
631
- @preload_with = nil
632
- @prepare_initial_objects = nil
638
+ @preload_with_handler = nil
639
+ @prepare_initial_objects_handler = nil
633
640
  @prepare_initial_objects_signature = nil
634
641
  @locked = false
635
642
  @shareable = false
@@ -637,14 +644,12 @@ class Serega
637
644
  @plan_cache = SeregaPlan::Cache.new(self, SeregaPlan::SelectionPlanner)
638
645
  end
639
646
 
640
- # The handler given to `method_name` as a value or a block
641
- def checked_handler(method_name, value, block)
642
- raise SeregaError, "#{method_name} accepts a single callable or a block, not both" if value && block
643
-
644
- handler = value || block
645
- raise SeregaError, "#{method_name} value must be a Proc or respond to #call" unless handler.respond_to?(:call)
647
+ # The handler given to `method_name` as a value, which must come without a block
648
+ def checked_value_handler(method_name, value, block)
649
+ raise SeregaError, "#{method_name} accepts a single callable or a block, not both" if block
650
+ raise SeregaError, "#{method_name} value must be a Proc or respond to #call" unless value.respond_to?(:call)
646
651
 
647
- handler
652
+ value
648
653
  end
649
654
 
650
655
  def prepare_initial_objects_signature_error
@@ -668,13 +673,14 @@ class Serega
668
673
  #
669
674
  # ```ruby
670
675
  # serializer = UserSerializer.new(only: [:first_name])
671
- # serializer.to_h(user) # => {first_name: "Bruce"}
672
- # serializer.to_h(users) # => [{first_name: "Bruce"}, {first_name: "Alfred"}]
676
+ # serializer.to_h(user) # => {first_name: "Felonious"}
677
+ # serializer.to_h(users) # => [{first_name: "Felonious"}, {first_name: "Lucy"}]
673
678
  # ```
674
679
  #
675
- def initialize(only: nil, with: nil, except: nil, preset: nil, check_selection: nil)
676
- @plan = self.class.plan_cache.fetch(only, with, except, preset, check_selection: check_selection)
680
+ def initialize(only: nil, with: nil, except: nil, preset: nil)
681
+ @plan = self.class.plan_cache.fetch(only, with, except, preset)
677
682
  @direct_hash_builder = @plan.direct_hash_builder
683
+ @many = self.class.config.many
678
684
  end
679
685
 
680
686
  # Plan for serialization.
@@ -695,6 +701,7 @@ class Serega
695
701
  def call(object, context: nil, many: nil)
696
702
  # One Hash source of a flat plan skips #serialize_to and the run: 73 ns
697
703
  # and 1 object instead of 136 ns and 3 objects (Ruby 4.0, YJIT).
704
+ many = @many if many.nil?
698
705
  builder = @direct_hash_builder
699
706
  if builder && object && !many && (context.nil? || context.is_a?(Hash))
700
707
  many = object.instance_of?(Array) || SeregaUtils::CollectionDetector.call(object, @plan.collection_classes) if many.nil?
@@ -743,6 +750,7 @@ class Serega
743
750
  # options other than :context and :many.
744
751
  def serialize_to(mode, object, context, many, opts)
745
752
  SeregaValidations::CheckSerializeKeywords.call(context, many)
753
+ many = @many if many.nil?
746
754
  context ||= FROZEN_EMPTY_HASH
747
755
  object = prepare_objects(object, context) if self.class.prepare_initial_objects_signature
748
756
  many = object.instance_of?(Array) || SeregaUtils::CollectionDetector.call(object, @plan.collection_classes) if many.nil?
@@ -750,7 +758,7 @@ class Serega
750
758
  end
751
759
 
752
760
  def prepare_objects(objects, context)
753
- handler = self.class.prepare_initial_objects
761
+ handler = self.class.prepare_initial_objects_handler
754
762
 
755
763
  case self.class.prepare_initial_objects_signature
756
764
  when "1" then handler.call(objects)
data/sig/serega.rbs CHANGED
@@ -54,17 +54,17 @@ class Serega
54
54
 
55
55
  # Results are `untyped`: plugins change their shape. Options that plugins
56
56
  # add at load time, such as `root:`, are not in these signatures.
57
- def call: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?check_selection: bool?, ?context: context?, ?many: bool?) -> untyped
57
+ def call: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?context: context?, ?many: bool?) -> untyped
58
58
  alias to_h call
59
- def to_data: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?check_selection: bool?, ?context: context?, ?many: bool?) -> untyped
60
- def to_struct: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?check_selection: bool?, ?context: context?, ?many: bool?) -> untyped
61
- def to_json: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?check_selection: bool?, ?context: context?, ?many: bool?) -> String
59
+ def to_data: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?context: context?, ?many: bool?) -> untyped
60
+ def to_struct: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?context: context?, ?many: bool?) -> untyped
61
+ def to_json: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?context: context?, ?many: bool?) -> String
62
62
 
63
63
  def make_shareable!: () -> self
64
64
  end
65
65
 
66
66
  module InstanceMethods
67
- def initialize: (?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?check_selection: bool?) -> void
67
+ def initialize: (?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?) -> void
68
68
 
69
69
  def call: (untyped object, ?context: context?, ?many: bool?) -> untyped
70
70
  alias to_h call
@@ -103,14 +103,13 @@ class Serega
103
103
  include SeregaConfigInstanceMethods
104
104
 
105
105
  module SeregaConfigInstanceMethods
106
- def check_selection: () -> bool
107
- def check_selection=: (bool value) -> bool
108
-
109
106
  def check_attribute_name: () -> bool
110
107
  def check_attribute_name=: (bool value) -> bool
111
108
 
112
109
  def require_many: () -> bool
113
110
  def require_many=: (bool value) -> bool
111
+ def many: () -> bool?
112
+ def many=: (bool? value) -> bool?
114
113
 
115
114
  def delegate_default_allow_nil: () -> bool
116
115
  def delegate_default_allow_nil=: (bool value) -> bool
@@ -118,6 +117,9 @@ class Serega
118
117
  def compact: () -> bool
119
118
  def compact=: (bool value) -> bool
120
119
 
120
+ def deprecation_warnings: () -> bool
121
+ def deprecation_warnings=: (bool value) -> bool
122
+
121
123
  def key_transform: () -> (:camel_case | ^(Symbol) -> (Symbol | String) | nil)
122
124
  def key_transform=: (:camel_case | ^(Symbol) -> (Symbol | String) | nil value) -> (:camel_case | ^(Symbol) -> (Symbol | String) | nil)
123
125
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serega
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.51.0
4
+ version: 0.52.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey
@@ -9,9 +9,9 @@ bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
- description: Serializes Ruby objects into Hashes, ready for further conversion to
13
- JSON or other formats, with a simple DSL for selecting fields and solving N+1 queries
14
- through batch loading. No runtime dependencies.
12
+ description: Serializes Ruby objects to Hashes, JSON, Data or Struct. Clients choose
13
+ the fields, and Serega preloads associations and runs batch loaders only for those
14
+ fields, with no N+1 queries. No runtime dependencies.
15
15
  email:
16
16
  - andreyruby@yandex.ru
17
17
  executables: []
@@ -22,6 +22,28 @@ files:
22
22
  - LICENSE.txt
23
23
  - README.md
24
24
  - VERSION
25
+ - docs/README.md
26
+ - docs/attributes.md
27
+ - docs/batch-loading.md
28
+ - docs/camel-case.md
29
+ - docs/conditional-attributes.md
30
+ - docs/configuration.md
31
+ - docs/context.md
32
+ - docs/errors.md
33
+ - docs/formatters.md
34
+ - docs/hash-records.md
35
+ - docs/migrating-from-root-and-metadata.md
36
+ - docs/preloads.md
37
+ - docs/prepare-initial-objects.md
38
+ - docs/presenter.md
39
+ - docs/presets.md
40
+ - docs/ractors.md
41
+ - docs/relations.md
42
+ - docs/responses.md
43
+ - docs/root-and-metadata.md
44
+ - docs/selecting-fields.md
45
+ - docs/serializing.md
46
+ - docs/testing.md
25
47
  - lib/serega.rb
26
48
  - lib/serega/attribute.rb
27
49
  - lib/serega/attribute/serializer_reference.rb
@@ -123,6 +145,7 @@ metadata:
123
145
  source_code_uri: https://github.com/andreyruby/serega
124
146
  documentation_uri: https://andreyruby.github.io/serega/
125
147
  changelog_uri: https://github.com/andreyruby/serega/blob/master/CHANGELOG.md
148
+ bug_tracker_uri: https://github.com/andreyruby/serega/issues
126
149
  rdoc_options:
127
150
  - "--markup"
128
151
  - markdown
@@ -143,7 +166,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
166
  - !ruby/object:Gem::Version
144
167
  version: '0'
145
168
  requirements: []
146
- rubygems_version: 4.0.23
169
+ rubygems_version: 4.0.24
147
170
  specification_version: 4
148
- summary: Ruby Serializer
171
+ summary: 'Fast Ruby serializer: clients choose the fields, Serega loads only what
172
+ they use'
149
173
  test_files: []