serega 0.15.0 → 0.17.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 (51) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +121 -22
  3. data/VERSION +1 -1
  4. data/lib/serega/attribute.rb +5 -5
  5. data/lib/serega/attribute_normalizer.rb +29 -11
  6. data/lib/serega/config.rb +1 -1
  7. data/lib/serega/object_serializer.rb +1 -1
  8. data/lib/serega/plan.rb +11 -11
  9. data/lib/serega/plan_point.rb +5 -5
  10. data/lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb +1 -1
  11. data/lib/serega/plugins/batch/batch.rb +15 -15
  12. data/lib/serega/plugins/batch/lib/batch_config.rb +11 -8
  13. data/lib/serega/plugins/batch/lib/modules/attribute_normalizer.rb +26 -11
  14. data/lib/serega/plugins/batch/lib/validations/check_batch_opt_key.rb +5 -35
  15. data/lib/serega/plugins/batch/lib/validations/check_batch_opt_loader.rb +5 -35
  16. data/lib/serega/plugins/batch/lib/validations/check_opt_batch.rb +2 -2
  17. data/lib/serega/plugins/camel_case/camel_case.rb +195 -0
  18. data/lib/serega/plugins/depth_limit/depth_limit.rb +185 -0
  19. data/lib/serega/plugins/explicit_many_option/explicit_many_option.rb +1 -1
  20. data/lib/serega/plugins/formatters/formatters.rb +88 -14
  21. data/lib/serega/plugins/if/if.rb +47 -23
  22. data/lib/serega/plugins/if/validations/check_opt_if.rb +4 -36
  23. data/lib/serega/plugins/if/validations/check_opt_if_value.rb +7 -39
  24. data/lib/serega/plugins/if/validations/check_opt_unless.rb +4 -45
  25. data/lib/serega/plugins/if/validations/check_opt_unless_value.rb +7 -39
  26. data/lib/serega/plugins/metadata/meta_attribute.rb +21 -5
  27. data/lib/serega/plugins/metadata/metadata.rb +22 -13
  28. data/lib/serega/plugins/metadata/validations/check_block.rb +10 -10
  29. data/lib/serega/plugins/metadata/validations/check_opt_const.rb +38 -0
  30. data/lib/serega/plugins/metadata/validations/check_opt_value.rb +61 -0
  31. data/lib/serega/plugins/metadata/validations/check_opts.rb +24 -10
  32. data/lib/serega/plugins/preloads/lib/modules/attribute_normalizer.rb +1 -1
  33. data/lib/serega/plugins/preloads/lib/preload_paths.rb +12 -5
  34. data/lib/serega/plugins/preloads/lib/preloads_constructor.rb +1 -1
  35. data/lib/serega/plugins/preloads/preloads.rb +12 -12
  36. data/lib/serega/plugins/root/root.rb +1 -1
  37. data/lib/serega/plugins/string_modifiers/string_modifiers.rb +1 -1
  38. data/lib/serega/utils/params_count.rb +50 -0
  39. data/lib/serega/utils/to_hash.rb +1 -1
  40. data/lib/serega/validations/attribute/check_block.rb +4 -5
  41. data/lib/serega/validations/attribute/check_opt_const.rb +1 -1
  42. data/lib/serega/validations/attribute/check_opt_delegate.rb +9 -4
  43. data/lib/serega/validations/attribute/{check_opt_key.rb → check_opt_method.rb} +8 -8
  44. data/lib/serega/validations/attribute/check_opt_value.rb +17 -13
  45. data/lib/serega/validations/check_attribute_params.rb +3 -2
  46. data/lib/serega/validations/check_initiate_params.rb +1 -1
  47. data/lib/serega/validations/check_serialize_params.rb +1 -1
  48. data/lib/serega/validations/utils/check_allowed_keys.rb +4 -2
  49. data/lib/serega/validations/utils/check_extra_keyword_arg.rb +33 -0
  50. data/lib/serega.rb +26 -11
  51. metadata +10 -4
@@ -10,31 +10,45 @@ class Serega
10
10
  class CheckOpts
