serega 0.42.0 → 0.51.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 (129) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +124 -1207
  3. data/VERSION +1 -1
  4. data/lib/serega/attribute/serializer_reference.rb +27 -0
  5. data/lib/serega/attribute/value_code.rb +63 -0
  6. data/lib/serega/attribute.rb +173 -140
  7. data/lib/serega/attribute_normalizer.rb +236 -391
  8. data/lib/serega/attribute_normalizers/batch.rb +45 -0
  9. data/lib/serega/attribute_normalizers/block.rb +81 -0
  10. data/lib/serega/attribute_normalizers/delegate.rb +34 -0
  11. data/lib/serega/attribute_normalizers/hash_access.rb +37 -0
  12. data/lib/serega/attribute_normalizers/preloads.rb +47 -0
  13. data/lib/serega/attribute_value_resolvers/base.rb +35 -0
  14. data/lib/serega/attribute_value_resolvers/batch.rb +18 -54
  15. data/lib/serega/attribute_value_resolvers/callable.rb +52 -0
  16. data/lib/serega/attribute_value_resolvers/const.rb +12 -24
  17. data/lib/serega/attribute_value_resolvers/delegate.rb +27 -28
  18. data/lib/serega/attribute_value_resolvers/hash_access.rb +49 -52
  19. data/lib/serega/attribute_value_resolvers/keyword.rb +17 -25
  20. data/lib/serega/attribute_values.rb +32 -0
  21. data/lib/serega/batch_loader.rb +37 -0
  22. data/lib/serega/conditions.rb +81 -0
  23. data/lib/serega/config.rb +148 -148
  24. data/lib/serega/engine/pulls.rb +51 -0
  25. data/lib/serega/engine/run.rb +101 -0
  26. data/lib/serega/errors.rb +60 -8
  27. data/lib/serega/formatters/callable.rb +44 -0
  28. data/lib/serega/formatters/method.rb +55 -0
  29. data/lib/serega/formatters.rb +27 -0
  30. data/lib/serega/plan/cache.rb +119 -0
  31. data/lib/serega/plan/depth_limit.rb +36 -0
  32. data/lib/serega/plan/per_ractor_cache.rb +28 -0
  33. data/lib/serega/plan/point.rb +67 -0
  34. data/lib/serega/plan/presets.rb +116 -0
  35. data/lib/serega/plan/selection.rb +94 -0
  36. data/lib/serega/plan/selection_planner.rb +50 -0
  37. data/lib/serega/plan.rb +92 -133
  38. data/lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb +13 -80
  39. data/lib/serega/plugins/activerecord_preloads/lib/preloader.rb +8 -75
  40. data/lib/serega/plugins/context_metadata/context_metadata.rb +40 -123
  41. data/lib/serega/plugins/metadata/check_params.rb +80 -0
  42. data/lib/serega/plugins/metadata/meta_attribute.rb +53 -76
  43. data/lib/serega/plugins/metadata/metadata.rb +38 -175
  44. data/lib/serega/plugins/root/root.rb +94 -235
  45. data/lib/serega/plugins.rb +16 -49
  46. data/lib/serega/presenter.rb +81 -19
  47. data/lib/serega/result_builder.rb +68 -0
  48. data/lib/serega/result_code/attribute_point_code.rb +117 -0
  49. data/lib/serega/result_code/point_code.rb +54 -0
  50. data/lib/serega/result_code/relation_point_code.rb +112 -0
  51. data/lib/serega/result_code.rb +159 -0
  52. data/lib/serega/shareable.rb +52 -0
  53. data/lib/serega/source_group.rb +156 -0
  54. data/lib/serega/utils/camel_case.rb +22 -0
  55. data/lib/serega/utils/collection_detector.rb +20 -13
  56. data/lib/serega/utils/deep_merge.rb +26 -0
  57. data/lib/serega/utils/enum_deep_dup.rb +10 -16
  58. data/lib/serega/utils/enum_deep_freeze.rb +8 -14
  59. data/lib/serega/utils/method_signature.rb +6 -15
  60. data/lib/serega/utils/parse_string_selection.rb +63 -0
  61. data/lib/serega/utils/ractor_local.rb +36 -0
  62. data/lib/serega/utils/ruby_literal.rb +28 -0
  63. data/lib/serega/utils/serialized_attribute_error.rb +60 -20
  64. data/lib/serega/utils/to_hash.rb +5 -26
  65. data/lib/serega/validations/attribute/check_block.rb +3 -15
  66. data/lib/serega/validations/attribute/check_name.rb +4 -15
  67. data/lib/serega/validations/attribute/check_opt_base_serializer.rb +3 -15
  68. data/lib/serega/validations/attribute/check_opt_batch.rb +26 -56
  69. data/lib/serega/validations/attribute/check_opt_condition.rb +55 -0
  70. data/lib/serega/validations/attribute/check_opt_delegate.rb +10 -63
  71. data/lib/serega/validations/attribute/check_opt_format.rb +36 -0
  72. data/lib/serega/validations/attribute/check_opt_hash_access.rb +11 -59
  73. data/lib/serega/validations/attribute/check_opt_hide.rb +3 -14
  74. data/lib/serega/validations/attribute/check_opt_many.rb +18 -20
  75. data/lib/serega/validations/attribute/check_opt_method.rb +3 -25
  76. data/lib/serega/validations/attribute/check_opt_preload.rb +3 -26
  77. data/lib/serega/validations/attribute/check_opt_preset.rb +34 -0
  78. data/lib/serega/validations/attribute/check_opt_selection.rb +41 -0
  79. data/lib/serega/validations/attribute/check_opt_serializer.rb +4 -18
  80. data/lib/serega/validations/attribute/check_opt_value.rb +3 -21
  81. data/lib/serega/validations/check_attribute_params.rb +92 -71
  82. data/lib/serega/validations/check_batch_loader_params.rb +43 -62
  83. data/lib/serega/validations/check_definition_selection.rb +21 -0
  84. data/lib/serega/validations/check_formatter.rb +80 -0
  85. data/lib/serega/validations/check_preset_params.rb +103 -0
  86. data/lib/serega/validations/check_relation_selection.rb +31 -0
  87. data/lib/serega/validations/check_selection.rb +89 -0
  88. data/lib/serega/validations/check_serialize_keywords.rb +23 -0
  89. data/lib/serega/validations/utils/check_allowed_keys.rb +3 -13
  90. data/lib/serega/validations/utils/check_hash_access_value.rb +44 -0
  91. data/lib/serega/validations/utils/check_opt_is_bool.rb +3 -13
  92. data/lib/serega/validations/utils/check_opt_is_hash.rb +3 -13
  93. data/lib/serega/validations/utils/check_opt_is_string_or_symbol.rb +3 -13
  94. data/lib/serega/version.rb +1 -4
  95. data/lib/serega.rb +525 -390
  96. data/sig/serega/plugins.rbs +49 -0
  97. data/sig/serega.rbs +158 -0
  98. metadata +64 -40
  99. data/lib/serega/data_builder.rb +0 -54
  100. data/lib/serega/engine/level.rb +0 -59
  101. data/lib/serega/engine/level_queue.rb +0 -56
  102. data/lib/serega/engine/loader.rb +0 -70
  103. data/lib/serega/helpers/serializer_class_helper.rb +0 -19
  104. data/lib/serega/object_serializer.rb +0 -125
  105. data/lib/serega/plan_point.rb +0 -165
  106. data/lib/serega/plugins/camel_case/camel_case.rb +0 -213
  107. data/lib/serega/plugins/depth_limit/depth_limit.rb +0 -206
  108. data/lib/serega/plugins/explicit_many_option/explicit_many_option.rb +0 -69
  109. data/lib/serega/plugins/explicit_many_option/validations/check_opt_many.rb +0 -38
  110. data/lib/serega/plugins/formatters/formatters.rb +0 -322
  111. data/lib/serega/plugins/if/if.rb +0 -287
  112. data/lib/serega/plugins/if/validations/check_opt_if.rb +0 -72
  113. data/lib/serega/plugins/if/validations/check_opt_if_value.rb +0 -77
  114. data/lib/serega/plugins/if/validations/check_opt_unless.rb +0 -72
  115. data/lib/serega/plugins/if/validations/check_opt_unless_value.rb +0 -77
  116. data/lib/serega/plugins/metadata/validations/check_block.rb +0 -47
  117. data/lib/serega/plugins/metadata/validations/check_opt_const.rb +0 -39
  118. data/lib/serega/plugins/metadata/validations/check_opt_hide_empty.rb +0 -35
  119. data/lib/serega/plugins/metadata/validations/check_opt_hide_nil.rb +0 -35
  120. data/lib/serega/plugins/metadata/validations/check_opt_value.rb +0 -61
  121. data/lib/serega/plugins/metadata/validations/check_opts.rb +0 -59
  122. data/lib/serega/plugins/metadata/validations/check_path.rb +0 -50
  123. data/lib/serega/plugins/string_modifiers/parse_string_modifiers.rb +0 -111
  124. data/lib/serega/plugins/string_modifiers/string_modifiers.rb +0 -63
  125. data/lib/serega/utils/symbol_name.rb +0 -39
  126. data/lib/serega/validations/attribute/check_opt_const.rb +0 -40
  127. data/lib/serega/validations/check_initiate_params.rb +0 -57
  128. data/lib/serega/validations/check_serialize_params.rb +0 -54
  129. data/lib/serega/validations/initiate/check_modifiers.rb +0 -93
