serega 0.51.1 → 0.52.1

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 +49 -21
  3. data/VERSION +1 -1
  4. data/docs/README.md +42 -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 +262 -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-rails.md +287 -0
  22. data/docs/responses.md +293 -0
  23. data/docs/root-and-metadata.md +110 -0
  24. data/docs/selecting-fields.md +164 -0
  25. data/docs/serializing.md +113 -0
  26. data/docs/testing.md +230 -0
  27. data/lib/serega/attribute_normalizer.rb +26 -8
  28. data/lib/serega/attribute_normalizers/block.rb +3 -3
  29. data/lib/serega/config.rb +28 -13
  30. data/lib/serega/errors.rb +2 -2
  31. data/lib/serega/plan/cache.rb +13 -29
  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.rb +1 -1
  35. data/lib/serega/plugins/context_metadata/context_metadata.rb +5 -1
  36. data/lib/serega/plugins/metadata/metadata.rb +4 -0
  37. data/lib/serega/plugins/root/root.rb +14 -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 +1 -1
  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 +30 -26
  45. data/sig/serega.rbs +10 -8
  46. metadata +24 -1
@@ -74,12 +74,13 @@ class Serega
74
74
  private
75
75
 
76
76
  # Code that appends the sources of `value_N` to `child_sources_N` and
77
- # gives its pull, as SeregaEngine::Pulls.append does
77
+ # gives its pull, as SeregaEngine::Pulls.append does. A nil value gives
78
+ # the pull 0, an empty list, when the point serializes nil as `[]`.
78
79
  def pull_code
79
80
  index = @index
80
81
  <<~RUBY.chomp
81
82
  #{skip_branch}if value_#{index}.nil?
82
- nil
83
+ #{@point.nil_as_empty_list? ? "0" : "nil"}
83
84
  #{collection_branch}else
84
85
  child_sources_#{index} << value_#{index}
85
86
  #{@point.many ? "1" : "Serega::SeregaEngine::Pulls::SINGLE_SOURCE"}
@@ -108,7 +108,7 @@ class Serega
108
108
 
109
109
  points.each do |point|
110
110
  unless handler
111
- raise SeregaError, "The :preload option requires a preload handler. Register one with `preload_with` (the :activerecord_preloads plugin does this for you)."
111
+ raise SeregaError, "The :preload option requires a preload handler\n(hint: register one with `preload_with`, or load the :activerecord_preloads plugin)"
112
112
  end
113
113
 
114
114
  handler.call(sources, point.preloads)
@@ -10,14 +10,11 @@ class Serega
10
10
  # Attribute `block` parameter validator
11
11
  #
12
12
  class CheckBlock # :nodoc:
13
- # Explains the changed attribute block behavior. Shown when the block
14
- # looks like an old-style value block — it accepts parameters or
15
- # defines no attributes.
13
+ # Error of a block that looks like a value block: it accepts
14
+ # parameters or defines no attributes.
16
15
  ERROR_MESSAGE =
17
- "Attribute block now defines a nested serializer:" \
18
- " it is executed in the context of a new serializer class and must define its attributes." \
19
- " Defining the attribute value with a block is not supported anymore," \
20
- " use the `value: <callable>` option instead."
16
+ "Attribute block defines a nested serializer and must define its attributes\n" \
17
+ "(hint: to compute the attribute value, use the `value: <callable>` option)"
21
18
 
22
19
  class << self
23
20
  # Checks block parameter provided with attribute.
@@ -22,9 +22,8 @@ class Serega
22
22
  return unless opts.key?(:delegate)
23
23
 
24
24
  raise SeregaError,
25
- "Option :hash_access can not be used together with option :delegate." \
26
- " Use the delegate :to_hash_access (intermediate step) and" \
27
- " :hash_access (final step) sub-options instead"
25
+ "Option :hash_access can not be used together with option :delegate\n" \
26
+ "(hint: use the :to_hash_access and :hash_access sub-options of :delegate)"
28
27
  end
29
28
  end
30
29
  end
@@ -56,7 +56,8 @@ class Serega
56
56
  <<~ERR.strip
57
57
  Batch loader arguments should have one of this signatures:
58
58
  - (objects) # one argument
59
- - (objects, :ctx) # one argument and one :ctx keyword argument
59
+ - (objects, ctx) # two arguments, the second is the context
60
+ - (objects, ctx:) # one argument and the :ctx keyword argument
60
61
  ERR
61
62
  end
62
63
  end
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
@@ -440,24 +443,22 @@ class Serega
440
443
  #
441
444
  # - `only`, `except`, `with`: fields to serialize, to skip and to add,
442
445
  # see [Selecting fields](https://github.com/andreyruby/serega/blob/master/docs/selecting-fields.md);