11
11
  class << self
12
12
  #
13
- # Validates attribute options
13
+ # Validates meta_attribute options
14
14
  # Checks used options are allowed and then checks options values.
15
15
  #
16
16
  # @param opts [Hash] Attribute options
17
- # @param attribute_keys [Array<Symbol>] Allowed options keys
17
+ # @param block [Proc] Attribute block
18
+ # @param allowed_keys [Array<Symbol>] Allowed options keys
18
19
  #
19
20
  # @raise [SeregaError] when attribute has invalid options
20
21
  #
21
22
  # @return [void]
22
23
  #
23
- def call(opts, attribute_keys)
24
+ def call(opts, block, allowed_keys)
25
+ check_allowed_options_keys(opts, allowed_keys)
26
+ check_each_opt(opts, block)
27
+ check_any_value_provided(opts, block)
28
+ end
29
+
30
+ private
31
+
32
+ def check_allowed_options_keys(opts, allowed_keys)
24
33
  opts.each_key do |key|
25
- next if attribute_keys.include?(key.to_sym)
34
+ next if allowed_keys.include?(key.to_sym)
26
35
 
27
- raise SeregaError, "Invalid option #{key.inspect}. Allowed options are: #{attribute_keys.map(&:inspect).join(", ")}"
36
+ allowed = allowed_keys.map(&:inspect).join(", ")
37
+ raise SeregaError, "Invalid option #{key.inspect}. Allowed options are: #{allowed}"
28
38
  end
29
-
30
- check_each_opt(opts)
31
39
  end
32
40
 
33
- private
34
-
35
- def check_each_opt(opts)
41
+ def check_each_opt(opts, block)
36
42
  CheckOptHideEmpty.call(opts)
37
43
  CheckOptHideNil.call(opts)
44
+ CheckOptValue.call(opts, block)
45
+ CheckOptConst.call(opts, block)
46
+ end
47
+
48
+ def check_any_value_provided(opts, block)
49
+ return if opts.key?(:const) || opts.key?(:value) || block
50
+
51
+ raise SeregaError, "Please provide block argument or add :value or :const option"
38
52
  end
39
53
  end
40
54
  end
@@ -36,7 +36,7 @@ class Serega
36
36
  if preloads_provided
37
37
  opts[:preload]
38
38
  elsif opts.key?(:serializer) && self.class.serializer_class.config.preloads.auto_preload_attributes_with_serializer
39
- key
39
+ method_name
40
40
  elsif opts.key?(:delegate) && self.class.serializer_class.config.preloads.auto_preload_attributes_with_delegate
41
41
  opts[:delegate].fetch(:to)
42
42
  end
@@ -22,18 +22,25 @@ class Serega
22
22
  #
23
23
  # Transforms user provided preloads to array of paths
24
24
  #
25
- # @param value [Array,Hash,String,Symbol,nil,false] preloads
25
+ # @param preloads [Array,Hash,String,Symbol,nil,false] association(s) to preload
26
26
  #
27
27
  # @return [Hash] preloads transformed to hash
28
28
  #
29
- def call(preloads, path = [], result = [])
30
- preloads = FormatUserPreloads.call(preloads)
29
+ def call(preloads)
30
+ formatted_preloads = FormatUserPreloads.call(preloads)
31
+ return FROZEN_EMPTY_ARRAY if formatted_preloads.empty?
31
32
 
32
- preloads.each do |key, nested_preloads|
33
+ paths(formatted_preloads, [], [])
34
+ end
35
+
36
+ private
37
+
38
+ def paths(formatted_preloads, path, result)
39
+ formatted_preloads.each do |key, nested_preloads|
33
40
  path << key
34
41
  result << path.dup
35
42
 
36
- call(nested_preloads, path, result)
43
+ paths(nested_preloads, path, result)
37
44
  path.pop
38
45
  end
39
46
 
@@ -11,7 +11,7 @@ class Serega
11
11
  #
12
12
  # Constructs preloads hash for given attributes plan
13
13
  #