@@ -1,83 +1,64 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- # @private
5
- module SeregaValidations
4
+ module SeregaValidations # :nodoc:
6
5
  #
7
6
  # Batch loader parameters validators
8
7
  #
9
- # @private
10
- class CheckBatchLoaderParams
11
- #
12
- # batch_loader parameters validation instance methods
13
- #
14
- # @private
15
- module InstanceMethods
16
- # @return [Symbol] validated batch_loader name
17
- attr_reader :name
8
+ class CheckBatchLoaderParams # :nodoc:
9
+ # Validated batch_loader name
10
+ attr_reader :name
18
11
 
19
- # @return [nil, #call] validated batch_loader value or block
20
- attr_reader :batch_loader
12
+ # Validated batch_loader value or block
13
+ attr_reader :batch_loader
21
14
 
22
- # Instantiates batch loader params checker
23
- # @param name [Symbol, String] Batch loader name
24
- # @param batch_loader [Proc, #call] Batch loader
25
- def initialize(name, batch_loader)
26
- @name = name
27
- @batch_loader = batch_loader
28
- end
29
-
30
- #
31
- # Checks batch loader parameters
32
- #
33
- # @raise [SeregaError] SeregaError that batch loader has invalid arguments
34
- #
35
- # @return [void]
36
- #
37
- def validate
38
- check_name
39
- check_loader
40
- end
15
+ # Instantiates batch loader params checker
16
+ def initialize(name, batch_loader)
17
+ @name = name
18
+ @batch_loader = batch_loader
19
+ end
41
20
 
