serega 0.51.1 → 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 (44) 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 +2 -2
  30. data/lib/serega/plan/cache.rb +13 -29
  31. data/lib/serega/plan/per_ractor_cache.rb +2 -2
  32. data/lib/serega/plan/point.rb +13 -2
  33. data/lib/serega/plan.rb +1 -1
  34. data/lib/serega/plugins/context_metadata/context_metadata.rb +1 -1
  35. data/lib/serega/plugins/root/root.rb +10 -10
  36. data/lib/serega/plugins.rb +13 -0
  37. data/lib/serega/result_code/relation_point_code.rb +3 -2
  38. data/lib/serega/source_group.rb +1 -1
  39. data/lib/serega/validations/attribute/check_block.rb +4 -7
  40. data/lib/serega/validations/attribute/check_opt_hash_access.rb +2 -3
  41. data/lib/serega/validations/check_batch_loader_params.rb +2 -1
  42. data/lib/serega.rb +30 -26
  43. data/sig/serega.rbs +10 -8
  44. metadata +23 -1
@@ -9,21 +9,12 @@ class Serega
9
9
  # the plans.
10
10
  #
11
11
  class Cache # :nodoc:
12
- # Cached plan, whether its parameters were validated, and the
13
- # parameters as provided
14
- CACHED_PLAN = Struct.new(:plan, :validated, :param1, :param2, :param3, :param4) do
12
+ # Cached plan and the parameters as provided
13
+ CACHED_PLAN = Struct.new(:plan, :param1, :param2, :param3, :param4) do
15
14
  # Whether the given parameters equal the cached ones
16
15
  def same_params?(param1, param2, param3, param4)
17
16
  param1.eql?(self.param1) && param2.eql?(self.param2) && param3.eql?(self.param3) && param4.eql?(self.param4)
18
17
  end
19
-
20
- # Validates the cached parameters with the planner, once
21
- def validate(planner, serializer_class)
22
- return if validated
23
-
24
- planner.validate(serializer_class, planner.parse(serializer_class, param1, param2, param3, param4))
25
- self.validated = true
26
- end
27
18
  end
28
19
  private_constant :CACHED_PLAN
29
20
 
@@ -46,18 +37,14 @@ class Serega
46
37
  # The plan without parameters is always cached; plans with parameters
47
38
  # are cached by parameters as provided when
48
39
  # `max_cached_plans_per_serializer_count` is positive. Parameters are
49
- # parsed when a plan is built, and validated when
50
- # `check_selection` is true, or nil with
51
- # `config.check_selection` on.
52
- def fetch(param1 = nil, param2 = nil, param3 = nil, param4 = nil, check_selection: nil)
40
+ # parsed and validated when a plan is built.
41
+ def fetch(param1 = nil, param2 = nil, param3 = nil, param4 = nil)
53
42
  return default_plan if param1.nil? && param2.nil? && param3.nil? && param4.nil?
54
43
 
55
- config = @serializer_class.config
56
- check_selection = config.check_selection if check_selection.nil?
57
- max_size = config.max_cached_plans_per_serializer_count
58
- return build(param1, param2, param3, param4, check_selection) if max_size.zero?
44
+ max_size = @serializer_class.config.max_cached_plans_per_serializer_count
45
+ return build(param1, param2, param3, param4) if max_size.zero?
59
46
 
60
- cached_plan(param1, param2, param3, param4, max_size, check_selection)
47
+ cached_plan(param1, param2, param3, param4, max_size)
61
48
  end
62
49
 
63
50
  private
@@ -70,16 +57,13 @@ class Serega
70
57
  # Keeps plans by the key of the parameters, and compares each
71
58
  # parameter with the cached one. Parameters with the key of other
72
59
  # cached parameters replace them.
73
- def cached_plan(param1, param2, param3, param4, max_size, check_selection)
60
+ def cached_plan(param1, param2, param3, param4, max_size)
74
61
  key = cache_key(param1, param2, param3, param4)