14
- # @param plan [Array<Serega::PlanPoint>] Serialization plan
14
+ # @param plan [Array<SeregaPlanPoint>] Serialization plan
15
15
  #
16
16
  # @return [Hash]
17
17
  #
@@ -15,7 +15,7 @@ class Serega
15
15
  #
16
16
  # This options are very handy if you want to forget about finding preloads manually.
17
17
  #
18
- # Preloads can be disabled with `preload: false` attribute option option.
18
+ # Preloads can be disabled with `preload: false` attribute option.
19
19
  # Also automatically added preloads can be overwritten with manually specified `preload: :another_value`.
20
20
  #
21
21
  # Some examples, **please read comments in the code below**
@@ -79,17 +79,17 @@ class Serega
79
79
  # @return [void]
80
80
  #
81
81
  def self.load_plugin(serializer_class, **_opts)
82
- require_relative "./lib/format_user_preloads"
83
- require_relative "./lib/modules/attribute"
84
- require_relative "./lib/modules/attribute_normalizer"
85
- require_relative "./lib/modules/check_attribute_params"
86
- require_relative "./lib/modules/config"
87
- require_relative "./lib/modules/plan_point"
88
- require_relative "./lib/preload_paths"
89
- require_relative "./lib/preloads_config"
90
- require_relative "./lib/preloads_constructor"
91
- require_relative "./validations/check_opt_preload"
92
- require_relative "./validations/check_opt_preload_path"
82
+ require_relative "lib/format_user_preloads"
83
+ require_relative "lib/modules/attribute"
84
+ require_relative "lib/modules/attribute_normalizer"
85
+ require_relative "lib/modules/check_attribute_params"
86
+ require_relative "lib/modules/config"
87
+ require_relative "lib/modules/plan_point"
88
+ require_relative "lib/preload_paths"
89
+ require_relative "lib/preloads_config"
90
+ require_relative "lib/preloads_constructor"
91
+ require_relative "validations/check_opt_preload"
92
+ require_relative "validations/check_opt_preload_path"
93
93
 
94
94
  serializer_class.include(InstanceMethods)
95
95
  serializer_class::SeregaAttribute.include(AttributeInstanceMethods)
@@ -127,7 +127,7 @@ class Serega
127
127
  # @option opts [Symbol, String, nil] :one root for single-object serialization
128
128
  # @option opts [Symbol, String, nil] :many root for many-objects serialization
129
129
  #
130
- # @return [Serega::SeregaPlugins::Root::RootConfig] RootConfig object
130
+ # @return [SeregaPlugins::Root::RootConfig] RootConfig object
131
131
  #
132
132
  def initialize(opts)
133
133
  @opts = opts
@@ -18,7 +18,7 @@ class Serega
18
18
  #
19
19
  def self.load_plugin(serializer_class, **_opts)
20
20
  serializer_class.include(InstanceMethods)
21
- require_relative "./parse_string_modifiers"
21
+ require_relative "parse_string_modifiers"
22
22
  end
23
23
 
24
24
  #
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ #
5
+ # Utilities
6
+ #
7
+ module SeregaUtils
8
+ #
9
+ # Utility to count regular parameters of callable object
10
+ #
11
+ class ParamsCount
12
+ NO_NAMED_REST_PARAM = [:rest].freeze
13
+ private_constant :NO_NAMED_REST_PARAM
14
+
15
+ class << self
16
+ #
17
+ # Count parameters for callable object
18
+ #
19
+ # @param object [#call] callable object
20
+ #
21
+ # @return [Integer] count of regular parameters
22
+ #
23
+ def call(object, max_count:)
24
+ # Procs (but not lambdas) can accept all provided parameters
25
+ parameters = object.is_a?(Proc) ? object.parameters : object.method(:call).parameters
26
+ return 1 if parameters[0] == NO_NAMED_REST_PARAM
27
+ return max_count if object.is_a?(Proc) && !object.lambda?
28
+
29
+ count = 0
30
+
31
+ # If all we have is no-name *rest parameters, then we assume we need to provide
32
+ # 1 argument. It is now always correct, but in serialization context it's most common that
33
+ # only one argument is needed.
34
+ parameters.each do |parameter|
35
+ next if parameter == NO_NAMED_REST_PARAM # Workaround for procs like :odd?.to_proc
36
+ param_type = parameter[0]
37
+
38
+ case param_type
39
+ when :req then count += 1
40
+ when :opt then count += 1 if count < max_count
41
+ when :rest then count += max_count - count if max_count > count
42
+ end # else :opt, :keyreq, :key, :keyrest, :block - do nothing
43
+ end
44
+
45
+ count
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -31,7 +31,7 @@ class Serega
31
31
  when NilClass, FalseClass then nil_to_hash(value)