42
- private
21
+ # Checks batch loader parameters
22
+ def validate
23
+ check_name
24
+ check_loader
25
+ end
43
26
 
44
- def check_name
45
- raise SeregaError, name_type_error if !name.is_a?(Symbol) && !name.is_a?(String)
46
- end
27
+ private
47
28
 
48
- def check_loader
49
- check_batch_loader_type
50
- check_batch_loader_args
51
- end
29
+ def check_name
30
+ raise SeregaError, name_type_error if !name.is_a?(Symbol) && !name.is_a?(String)
31
+ end
52
32
 
53
- def check_batch_loader_type
54
- raise SeregaError, type_error if !batch_loader.is_a?(Proc) && !batch_loader.respond_to?(:call)
55
- end
33
+ def check_loader
34
+ check_batch_loader_type
35
+ check_batch_loader_args
36
+ end
56
37
 
57
- def check_batch_loader_args
58
- signature = SeregaUtils::MethodSignature.call(batch_loader, pos_limit: 2, keyword_args: [:ctx])
59
- raise SeregaError, arguments_error unless %w[1 2 1_ctx].include?(signature)
60
- end
38
+ def check_batch_loader_type
39
+ raise SeregaError, type_error if !batch_loader.is_a?(Proc) && !batch_loader.respond_to?(:call)
40
+ end
61
41
 
62
- def name_type_error
63
- "Batch loader name must be a Symbol or String"
64
- end
42
+ def check_batch_loader_args
43
+ signature = SeregaUtils::MethodSignature.call(batch_loader, pos_limit: 2, keyword_args: [:ctx])
44
+ raise SeregaError, arguments_error unless %w[1 2 1_ctx].include?(signature)
45
+ end
65
46
 
66
- def type_error
67
- "Batch loader value must be a Proc or respond to #call"
68
- end
47
+ def name_type_error
48
+ "Batch loader name must be a Symbol or String"
49
+ end
69
50
 