443
- # - `check_selection`: `false` skips unknown fields, by default
444
- # `config.check_selection`;
445
446
  # - `context`: data for attribute values and conditions;
446
447
  # - `many`: `true` serializes a list, `false` one object. By default an
447
448
  # `Enumerable` that is not a `Hash` or `Struct` is a list.
448
449
  #
449
450
  # ```ruby
450
- # UserSerializer.to_h(user) # => {first_name: "Bruce"}
451
- # 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"}]
452
453
  # UserSerializer.to_h(user, context: {admin: true})
453
454
  # ```
454
455
  #
455
456
  # See [Serializing](https://github.com/andreyruby/serega/blob/master/docs/serializing.md).
456
457
  #
457
- 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)
458
459
  # The serialization methods list each keyword instead of taking
459
460
  # `**opts`: a `**opts` Hash allocates on every call, even when empty.
460
- new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
461
+ new(only: only, with: with, except: except, preset: preset)
461
462
  .to_h(object, context: context, many: many)
462
463
  end
463
464
 
@@ -468,11 +469,11 @@ class Serega
468
469
  # Serializes like #to_h, to `Data` objects
469
470
  #
470
471
  # ```ruby
471
- # UserSerializer.to_data(user) # => #<data first_name="Bruce">
472
+ # UserSerializer.to_data(user) # => #<data first_name="Felonious">
472
473
  # ```
473
474
  #
474
- def to_data(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil)
475
- 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)
476
477
  .to_data(object, context: context, many: many)
477
478
  end
478
479
 
@@ -483,11 +484,11 @@ class Serega
483
484
  # Serializes like #to_h, to `Struct` objects
484
485
  #
485
486
  # ```ruby
486
- # UserSerializer.to_struct(user) # => #<struct first_name="Bruce">
487
+ # UserSerializer.to_struct(user) # => #<struct first_name="Felonious">
487
488
  # ```
488
489
  #
489
- def to_struct(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil)
490
- 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)
491
492
  .to_struct(object, context: context, many: many)
492
493
  end
493
494
 
@@ -499,11 +500,11 @@ class Serega
499
500
  # result
500
501
  #
501
502
  # ```ruby
502
- # UserSerializer.to_json(user) # => "{\"first_name\":\"Bruce\"}"
503
+ # UserSerializer.to_json(user) # => "{\"first_name\":\"Felonious\"}"
503
504
  # ```
504
505
  #
505
- def to_json(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil)
506
- 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)
507
508
  .to_json(object, context: context, many: many)
508
509
  end
509
510
 
@@ -672,13 +673,14 @@ class Serega
672
673
  #
673
674
  # ```ruby
674
675
  # serializer = UserSerializer.new(only: [:first_name])
675
- # serializer.to_h(user) # => {first_name: "Bruce"}
676
- # 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"}]
677
678
  # ```
678
679
  #
679
- def initialize(only: nil, with: nil, except: nil, preset: nil, check_selection: nil)
680
- @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)
681
682
  @direct_hash_builder = @plan.direct_hash_builder
683
+ @many = self.class.config.many
682
684
  end
683
685
 
684
686
  # Plan for serialization.
@@ -699,6 +701,7 @@ class Serega
699
701
  def call(object, context: nil, many: nil)
700
702
  # One Hash source of a flat plan skips #serialize_to and the run: 73 ns
701
703
  # and 1 object instead of 136 ns and 3 objects (Ruby 4.0, YJIT).
704
+ many = @many if many.nil?
702
705
  builder = @direct_hash_builder
703
706
  if builder && object && !many && (context.nil? || context.is_a?(Hash))
704
707
  many = object.instance_of?(Array) || SeregaUtils::CollectionDetector.call(object, @plan.collection_classes) if many.nil?
@@ -747,6 +750,7 @@ class Serega
747
750
  # options other than :context and :many.
748
751
  def serialize_to(mode, object, context, many, opts)
749
752
  SeregaValidations::CheckSerializeKeywords.call(context, many)
753
+ many = @many if many.nil?
750
754
  context ||= FROZEN_EMPTY_HASH
751
755
  object = prepare_objects(object, context) if self.class.prepare_initial_objects_signature
752
756
  many = object.instance_of?(Array) || SeregaUtils::CollectionDetector.call(object, @plan.collection_classes) if many.nil?
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.1
4
+ version: 0.52.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey
@@ -22,6 +22,29 @@ 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-rails.md
43
+ - docs/responses.md
44
+ - docs/root-and-metadata.md
45
+ - docs/selecting-fields.md
46
+ - docs/serializing.md
47
+ - docs/testing.md
25
48
  - lib/serega.rb
26
49
  - lib/serega/attribute.rb
27
50
  - lib/serega/attribute/serializer_reference.rb