32
32
  when String then string_to_hash(value)
33
33
  when Symbol then symbol_to_hash(value)
34
- else raise SeregaError, "Cant convert #{value.class} class object to hash"
34
+ else raise SeregaError, "Can't convert #{value.class} class object to hash"
35
35
  end
36
36
  end
37
37
 
@@ -14,7 +14,6 @@ class Serega
14
14
  #
15
15
  # Checks block parameter provided with attribute.
16
16
  # Must have up to two arguments - object and context.
17
- # It should not have any *rest or **key arguments
18
17
  #
19
18
  # @example without arguments
20
19
  # attribute(:email) { CONSTANT_EMAIL }
@@ -40,14 +39,14 @@ class Serega
40
39
  private
41
40
 
42
41
  def check_block(block)
43
- params = block.parameters
44
- return if (params.count <= 2) && params.all? { |par| par[0] == :opt }
42
+ SeregaValidations::Utils::CheckExtraKeywordArg.call(block, "block")
43
+ params_count = SeregaUtils::ParamsCount.call(block, max_count: 2)
45
44
 
46
- raise SeregaError, block_error
45
+ raise SeregaError, block_error if params_count > 2
47
46
  end
48
47
 
49
48
  def block_error
50
- "Block can have maximum two regular parameters (no **keyword or *array args)"
49
+ "Block can have maximum two parameters (object, context)"
51
50
  end
52
51
  end
53
52
  end
@@ -26,7 +26,7 @@ class Serega
26
26
  private
27
27
 
28
28
  def check_usage_with_other_params(opts, block)
29
- raise SeregaError, "Option :const can not be used together with option :key" if opts.key?(:key)
29
+ raise SeregaError, "Option :const can not be used together with option :method" if opts.key?(:method)
30
30
  raise SeregaError, "Option :const can not be used together with option :value" if opts.key?(:value)
31
31
  raise SeregaError, "Option :const can not be used together with block" if block
32
32
  end
@@ -32,8 +32,9 @@ class Serega
32
32
 
33
33
  delegate_opts = opts[:delegate]
34
34
  check_opt_delegate_to(delegate_opts)
35
- check_opt_delegate_key(delegate_opts)
35
+ check_opt_delegate_method(delegate_opts)
36
36
  check_opt_delegate_allow_nil(delegate_opts)
37
+ check_opt_delegate_extra_opts(delegate_opts)
37
38
  end
38
39
 
39
40
  def check_opt_delegate_to(delegate_opts)
@@ -43,16 +44,20 @@ class Serega
43
44
  Utils::CheckOptIsStringOrSymbol.call(delegate_opts, :to)
44
45
  end
45
46
 
46
- def check_opt_delegate_key(delegate_opts)
47
- Utils::CheckOptIsStringOrSymbol.call(delegate_opts, :key)
47
+ def check_opt_delegate_method(delegate_opts)
48
+ Utils::CheckOptIsStringOrSymbol.call(delegate_opts, :method)
48
49
  end
49
50
 
50
51
  def check_opt_delegate_allow_nil(delegate_opts)
51
52
  Utils::CheckOptIsBool.call(delegate_opts, :allow_nil)
52
53
  end
53
54
 
55
+ def check_opt_delegate_extra_opts(delegate_opts)
56
+ Utils::CheckAllowedKeys.call(delegate_opts, %i[to method allow_nil], :delegate)
57
+ end
58
+
54
59
  def check_usage_with_other_params(opts, block)