70
- def arguments_error
71
- <<~ERR.strip
72
- Batch loader arguments should have one of this signatures:
73
- - (objects) # one argument
74
- - (objects, :ctx) # one argument and one :ctx keyword argument
75
- ERR
76
- end
51
+ def type_error
52
+ "Batch loader value must be a Proc or respond to #call"
77
53
  end
78
54
 
79
- include InstanceMethods
80
- extend Serega::SeregaHelpers::SerializerClassHelper
55
+ def arguments_error
56
+ <<~ERR.strip
57
+ Batch loader arguments should have one of this signatures:
58
+ - (objects) # one argument
59
+ - (objects, :ctx) # one argument and one :ctx keyword argument
60
+ ERR
61
+ end
81
62
  end
82
63
  end
83
64
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaValidations # :nodoc:
5
+ #
6
+ # Field selection validation of a preset or a relation when it is
7
+ # defined. It skips the nested fields of relations with a serializer name
8
+ # or a Proc, as that serializer may be defined later.
9
+ #
10
+ class CheckDefinitionSelection < CheckSelection # :nodoc:
11
+ private
12
+
13
+ def validate_nested(name, attribute, nested_fields)
14
+ serializer = attribute.initials[:opts][:serializer]
15
+ return if serializer.is_a?(String) || serializer.is_a?(Proc)
16
+
17
+ super
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaValidations # :nodoc:
5
+ #
6
+ # Validator for formatters defined with `Serega.formatter` or given
7
+ # directly as the attribute :format option
8
+ #
9
+ class CheckFormatter # :nodoc:
10
+ class << self
11
+ # Checks the formatter is a callable with valid parameters, a method
12
+ # name, or an Array of a method name and its arguments. The attribute
13
+ # :format option also accepts a Hash with one of them as :use and the
14
+ # :allow_nil option.
15
+ def call(formatter_name, formatter)
16
+ if formatter.is_a?(Hash) && !formatter_name
17
+ Utils::CheckAllowedKeys.call(formatter, %i[use allow_nil], :formatter)
18
+ Utils::CheckOptIsBool.call(formatter, :allow_nil)
19
+ formatter = formatter[:use]
20
+ end
21
+
22
+ check_value(formatter_name, formatter)
23
+ end
24
+
25
+ private
26
+
27
+ def check_value(formatter_name, formatter)
28
+ return if formatter.is_a?(Symbol)
29
+ return if formatter.is_a?(Array) && formatter.first.is_a?(Symbol)
30
+ raise SeregaError, invalid_formatter(formatter_name) unless formatter.respond_to?(:call)
31
+
32
+ signature = SeregaUtils::MethodSignature.call(formatter, pos_limit: 2, keyword_args: [:ctx])
33
+ raise SeregaError, signature_error unless valid_signature?(signature)
34
+ end
35
+
36
+ def valid_signature?(signature)
37
+ case signature
38
+ when "1" # (value)
39
+ true
40
+ when "2" # (value, context)
41
+ true
42
+ when "1_ctx" # (value, :ctx)
43
+ true
44
+ else
45
+ false
46
+ end
47
+ end
48
+
49
+ def invalid_formatter(formatter_name)
50
+ return invalid_format_option unless formatter_name
51
+
52
+ <<~ERROR.strip
53
+ Invalid formatter #{formatter_name.inspect}. A formatter must be one of:
54
+ - a callable, for example ->(value) { value.round(2) }
55
+ - a method name, for example :to_s
56
+ - an Array of a method name and its arguments, for example [:round, 2]
57
+ ERROR
58
+ end
59
+
60
+ def invalid_format_option
61
+ <<~ERROR.strip
62
+ Invalid attribute option :format. It must be a formatter name or one of:
63
+ - a callable, for example ->(value) { value.round(2) }
64
+ - an Array of a method name and its arguments, for example [:round, 2]
65
+ - a Hash with one of them or a method name as :use and the :allow_nil option, for example {use: :to_s, allow_nil: true}
66
+ ERROR
67
+ end
68
+
69
+ def signature_error
70
+ <<~ERROR.strip
71
+ Invalid formatter parameters, valid parameters signatures:
72
+ - (value) # one positional parameter
73
+ - (value, context) # two positional parameters
74
+ - (value, :ctx) # one positional parameter and :ctx keyword
75
+ ERROR
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaValidations # :nodoc:
5
+ #
6
+ # Validations of the parameters of `Serega.preset`. Nested fields of a
7
+ # relation with a serializer name or a Proc are not checked: that
8
+ # serializer may be defined later.
9
+ #
10
+ class CheckPresetParams # :nodoc:
11
+ # Initializes the preset params validator
12
+ def initialize(serializer_class, name, preset, only, with, except)
13
+ @serializer_class = serializer_class
14
+ @name = name
15
+ @preset = preset
16
+ @only = only
17
+ @with = with
18
+ @except = except
19
+ end
20
+
21
+ # Raises an error for an invalid name, an invalid `:preset` option,
22
+ # presets that include each other, invalid field selection options and
23
+ # a preset without a field selection, PresetNotExist for an undefined
24
+ # included preset, and AttributeNotExist for unknown attributes
25
+ def validate
26
+ check_name
27
+ names = included_names
28
+ check_included_presets(names)
29
+ check_selection(names)
30
+ end
31
+
32
+ private
33
+
34
+ def check_name
35
+ return if @name.is_a?(Symbol) || @name.is_a?(String)
36
+
37
+ raise SeregaError, "Preset name must be a Symbol or a String, #{@name.inspect} provided"
38
+ end
39
+
40
+ def included_names
41
+ return FROZEN_EMPTY_ARRAY if @preset.nil?
42
+
43
+ names = @preset.is_a?(Array) ? @preset : [@preset]
44
+ check_preset_option(names)
45
+ names.map(&:to_sym)
46
+ end
47
+
48
+ def check_preset_option(names)
49
+ return if names.all? { |name| name.is_a?(Symbol) || name.is_a?(String) }
50
+
51
+ raise SeregaError, "Invalid option :preset => #{@preset.inspect}. Must be a Symbol, a String, or an Array of them"
52
+ end
53
+
54
+ def check_included_presets(names)
55
+ check_defined(names)
56
+ check_cycle(names, [@name.to_sym])
57
+ end
58
+
59
+ def check_defined(names)
60
+ names.each { |included_name| SeregaPlan::Presets.fetch_definition(@serializer_class, included_name) }
61
+ end
62
+
63
+ # Raises an error when the included presets, or the presets they
64
+ # include, include the new preset. `path` lists the presets from the
65
+ # new one, its first item, to the included ones. `visited` keeps the
66
+ # presets already walked, so each preset is walked once.
67
+ def check_cycle(names, path, visited = Set.new)
68
+ names.each do |included_name|
69
+ included_path = [*path, included_name]
70
+ raise SeregaError, "Presets of #{@serializer_class} include each other: #{included_path.join(" -> ")}" if included_name == path.first
71
+ next unless visited.add?(included_name)
72
+
73
+ check_cycle(@serializer_class.presets[included_name][:presets], included_path, visited)
74
+ end
75
+ end
76
+
77
+ def parse_option(key, value)
78
+ SeregaPlan::SelectionPlanner.parse_option(value)
79
+ rescue SeregaError
80
+ raise SeregaError, "Invalid :#{key} => #{value.inspect} in preset :#{@name}. Must be a Symbol, a String, an Array or a Hash of attribute names"
81
+ end
82
+
83
+ def check_selection(names)
84
+ selection = {only: parse_option(:only, @only), with: parse_option(:with, @with), except: parse_option(:except, @except)}
85
+ check_not_empty(names, selection)
86
+ check_attributes(selection)
87
+ end
88
+
89
+ def check_attributes(selection)
90
+ CheckDefinitionSelection.new.call(@serializer_class, selection[:only], selection[:with], selection[:except])
91
+ rescue AttributeNotExist => error
92
+ attributes = error.attributes
93
+ raise AttributeNotExist.new("Preset :#{@name} names not existing attributes: #{attributes.join(", ")}", error.serializer, attributes)
94
+ end
95
+
96
+ def check_not_empty(names, selection)
97
+ return unless names.empty? && SeregaPlan::SelectionPlanner.empty?(selection)
98
+
99
+ raise SeregaError, "Preset :#{@name} must select fields with :only, :with, :except or :preset"
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaValidations # :nodoc:
5
+ #
6
+ # Checks the attributes that the `only`, `with` and `except` options of
7
+ # a relation name
8
+ #
9
+ class CheckRelationSelection # :nodoc:
10
+ class << self
11
+ # Checks the options of a relation when its plan is built, for a
12
+ # serializer given as a String or a Proc, or defined with a block. A
13
+ # serializer class is checked when the attribute is defined.
14
+ def call(attribute, serializer)
15
+ return if attribute.initials[:opts][:serializer].is_a?(Class)
16
+
17
+ check(attribute.name, serializer, attribute.selection, CheckSelection)
18
+ end
19
+
20
+ # Raises AttributeNotExist that names the relation for attributes
21
+ # the selection names and the serializer does not have
22
+ def check(name, serializer, selection, validator)
23
+ validator.new.call(serializer, selection[:only], selection[:with], selection[:except])
24
+ rescue AttributeNotExist => error
25
+ attributes = error.attributes
26
+ raise AttributeNotExist.new("Relation :#{name} names not existing attributes: #{attributes.join(", ")}", error.serializer, attributes)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaValidations # :nodoc:
5
+ #
6
+ # Raises AttributeNotExist for the attributes that the field selection
7
+ # options name and the serializer does not have
8
+ #
9
+ class CheckSelection # :nodoc:
10
+ #
11
+ # Instantiates the field selection validator
12
+ #
13
+ def initialize
14
+ @parents_names = nil
15
+ @error_attributes = nil
16
+ end
17
+
18
+ # Validates provided fields names are existing attributes
19
+ def call(serializer_class, only, with, except)
20
+ validate(serializer_class, only)
21
+ validate(serializer_class, with)
22
+ validate(serializer_class, except)
23
+
24
+ raise_errors(serializer_class) if any_error?
25
+ end
26
+
27
+ private
28
+
29
+ def validate(serializer_class, fields)
30
+ fields.each do |name, nested_fields|
31
+ attribute = serializer_class && serializer_class.attributes[name]
32
+
33
+ # Save error when no attribute with checked name exists
34
+ unless attribute
35
+ save_error(name)
36
+ next
37
+ end
38
+
39
+ # Return when attribute has no nested fields
40
+ next if nested_fields.empty?
41
+
42
+ validate_nested(name, attribute, nested_fields)
43
+ end
44
+ end
45
+
46
+ # Validates the nested fields of an attribute with the attributes of
47
+ # its serializer
48
+ def validate_nested(name, attribute, nested_fields)
49
+ with_parent_name(name) do
50
+ validate(attribute.serializer, nested_fields)
51
+ end
52
+ end
53
+
54
+ def parents_names
55
+ @parents_names ||= []
56
+ end
57
+
58
+ def with_parent_name(name)
59
+ parents_names << name
60
+ yield
61
+ parents_names.pop
62
+ end
63
+
64
+ def error_attributes
65
+ @error_attributes ||= []
66
+ end
67
+
68
+ def save_error(name)
69
+ error_attributes << build_full_attribute_name(*parents_names, name)
70
+ end
71
+
72
+ def build_full_attribute_name(*names)
73
+ head, *nested = *names
74
+ result = +head.to_s # names are symbols, we need not frozen string
75
+ nested.each { |nested_name| result << "(" << nested_name.to_s }
76
+ nested.each { result << ")" }
77
+ result
78
+ end
79
+
80
+ def raise_errors(serializer_class)
81
+ raise Serega::AttributeNotExist.new("Not existing attributes: #{error_attributes.join(", ")}", serializer_class, error_attributes)
82
+ end
83
+
84
+ def any_error?
85
+ !@error_attributes.nil?
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaValidations # :nodoc:
5
+ #
6
+ # Validator for the :context and :many serialization options
7
+ #
8
+ class CheckSerializeKeywords # :nodoc:
9
+ class << self
10
+ # Checks the context is a Hash and many is a boolean, when given
11
+ def call(context, many)
12
+ unless context.nil? || context.is_a?(Hash)
13
+ raise SeregaError, "Invalid option :context => #{context.inspect}. Must have a Hash value"
14
+ end
15
+
16
+ return if many.nil? || many.equal?(true) || many.equal?(false)
17
+
18
+ raise SeregaError, "Invalid option :many => #{many.inspect}. Must have a boolean value"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,26 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- # @private
5
- module SeregaValidations
4
+ module SeregaValidations # :nodoc:
6
5
  #