75
62
  cached = @plans[key]
76
- if cached&.same_params?(param1, param2, param3, param4)
77
- cached.validate(@planner, @serializer_class) if check_selection
78
- return cached.plan
79
- end
63
+ return cached.plan if cached&.same_params?(param1, param2, param3, param4)
80
64
 
81
- plan = build(param1, param2, param3, param4, check_selection)
82
- @plans[key] = CACHED_PLAN.new(plan, check_selection, *frozen_copy([param1, param2, param3, param4]))
65
+ plan = build(param1, param2, param3, param4)
66
+ @plans[key] = CACHED_PLAN.new(plan, *frozen_copy([param1, param2, param3, param4]))
83
67
  @plans.shift if @plans.length > max_size
84
68
  plan
85
69
  end
@@ -103,11 +87,11 @@ class Serega
103
87
  # rubocop:enable Security/CompoundHash
104
88
  end
105
89
 
106
- def build(param1, param2, param3, param4, check_selection)
90
+ def build(param1, param2, param3, param4)
107
91
  params = @planner.parse(@serializer_class, param1, param2, param3, param4)
108
92
  return default_plan if @planner.empty?(params)
109
93
 
110
- @planner.validate(@serializer_class, params) if check_selection
94
+ @planner.validate(@serializer_class, params)
111
95
  @planner.plan(@serializer_class, params)
112
96
  end
113
97
 
@@ -19,9 +19,9 @@ class Serega
19
19
  # Returns serialization plan from the plans cache of the current Ractor.
20
20
  # Takes the parameters one by one like Cache#fetch: a splat allocates
21
21
  # an Array on every `Serega.new`.
22
- def fetch(param1 = nil, param2 = nil, param3 = nil, param4 = nil, check_selection: nil)
22
+ def fetch(param1 = nil, param2 = nil, param3 = nil, param4 = nil)
23
23
  plan_cache = SeregaUtils::RactorLocal.fetch(@key) { SeregaPlan::Cache.new(@serializer_class, @planner) }
24
- plan_cache.fetch(param1, param2, param3, param4, check_selection: check_selection)
24
+ plan_cache.fetch(param1, param2, param3, param4)
25
25
  end
26
26
  end
27
27
  end
@@ -48,9 +48,20 @@ class Serega
48
48
  names
49
49
  end
50
50
 
51
- # Attribute `many` option
51
+ # Attribute `many` option, or `config.many` of the relation serializer
52
+ # when the attribute has no `many` option
52
53
  def many
53
- attribute.many
54
+ many = attribute.many
55
+ return many unless many.nil? && @child_plan
56
+
57
+ @child_plan.serializer_class.config.many
58
+ end
59
+
60
+ # Whether a nil relation value serializes as `[]`: `config.many` of
61
+ # the relation serializer is true, and the attribute has neither the
62
+ # `:many` nor the `:default` option
63
+ def nil_as_empty_list?
64
+ attribute.many.nil? && many == true && !attribute.initials[:opts].key?(:default)
54
65
  end
55
66
 
56
67
  # Attribute `batch_loaders`
data/lib/serega/plan.rb CHANGED
@@ -59,7 +59,7 @@ class Serega
59
59
  rescue *RACTOR_ISOLATION_ERRORS
60
60
  raise if serializer_class.shareable?
61
61
 
62
- raise SeregaError, "#{serializer_class} can not serialize in other Ractors. Call `make_shareable!` at the end of its definition"
62
+ raise SeregaError, "#{serializer_class} can not serialize in other Ractors\n(hint: call `make_shareable!` at the end of its definition)"
63
63
  # :nocov:
64
64
  end
65
65
 
@@ -10,7 +10,7 @@ class Serega
10
10
  # plugin :context_metadata
11
11
  #
12
12
  # UserSerializer.to_h(user, meta: {version: "1.0.1"})