55
- raise SeregaError, "Option :delegate can not be used together with option :key" if opts.key?(:key)
60
+ raise SeregaError, "Option :delegate can not be used together with option :method" if opts.key?(:method)
56
61
  raise SeregaError, "Option :delegate can not be used together with option :const" if opts.key?(:const)
57
62
  raise SeregaError, "Option :delegate can not be used together with option :value" if opts.key?(:value)
58
63
  raise SeregaError, "Option :delegate can not be used together with block" if block
@@ -4,12 +4,12 @@ class Serega
4
4
  module SeregaValidations
5
5
  module Attribute
6
6
  #
7
- # Attribute `:key` option validator
7
+ # Attribute `:method` option validator
8
8
  #
9
- class CheckOptKey
9
+ class CheckOptMethod
10
10
  class << self
11
11
  #
12
- # Checks attribute :key option
12
+ # Checks attribute :method option
13
13
  #
14
14
  # @param opts [Hash] Attribute options
15
15
  #
@@ -18,18 +18,18 @@ class Serega
18
18
  # @return [void]
19
19
  #
20
20
  def call(opts, block = nil)
21
- return unless opts.key?(:key)
21
+ return unless opts.key?(:method)
22
22
 
23
23
  check_usage_with_other_params(opts, block)
24
- Utils::CheckOptIsStringOrSymbol.call(opts, :key)
24
+ Utils::CheckOptIsStringOrSymbol.call(opts, :method)
25
25
  end
26
26
 
27
27
  private
28
28
 
29
29
  def check_usage_with_other_params(opts, block)
30
- raise SeregaError, "Option :key can not be used together with option :const" if opts.key?(:const)
31
- raise SeregaError, "Option :key can not be used together with option :value" if opts.key?(:value)
32
- raise SeregaError, "Option :key can not be used together with block" if block
30
+ raise SeregaError, "Option :method can not be used together with option :const" if opts.key?(:const)
31
+ raise SeregaError, "Option :method can not be used together with option :value" if opts.key?(:value)
32
+ raise SeregaError, "Option :method can not be used together with block" if block
33
33
  end
34
34
  end
35
35
  end
@@ -21,33 +21,37 @@ class Serega
21
21
  return unless opts.key?(:value)
22
22
 
23
23
  check_usage_with_other_params(opts, block)
24
- check_proc(opts[:value])
24
+
25
+ check_value(opts[:value])
25
26
  end
26
27
 
27
28
  private
28
29
 
29
30
  def check_usage_with_other_params(opts, block)
30
- raise SeregaError, "Option :value can not be used together with option :key" if opts.key?(:key)
31
+ raise SeregaError, "Option :value can not be used together with option :method" if opts.key?(:method)
31
32
  raise SeregaError, "Option :value can not be used together with option :const" if opts.key?(:const)
32
33
  raise SeregaError, "Option :value can not be used together with block" if block
33
34
  end
34
35
 
35
- def check_proc(value)
36
- raise SeregaError, value_error unless value.is_a?(Proc)
36
+ def check_value(value)
37
+ check_value_type(value)
38
+
39
+ SeregaValidations::Utils::CheckExtraKeywordArg.call(value, ":value option")
40
+ params_count = SeregaUtils::ParamsCount.call(value, max_count: 2)
37
41
 
38
- params = value.parameters
42
+ raise SeregaError, params_count_error if params_count > 2
43
+ end
39
44
 
40
- if value.lambda?
41
- return if (params.count == 2) && params.all? { |par| par[0] == :req }
42
- elsif (params.count <= 2) && params.all? { |par| par[0] == :opt }
43
- return
44
- end
45
+ def check_value_type(value)
46
+ raise SeregaError, type_error if !value.is_a?(Proc) && !value.respond_to?(:call)
47
+ end
45
48
 
46
- raise SeregaError, value_error
49
+ def type_error
50
+ "Option :value value must be a Proc or respond to #call"
47
51
  end
48
52
 