7
6
  # Validations Utilities
8
7
  #
9
- # @private
10
- module Utils
8
+ module Utils # :nodoc:
11
9
  #
12
10
  # Utility to check all hash keys are allowed
13
11
  #
14
- # @private
15
- class CheckAllowedKeys
12
+ class CheckAllowedKeys # :nodoc:
16
13
  # Checks hash keys are allowed
17
- #
18
- # @param opts [Hash] Some options Hash
19
- # @param allowed_keys [Array] Allowed hash keys
20
- #
21
- # @raise [Serega::SeregaError] error when any hash key is not allowed
22
- #
23
- # @return [void]
24
14
  def self.call(opts, allowed_keys, parameter_name)
25
15
  opts.each_key do |key|
26
16
  next if allowed_keys.include?(key)
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaValidations # :nodoc:
5
+ module Utils # :nodoc:
6
+ #
7
+ # Validator of a hash access value: the attribute :hash_access option and
8
+ # the delegate :to_hash_access and :hash_access sub-options
9
+ #
10
+ class CheckHashAccessValue # :nodoc:
11
+ class << self
12
+ # Checks that the `key` value is a Boolean, a Symbol mode or a Hash
13
+ # with :mode and :allow_missing_key
14
+ def call(opts, key)
15
+ return unless opts.key?(key)
16
+
17
+ value = opts[key]
18
+ case value
19
+ when true, false then nil
20
+ when Symbol then check_mode(value)
21
+ when Hash
22
+ CheckAllowedKeys.call(value, %i[mode allow_missing_key], key)
23
+ check_mode(value[:mode]) if value.key?(:mode)
24
+ CheckOptIsBool.call(value, :allow_missing_key)
25
+ else
26
+ raise SeregaError,
27
+ "Invalid option #{key.inspect} => #{value.inspect}." \
28
+ " It must be a Boolean, a Symbol mode (:symbol, :string)" \
29
+ " or a Hash with :mode and :allow_missing_key keys"
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def check_mode(mode)
36
+ return if AttributeValueResolvers::HashAccessResolver::MODES.include?(mode)
37
+
38
+ raise SeregaError, "Invalid :hash_access mode #{mode.inspect}. Allowed modes: :symbol, :string"
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,23 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- # @private
5
- module SeregaValidations
6
- # @private
7
- module Utils
4
+ module SeregaValidations # :nodoc:
5
+ module Utils # :nodoc:
8
6
  #