13
- # # => {data: {username: "batman"}, version: "1.0.1"}
13
+ # # => {data: {username: "gru"}, version: "1.0.1"}
14
14
  # ```
15
15
  #
16
16
  # See [Metadata per serialization](https://github.com/andreyruby/serega/blob/master/docs/root-and-metadata.md#metadata-per-serialization).
@@ -9,8 +9,8 @@ class Serega
9
9
  # plugin :root, root: :users # one key for objects and lists
10
10
  # plugin :root, root_one: :user, root_many: :users # separate keys
11
11
  #
12
- # UserSerializer.to_h(user, root: :user) # => {user: {username: "batman"}}
13
- # UserSerializer.to_h(user, root: nil) # => {username: "batman"}
12
+ # UserSerializer.to_h(user, root: :user) # => {user: {username: "gru"}}
13
+ # UserSerializer.to_h(user, root: nil) # => {username: "gru"}
14
14
  # ```
15
15
  #
16
16
  # See [Root key](https://github.com/andreyruby/serega/blob/master/docs/root-and-metadata.md#root-key).
@@ -38,23 +38,23 @@ class Serega
38
38
 
39
39
  # Class methods the plugin adds to serializers
40
40
  module ClassMethods
41
- def call(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil, **opts) # :nodoc:
42
- new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
41
+ def call(object, only: nil, with: nil, except: nil, preset: nil, context: nil, many: nil, **opts) # :nodoc:
42
+ new(only: only, with: with, except: except, preset: preset)
43
43
  .to_h(object, context: context, many: many, **opts)
44
44
  end
45
45
 
46
- def to_data(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil, **opts) # :nodoc:
47
- new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
46
+ def to_data(object, only: nil, with: nil, except: nil, preset: nil, context: nil, many: nil, **opts) # :nodoc:
47
+ new(only: only, with: with, except: except, preset: preset)
48
48
  .to_data(object, context: context, many: many, **opts)
49
49
  end
50
50
 
51
- def to_struct(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil, **opts) # :nodoc:
52
- new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
51
+ def to_struct(object, only: nil, with: nil, except: nil, preset: nil, context: nil, many: nil, **opts) # :nodoc:
52
+ new(only: only, with: with, except: except, preset: preset)
53
53
  .to_struct(object, context: context, many: many, **opts)
54
54
  end
55
55
 
56
- def to_json(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil, **opts) # :nodoc:
57
- new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
56
+ def to_json(object, only: nil, with: nil, except: nil, preset: nil, context: nil, many: nil, **opts) # :nodoc:
57
+ new(only: only, with: with, except: except, preset: preset)
58
58
  .to_json(object, context: context, many: many, **opts)
59
59
  end
60
60
 
@@ -13,6 +13,9 @@ class Serega
13
13
  activerecord_preloads: :ActiverecordPreloads
14
14
  }.freeze
15
15
 
16
+ # Names of deprecated plugins, replaced by response serializers
17
+ DEPRECATED = %i[root metadata context_metadata].freeze # :nodoc:
18
+
16
19
  class << self
17
20
  # Loads the code of the plugin and returns its module. Raises
18
21
  # SeregaError when the plugin does not exist.
@@ -21,6 +24,16 @@ class Serega
21
24
  require_relative "plugins/#{name}/#{name}"
22
25
  const_get(module_name)
23
26
  end
27
+
28
+ # Warning text for a deprecated plugin, `nil` for the others
29
+ def deprecation_warning(serializer_class, name) # :nodoc:
30
+ return unless DEPRECATED.include?(name)
31
+
32
+ "#{serializer_class}: plugin #{name.inspect} is deprecated. Migrate to a response serializer with data and " \
33
+ "meta attributes: " \
34
+ "https://github.com/andreyruby/serega/blob/master/docs/migrating-from-root-and-metadata.md. " \
35
+ "Set config.deprecation_warnings = false to hide this warning."
36
+ end
24
37
  end
25
38
  end
26
39
  end
@@ -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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey
@@ -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