49
- def value_error
50
- "Option :value must be a Proc that is able to accept two parameters (no **keyword or *array args)"
53
+ def params_count_error
54
+ "Option :value value can have maximum 2 parameters (object, context)"
51
55
  end
52
56
  end
53
57
  end
@@ -55,15 +55,16 @@ class Serega
55
55
  # Patched in:
56
56
  # - plugin :batch (checks :batch option)
57
57
  # - plugin :context_metadata (checks context metadata option which is :meta by default)
58
+ # - plugin :formatters (checks :format option)
58
59
  # - plugin :if (checks :if, :if_value, :unless, :unless_value options)
59
60
  # - plugin :preloads (checks :preload option)
60
61
  def check_opts
61
- Utils::CheckAllowedKeys.call(opts, allowed_opts_keys)
62
+ Utils::CheckAllowedKeys.call(opts, allowed_opts_keys, :attribute)
62
63
 
63
64
  Attribute::CheckOptConst.call(opts, block)
64
65
  Attribute::CheckOptDelegate.call(opts, block)
65
66
  Attribute::CheckOptHide.call(opts)
66
- Attribute::CheckOptKey.call(opts, block)
67
+ Attribute::CheckOptMethod.call(opts, block)
67
68
  Attribute::CheckOptMany.call(opts)
68
69
  Attribute::CheckOptSerializer.call(opts)
69
70
  Attribute::CheckOptValue.call(opts, block)
@@ -35,7 +35,7 @@ class Serega
35
35
  private
36
36
 
37
37
  def check_allowed_keys
38
- Utils::CheckAllowedKeys.call(opts, serializer_class.config.initiate_keys)
38
+ Utils::CheckAllowedKeys.call(opts, serializer_class.config.initiate_keys, :initiate)
39
39
  end
40
40
 
41
41
  def check_modifiers
@@ -33,7 +33,7 @@ class Serega
33
33
  private
34
34
 
35
35
  def check_opts
36
- Utils::CheckAllowedKeys.call(opts, serializer_class.config.serialize_keys)
36
+ Utils::CheckAllowedKeys.call(opts, serializer_class.config.serialize_keys, :serialize)
37
37
 
38
38
  Utils::CheckOptIsHash.call(opts, :context)
39
39
  Utils::CheckOptIsBool.call(opts, :many)
@@ -18,11 +18,13 @@ class Serega
18
18
  # @raise [Serega::SeregaError] error when any hash key is not allowed
19
19
  #
20
20
  # @return [void]
21
- def self.call(opts, allowed_keys)
21
+ def self.call(opts, allowed_keys, parameter_name)
22
22
  opts.each_key do |key|
23
23
  next if allowed_keys.include?(key)
24
24
 
25
- raise SeregaError, "Invalid option #{key.inspect}. Allowed options are: #{allowed_keys.map(&:inspect).join(", ")}"
25
+ raise SeregaError,
26
+ "Invalid #{parameter_name} option #{key.inspect}." \
27
+ " Allowed options are: #{allowed_keys.map(&:inspect).sort.join(", ")}"
26
28
  end
27
29
  end
28
30
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaValidations
5
+ #
6
+ # Validations Utilities
7
+ #
8
+ module Utils
9
+ #
10
+ # Utility to check that callable object has no required keyword arguments
11
+ #
12
+ class CheckExtraKeywordArg
13
+ # Checks hash keys are allowed
14
+ #
15
+ # @param callable [#call] Callable object
16
+ # @param callable_description [Symbol] Callable object description
17
+ #
18
+ # @raise [Serega::SeregaError] error if callable accepts required keyword argument
19
+ #
20
+ # @return [void]
21
+ def self.call(callable, callable_description)
22
+ parameters = callable.is_a?(Proc) ? callable.parameters : callable.method(:call).parameters
23
+
24
+ parameters.each do |parameter|
25
+ next unless parameter[0] == :keyreq
26
+
27
+ raise Serega::SeregaError, "Invalid #{callable_description}. It should not have any required keyword arguments"
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
data/lib/serega.rb CHANGED
@@ -17,6 +17,7 @@ require_relative "serega/errors"
17
17
  require_relative "serega/helpers/serializer_class_helper"