9
7
  # Utility to check hash key value is boolean
10
8
  #
11
- # @private
12
- class CheckOptIsBool
9
+ class CheckOptIsBool # :nodoc:
13
10
  # Checks hash key has boolean value
14
- #
15
- # @param opts [Hash] Some options Hash
16
- # @param key [Object] Hash key
17
- #
18
- # @raise [Serega::SeregaError] error when provided key exists and value is not boolean
19
- #
20
- # @return [void]
21
11
  def self.call(opts, key)
22
12
  return unless opts.key?(key)
23
13
 
@@ -1,23 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- # @private
5
- module SeregaValidations
6
- # @private
7
- module Utils
4
+ module SeregaValidations # :nodoc:
5
+ module Utils # :nodoc:
8
6
  #
9
7
  # Utility to check hash key value is Hash
10
8
  #
11
- # @private
12
- class CheckOptIsHash
9
+ class CheckOptIsHash # :nodoc:
13
10
  # Checks hash key value is Hash
14
- #
15
- # @param opts [Hash] Some options Hash
16
- # @param key [Object] Hash key
17
- #
18
- # @raise [Serega::SeregaError] error when provided key exists and value is not a Hash
19
- #
20
- # @return [void]
21
11
  def self.call(opts, key)
22
12
  return unless opts.key?(key)
23
13
 
@@ -1,23 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- # @private
5
- module SeregaValidations
6
- # @private
7
- module Utils
4
+ module SeregaValidations # :nodoc:
5
+ module Utils # :nodoc:
8
6
  #
9
7
  # Utility to check hash key value is String or Symbol
10
8
  #
11
- # @private
12
- class CheckOptIsStringOrSymbol
9
+ class CheckOptIsStringOrSymbol # :nodoc:
13
10
  # Checks hash key has String or Symbol value
14
- #
15
- # @param opts [Hash] Some options Hash
16
- # @param key [Object] Hash key
17
- #
18
- # @raise [Serega::SeregaError] error when provided key exists and value is not String or Symbol
19
- #
20
- # @return [void]
21
11
  def self.call(opts, key)
22
12
  return unless opts.key?(key)
23
13
 
@@ -1,9 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- # Serega gem version
5
- #
6
- # @return [String] SemVer gem version
7
- #
4
+ # Version of the gem
8
5
  VERSION = File.read(File.join(File.dirname(__FILE__), "../../VERSION")).strip
9
6
  end