18
18
  require_relative "serega/utils/enum_deep_dup"
19
19
  require_relative "serega/utils/enum_deep_freeze"
20
+ require_relative "serega/utils/params_count"
20
21
  require_relative "serega/utils/symbol_name"
21
22
  require_relative "serega/utils/to_hash"
22
23
  require_relative "serega/json/adapter"
@@ -24,6 +25,7 @@ require_relative "serega/json/adapter"
24
25
  require_relative "serega/attribute"
25
26
  require_relative "serega/attribute_normalizer"
26
27
  require_relative "serega/validations/utils/check_allowed_keys"
28
+ require_relative "serega/validations/utils/check_extra_keyword_arg"
27
29
  require_relative "serega/validations/utils/check_opt_is_bool"
28
30
  require_relative "serega/validations/utils/check_opt_is_hash"
29
31
  require_relative "serega/validations/utils/check_opt_is_string_or_symbol"
@@ -32,8 +34,8 @@ require_relative "serega/validations/attribute/check_name"
32
34
  require_relative "serega/validations/attribute/check_opt_const"
33
35
  require_relative "serega/validations/attribute/check_opt_hide"
34
36
  require_relative "serega/validations/attribute/check_opt_delegate"
35
- require_relative "serega/validations/attribute/check_opt_key"
36
37
  require_relative "serega/validations/attribute/check_opt_many"
38
+ require_relative "serega/validations/attribute/check_opt_method"
37
39
  require_relative "serega/validations/attribute/check_opt_serializer"
38
40
  require_relative "serega/validations/attribute/check_opt_value"
39
41
  require_relative "serega/validations/initiate/check_modifiers"
@@ -172,7 +174,20 @@ class Serega
172
174
  new(modifiers_opts).to_h(object, serialize_opts)
173
175
  end
174
176
 
175
- # @see #call
177
+ #
178
+ # Serializes provided object to Hash
179
+ #
180
+ # @param object [Object] Serialized object
181
+ # @param opts [Hash, nil] Serializer modifiers and other instantiating options
182
+ # @option opts [Array, Hash, String, Symbol] :only The only attributes to serialize
183
+ # @option opts [Array, Hash, String, Symbol] :except Attributes to hide
184
+ # @option opts [Array, Hash, String, Symbol] :with Attributes (usually hidden) to serialize additionally
185
+ # @option opts [Boolean] :validate Validates provided modifiers (Default is true)
186
+ # @option opts [Hash] :context Serialization context
187
+ # @option opts [Boolean] :many Set true if provided multiple objects (Default `object.is_a?(Enumerable)`)
188
+ #
189
+ # @return [Hash] Serialization result
190
+ #
176
191
  def to_h(object, opts = nil)
177
192
  call(object, opts)
178
193
  end
@@ -283,8 +298,17 @@ class Serega
283
298
  def initialize(opts = nil)
284
299
  @opts = (opts.nil? || opts.empty?) ? FROZEN_EMPTY_HASH : parse_modifiers(opts)
285
300
  self.class::CheckInitiateParams.new(@opts).validate if opts&.fetch(:check_initiate_params) { config.check_initiate_params }
301
+
302
+ @plan = self.class::SeregaPlan.call(@opts)
286
303
  end
287
304
 
305
+ #
306
+ # Plan for serialization.
307
+ # This plan can be traversed to find serialized attributes and nested attributes.
308
+ #
309
+ # @return [Serega::SeregaPlan] Serialization plan
310
+ attr_reader :plan
311
+
288
312
  #
289
313
  # Serializes provided object to Hash
290
314
  #
@@ -338,15 +362,6 @@ class Serega
338
362
  config.from_json.call(json)
339
363
  end
340
364
 
341
- #
342
- # Plan for serialization.
343
- # This plan can be traversed to find serialized attributes and nested attributes.
344
- #
345
- # @return [Array<Serega::SeregaPlanPoint>] plan
346
- def plan
347
- @plan ||= self.class::SeregaPlan.call(opts)
348
- end
349
-
350
365
  private
351
366
 
352
367
  attr_reader :opts
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.15.0
4
+ version: 0.17.0
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-08-13 00:00:00.000000000 Z
11
+ date: 2023-11-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  JSON Serializer
@@ -58,7 +58,9 @@ files:
58
58
  - lib/serega/plugins/batch/lib/validations/check_batch_opt_key.rb
59
59
  - lib/serega/plugins/batch/lib/validations/check_batch_opt_loader.rb
60
60
  - lib/serega/plugins/batch/lib/validations/check_opt_batch.rb
61
+ - lib/serega/plugins/camel_case/camel_case.rb
61
62
  - lib/serega/plugins/context_metadata/context_metadata.rb
63
+ - lib/serega/plugins/depth_limit/depth_limit.rb
62
64
  - lib/serega/plugins/explicit_many_option/explicit_many_option.rb
63
65
  - lib/serega/plugins/explicit_many_option/validations/check_opt_many.rb
64
66
  - lib/serega/plugins/formatters/formatters.rb
@@ -70,8 +72,10 @@ files:
70
72
  - lib/serega/plugins/metadata/meta_attribute.rb
71
73
  - lib/serega/plugins/metadata/metadata.rb
72
74
  - lib/serega/plugins/metadata/validations/check_block.rb
75
+ - lib/serega/plugins/metadata/validations/check_opt_const.rb
73
76
  - lib/serega/plugins/metadata/validations/check_opt_hide_empty.rb
74
77
  - lib/serega/plugins/metadata/validations/check_opt_hide_nil.rb
78
+ - lib/serega/plugins/metadata/validations/check_opt_value.rb
75
79
  - lib/serega/plugins/metadata/validations/check_opts.rb
76
80
  - lib/serega/plugins/metadata/validations/check_path.rb
77
81
  - lib/serega/plugins/preloads/lib/format_user_preloads.rb
@@ -92,6 +96,7 @@ files:
92
96
  - lib/serega/plugins/string_modifiers/string_modifiers.rb
93
97
  - lib/serega/utils/enum_deep_dup.rb
94
98
  - lib/serega/utils/enum_deep_freeze.rb
99
+ - lib/serega/utils/params_count.rb
95
100
  - lib/serega/utils/symbol_name.rb
96
101
  - lib/serega/utils/to_hash.rb
97
102
  - lib/serega/validations/attribute/check_block.rb
@@ -99,8 +104,8 @@ files:
99
104
  - lib/serega/validations/attribute/check_opt_const.rb
100
105
  - lib/serega/validations/attribute/check_opt_delegate.rb
101
106
  - lib/serega/validations/attribute/check_opt_hide.rb
102
- - lib/serega/validations/attribute/check_opt_key.rb
103
107
  - lib/serega/validations/attribute/check_opt_many.rb
108
+ - lib/serega/validations/attribute/check_opt_method.rb
104
109
  - lib/serega/validations/attribute/check_opt_serializer.rb
105
110
  - lib/serega/validations/attribute/check_opt_value.rb
106
111
  - lib/serega/validations/check_attribute_params.rb
@@ -108,6 +113,7 @@ files:
108
113
  - lib/serega/validations/check_serialize_params.rb
109
114
  - lib/serega/validations/initiate/check_modifiers.rb
110
115
  - lib/serega/validations/utils/check_allowed_keys.rb
116
+ - lib/serega/validations/utils/check_extra_keyword_arg.rb
111
117
  - lib/serega/validations/utils/check_opt_is_bool.rb
112
118
  - lib/serega/validations/utils/check_opt_is_hash.rb
113
119
  - lib/serega/validations/utils/check_opt_is_string_or_symbol.rb
@@ -134,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
140
  - !ruby/object:Gem::Version
135
141
  version: '0'
136
142
  requirements: []
137
- rubygems_version: 3.4.6
143
+ rubygems_version: 3.4.21
138
144
  signing_key:
139
145
  specification_version: 4
140
146
  summary: JSON Serializer