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.
- checksums.yaml +4 -4
- data/README.md +124 -1207
- data/VERSION +1 -1
- data/lib/serega/attribute/serializer_reference.rb +27 -0
- data/lib/serega/attribute/value_code.rb +63 -0
- data/lib/serega/attribute.rb +173 -140
- data/lib/serega/attribute_normalizer.rb +236 -391
- data/lib/serega/attribute_normalizers/batch.rb +45 -0
- data/lib/serega/attribute_normalizers/block.rb +81 -0
- data/lib/serega/attribute_normalizers/delegate.rb +34 -0
- data/lib/serega/attribute_normalizers/hash_access.rb +37 -0
- data/lib/serega/attribute_normalizers/preloads.rb +47 -0
- data/lib/serega/attribute_value_resolvers/base.rb +35 -0
- data/lib/serega/attribute_value_resolvers/batch.rb +18 -54
- data/lib/serega/attribute_value_resolvers/callable.rb +52 -0
- data/lib/serega/attribute_value_resolvers/const.rb +12 -24
- data/lib/serega/attribute_value_resolvers/delegate.rb +27 -28
- data/lib/serega/attribute_value_resolvers/hash_access.rb +49 -52
- data/lib/serega/attribute_value_resolvers/keyword.rb +17 -25
- data/lib/serega/attribute_values.rb +32 -0
- data/lib/serega/batch_loader.rb +37 -0
- data/lib/serega/conditions.rb +81 -0
- data/lib/serega/config.rb +148 -148
- data/lib/serega/engine/pulls.rb +51 -0
- data/lib/serega/engine/run.rb +101 -0
- data/lib/serega/errors.rb +60 -8
- data/lib/serega/formatters/callable.rb +44 -0
- data/lib/serega/formatters/method.rb +55 -0
- data/lib/serega/formatters.rb +27 -0
- data/lib/serega/plan/cache.rb +119 -0
- data/lib/serega/plan/depth_limit.rb +36 -0
- data/lib/serega/plan/per_ractor_cache.rb +28 -0
- data/lib/serega/plan/point.rb +67 -0
- data/lib/serega/plan/presets.rb +116 -0
- data/lib/serega/plan/selection.rb +94 -0
- data/lib/serega/plan/selection_planner.rb +50 -0
- data/lib/serega/plan.rb +92 -133
- data/lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb +13 -80
- data/lib/serega/plugins/activerecord_preloads/lib/preloader.rb +8 -75
- data/lib/serega/plugins/context_metadata/context_metadata.rb +40 -123
- data/lib/serega/plugins/metadata/check_params.rb +80 -0
- data/lib/serega/plugins/metadata/meta_attribute.rb +53 -76
- data/lib/serega/plugins/metadata/metadata.rb +38 -175
- data/lib/serega/plugins/root/root.rb +94 -235
- data/lib/serega/plugins.rb +16 -49
- data/lib/serega/presenter.rb +81 -19
- data/lib/serega/result_builder.rb +68 -0
- data/lib/serega/result_code/attribute_point_code.rb +117 -0
- data/lib/serega/result_code/point_code.rb +54 -0
- data/lib/serega/result_code/relation_point_code.rb +112 -0
- data/lib/serega/result_code.rb +159 -0
- data/lib/serega/shareable.rb +52 -0
- data/lib/serega/source_group.rb +156 -0
- data/lib/serega/utils/camel_case.rb +22 -0
- data/lib/serega/utils/collection_detector.rb +20 -13
- data/lib/serega/utils/deep_merge.rb +26 -0
- data/lib/serega/utils/enum_deep_dup.rb +10 -16
- data/lib/serega/utils/enum_deep_freeze.rb +8 -14
- data/lib/serega/utils/method_signature.rb +6 -15
- data/lib/serega/utils/parse_string_selection.rb +63 -0
- data/lib/serega/utils/ractor_local.rb +36 -0
- data/lib/serega/utils/ruby_literal.rb +28 -0
- data/lib/serega/utils/serialized_attribute_error.rb +60 -20
- data/lib/serega/utils/to_hash.rb +5 -26
- data/lib/serega/validations/attribute/check_block.rb +3 -15
- data/lib/serega/validations/attribute/check_name.rb +4 -15
- data/lib/serega/validations/attribute/check_opt_base_serializer.rb +3 -15
- data/lib/serega/validations/attribute/check_opt_batch.rb +26 -56
- data/lib/serega/validations/attribute/check_opt_condition.rb +55 -0
- data/lib/serega/validations/attribute/check_opt_delegate.rb +10 -63
- data/lib/serega/validations/attribute/check_opt_format.rb +36 -0
- data/lib/serega/validations/attribute/check_opt_hash_access.rb +11 -59
- data/lib/serega/validations/attribute/check_opt_hide.rb +3 -14
- data/lib/serega/validations/attribute/check_opt_many.rb +18 -20
- data/lib/serega/validations/attribute/check_opt_method.rb +3 -25
- data/lib/serega/validations/attribute/check_opt_preload.rb +3 -26
- data/lib/serega/validations/attribute/check_opt_preset.rb +34 -0
- data/lib/serega/validations/attribute/check_opt_selection.rb +41 -0
- data/lib/serega/validations/attribute/check_opt_serializer.rb +4 -18
- data/lib/serega/validations/attribute/check_opt_value.rb +3 -21
- data/lib/serega/validations/check_attribute_params.rb +92 -71
- data/lib/serega/validations/check_batch_loader_params.rb +43 -62
- data/lib/serega/validations/check_definition_selection.rb +21 -0
- data/lib/serega/validations/check_formatter.rb +80 -0
- data/lib/serega/validations/check_preset_params.rb +103 -0
- data/lib/serega/validations/check_relation_selection.rb +31 -0
- data/lib/serega/validations/check_selection.rb +89 -0
- data/lib/serega/validations/check_serialize_keywords.rb +23 -0
- data/lib/serega/validations/utils/check_allowed_keys.rb +3 -13
- data/lib/serega/validations/utils/check_hash_access_value.rb +44 -0
- data/lib/serega/validations/utils/check_opt_is_bool.rb +3 -13
- data/lib/serega/validations/utils/check_opt_is_hash.rb +3 -13
- data/lib/serega/validations/utils/check_opt_is_string_or_symbol.rb +3 -13
- data/lib/serega/version.rb +1 -4
- data/lib/serega.rb +525 -390
- data/sig/serega/plugins.rbs +49 -0
- data/sig/serega.rbs +158 -0
- metadata +64 -40
- data/lib/serega/data_builder.rb +0 -54
- data/lib/serega/engine/level.rb +0 -59
- data/lib/serega/engine/level_queue.rb +0 -56
- data/lib/serega/engine/loader.rb +0 -70
- data/lib/serega/helpers/serializer_class_helper.rb +0 -19
- data/lib/serega/object_serializer.rb +0 -125
- data/lib/serega/plan_point.rb +0 -165
- data/lib/serega/plugins/camel_case/camel_case.rb +0 -213
- data/lib/serega/plugins/depth_limit/depth_limit.rb +0 -206
- data/lib/serega/plugins/explicit_many_option/explicit_many_option.rb +0 -69
- data/lib/serega/plugins/explicit_many_option/validations/check_opt_many.rb +0 -38
- data/lib/serega/plugins/formatters/formatters.rb +0 -322
- data/lib/serega/plugins/if/if.rb +0 -287
- data/lib/serega/plugins/if/validations/check_opt_if.rb +0 -72
- data/lib/serega/plugins/if/validations/check_opt_if_value.rb +0 -77
- data/lib/serega/plugins/if/validations/check_opt_unless.rb +0 -72
- data/lib/serega/plugins/if/validations/check_opt_unless_value.rb +0 -77
- data/lib/serega/plugins/metadata/validations/check_block.rb +0 -47
- data/lib/serega/plugins/metadata/validations/check_opt_const.rb +0 -39
- data/lib/serega/plugins/metadata/validations/check_opt_hide_empty.rb +0 -35
- data/lib/serega/plugins/metadata/validations/check_opt_hide_nil.rb +0 -35
- data/lib/serega/plugins/metadata/validations/check_opt_value.rb +0 -61
- data/lib/serega/plugins/metadata/validations/check_opts.rb +0 -59
- data/lib/serega/plugins/metadata/validations/check_path.rb +0 -50
- data/lib/serega/plugins/string_modifiers/parse_string_modifiers.rb +0 -111
- data/lib/serega/plugins/string_modifiers/string_modifiers.rb +0 -63
- data/lib/serega/utils/symbol_name.rb +0 -39
- data/lib/serega/validations/attribute/check_opt_const.rb +0 -40
- data/lib/serega/validations/check_initiate_params.rb +0 -57
- data/lib/serega/validations/check_serialize_params.rb +0 -54
- data/lib/serega/validations/initiate/check_modifiers.rb +0 -93
|
@@ -1,78 +1,30 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class Serega
|
|
4
|
-
#
|
|
5
|
-
|
|
6
|
-
# @private
|
|
7
|
-
module Attribute
|
|
4
|
+
module SeregaValidations # :nodoc:
|
|
5
|
+
module Attribute # :nodoc:
|
|
8
6
|
#
|
|
9
7
|
# Attribute `:hash_access` option validator
|
|
10
8
|
#
|
|
11
|
-
#
|
|
12
|
-
class CheckOptHashAccess
|
|
9
|
+
class CheckOptHashAccess # :nodoc:
|
|
13
10
|
class << self
|
|
14
|
-
#
|
|
15
11
|
# Checks attribute :hash_access option
|
|
16
|
-
#
|
|
17
|
-
# @param opts [Hash] Attribute options
|
|
18
|
-
#
|
|
19
|
-
# @raise [SeregaError] Attribute validation error
|
|
20
|
-
#
|
|
21
|
-
# @return [void]
|
|
22
|
-
#
|
|
23
12
|
def call(opts)
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
value = opts[:hash_access]
|
|
27
|
-
check_value(value)
|
|
28
|
-
check_usage_with_other_params(opts) if value
|
|
13
|
+
Utils::CheckHashAccessValue.call(opts, :hash_access)
|
|
14
|
+
check_usage_with_delegate(opts) if opts[:hash_access]
|
|
29
15
|
end
|
|
30
16
|
|
|
31
17
|
private
|
|
32
18
|
|
|
33
|
-
def check_value(value)
|
|
34
|
-
case value
|
|
35
|
-
when true, false then nil
|
|
36
|
-
when Symbol then check_mode(value)
|
|
37
|
-
when Hash
|
|
38
|
-
Utils::CheckAllowedKeys.call(value, %i[mode allow_missing_key], :hash_access)
|
|
39
|
-
check_mode(value[:mode]) if value.key?(:mode)
|
|
40
|
-
check_allow_missing_key(value[:allow_missing_key]) if value.key?(:allow_missing_key)
|
|
41
|
-
else
|
|
42
|
-
raise SeregaError,
|
|
43
|
-
"Invalid option :hash_access => #{value.inspect}." \
|
|
44
|
-
" It must be a Boolean, a Symbol mode (:symbol, :string)" \
|
|
45
|
-
" or a Hash with :mode and :allow_missing_key keys"
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def check_mode(mode)
|
|
50
|
-
return if AttributeValueResolvers::HashAccessResolver::MODES.include?(mode)
|
|
51
|
-
|
|
52
|
-
raise SeregaError, "Invalid :hash_access mode #{mode.inspect}. Allowed modes: :symbol, :string"
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
def check_allow_missing_key(allow_missing_key)
|
|
56
|
-
return if allow_missing_key == true || allow_missing_key == false
|
|
57
|
-
|
|
58
|
-
raise SeregaError, "Invalid :hash_access option :allow_missing_key => #{allow_missing_key.inspect}. Must be a Boolean"
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
# The :const, :value and :batch options do not read the serialized
|
|
62
|
-
# object by attribute name, so hash access can not affect them.
|
|
63
19
|
# Delegated attributes configure hash access per step with the
|
|
64
20
|
# `delegate: {to_hash_access:, hash_access:}` sub-options.
|
|
65
|
-
def
|
|
66
|
-
|
|
67
|
-
raise SeregaError, "Option :hash_access can not be used together with option :value" if opts.key?(:value)
|
|
68
|
-
raise SeregaError, "Option :hash_access can not be used together with option :batch" if opts.key?(:batch)
|
|
21
|
+
def check_usage_with_delegate(opts)
|
|
22
|
+
return unless opts.key?(:delegate)
|
|
69
23
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
" :hash_access (final step) sub-options instead"
|
|
75
|
-
end
|
|
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"
|
|
76
28
|
end
|
|
77
29
|
end
|
|
78
30
|
end
|
|
@@ -1,24 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class Serega
|
|
4
|
-
#
|
|
5
|
-
|
|
6
|
-
# @private
|
|
7
|
-
module Attribute
|
|
4
|
+
module SeregaValidations # :nodoc:
|
|
5
|
+
module Attribute # :nodoc:
|
|
8
6
|
#
|
|
9
7
|
# Attribute `:hide` option validator
|
|
10
8
|
#
|
|
11
|
-
#
|
|
12
|
-
class CheckOptHide
|
|
13
|
-
#
|
|
9
|
+
class CheckOptHide # :nodoc:
|
|
14
10
|
# Checks attribute :hide option
|
|
15
|
-
#
|
|
16
|
-
# @param opts [Hash] Attribute options
|
|
17
|
-
#
|
|
18
|
-
# @raise [SeregaError] SeregaError that option has invalid value
|
|
19
|
-
#
|
|
20
|
-
# @return [void]
|
|
21
|
-
#
|
|
22
11
|
def self.call(opts)
|
|
23
12
|
Utils::CheckOptIsBool.call(opts, :hide)
|
|
24
13
|
end
|
|
@@ -1,35 +1,33 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class Serega
|
|
4
|
-
#
|
|
5
|
-
|
|
6
|
-
# @private
|
|
7
|
-
module Attribute
|
|
4
|
+
module SeregaValidations # :nodoc:
|
|
5
|
+
module Attribute # :nodoc:
|
|
8
6
|
#
|
|
9
7
|
# Attribute `:many` option validator
|
|
10
8
|
#
|
|
11
|
-
#
|
|
12
|
-
class CheckOptMany
|
|
9
|
+
class CheckOptMany # :nodoc:
|
|
13
10
|
class << self
|
|
14
|
-
#
|
|
15
11
|
# Checks attribute :many option
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
#
|
|
24
|
-
def call(opts, block = nil)
|
|
25
|
-
return unless opts.key?(:many)
|
|
26
|
-
|
|
27
|
-
check_many_option_makes_sence(opts, block)
|
|
28
|
-
Utils::CheckOptIsBool.call(opts, :many)
|
|
12
|
+
def call(opts, block = nil, required = false)
|
|
13
|
+
if opts.key?(:many)
|
|
14
|
+
check_many_option_makes_sence(opts, block)
|
|
15
|
+
Utils::CheckOptIsBool.call(opts, :many)
|
|
16
|
+
elsif required
|
|
17
|
+
check_many_option_not_required(opts, block)
|
|
18
|
+
end
|
|
29
19
|
end
|
|
30
20
|
|
|
31
21
|
private
|
|
32
22
|
|
|
23
|
+
def check_many_option_not_required(opts, block)
|
|
24
|
+
return if !opts[:serializer] && !block
|
|
25
|
+
|
|
26
|
+
raise SeregaError,
|
|
27
|
+
"Attribute option :many [Boolean] must be provided" \
|
|
28
|
+
" for attributes with :serializer option or a block"
|
|
29
|
+
end
|
|
30
|
+
|
|
33
31
|
def check_many_option_makes_sence(opts, block)
|
|
34
32
|
return if many_option_makes_sence?(opts, block)
|
|
35
33
|
|
|
@@ -1,39 +1,17 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class Serega
|
|
4
|
-
#
|
|
5
|
-
|
|
6
|
-
# @private
|
|
7
|
-
module Attribute
|
|
4
|
+
module SeregaValidations # :nodoc:
|
|
5
|
+
module Attribute # :nodoc:
|
|
8
6
|
#
|
|
9
7
|
# Attribute `:method` option validator
|
|
10
8
|
#
|
|
11
|
-
#
|
|
12
|
-
class CheckOptMethod
|
|
9
|
+
class CheckOptMethod # :nodoc:
|
|
13
10
|
class << self
|
|
14
|
-
#
|
|
15
11
|
# Checks attribute :method option
|
|
16
|
-
#
|
|
17
|
-
# @param opts [Hash] Attribute options
|
|
18
|
-
#
|
|
19
|
-
# @raise [SeregaError] SeregaError that option has invalid value
|
|
20
|
-
#
|
|
21
|
-
# @return [void]
|
|
22
|
-
#
|
|
23
12
|
def call(opts)
|
|
24
|
-
return unless opts.key?(:method)
|
|
25
|
-
|
|
26
|
-
check_usage_with_other_params(opts)
|
|
27
13
|
Utils::CheckOptIsStringOrSymbol.call(opts, :method)
|
|
28
14
|
end
|
|
29
|
-
|
|
30
|
-
private
|
|
31
|
-
|
|
32
|
-
def check_usage_with_other_params(opts)
|
|
33
|
-
raise SeregaError, "Option :method can not be used together with option :const" if opts.key?(:const)
|
|
34
|
-
raise SeregaError, "Option :method can not be used together with option :value" if opts.key?(:value)
|
|
35
|
-
raise SeregaError, "Option :method can not be used together with option :batch" if opts.key?(:batch)
|
|
36
|
-
end
|
|
37
15
|
end
|
|
38
16
|
end
|
|
39
17
|
end
|
|
@@ -1,40 +1,17 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class Serega
|
|
4
|
-
#
|
|
5
|
-
|
|
6
|
-
# @private
|
|
7
|
-
module Attribute
|
|
4
|
+
module SeregaValidations # :nodoc:
|
|
5
|
+
module Attribute # :nodoc:
|
|
8
6
|
#
|
|
9
7
|
# Validator for attribute :preload option
|
|
10
8
|
#
|
|
11
|
-
#
|
|
12
|
-
class CheckOptPreload
|
|
9
|
+
class CheckOptPreload # :nodoc:
|
|
13
10
|
class << self
|
|
14
|
-
#
|
|
15
11
|
# Checks :preload option
|
|
16
|
-
#
|
|
17
|
-
# @param opts [Hash] Attribute options
|
|
18
|
-
#
|
|
19
|
-
# @raise [SeregaError] validation error
|
|
20
|
-
#
|
|
21
|
-
# @return [void]
|
|
22
12
|
def call(opts)
|
|
23
|
-
return unless opts.key?(:preload)
|
|
24
|
-
|
|
25
|
-
check_opt_preload(opts)
|
|
26
|
-
check_usage_with_other_params(opts)
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
private
|
|
30
|
-
|
|
31
|
-
def check_opt_preload(opts)
|
|
32
13
|
raise SeregaError, "Option :preload value can not be `true`" if opts[:preload] == true
|
|
33
14
|
end
|
|
34
|
-
|
|
35
|
-
def check_usage_with_other_params(opts)
|
|
36
|
-
raise SeregaError, "Option :preload can not be used together with option :const" if opts.key?(:const)
|
|
37
|
-
end
|
|
38
15
|
end
|
|
39
16
|
end
|
|
40
17
|
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module SeregaValidations # :nodoc:
|
|
5
|
+
module Attribute # :nodoc:
|
|
6
|
+
#
|
|
7
|
+
# Attribute `:preset` option validator
|
|
8
|
+
#
|
|
9
|
+
class CheckOptPreset # :nodoc:
|
|
10
|
+
class << self
|
|
11
|
+
# Checks that the :preset option names presets of the relation
|
|
12
|
+
# serializer. A serializer given as a String or a Proc, or defined
|
|
13
|
+
# with a block, is checked when a plan is built.
|
|
14
|
+
def call(opts, block = nil)
|
|
15
|
+
return unless opts.key?(:preset)
|
|
16
|
+
|
|
17
|
+
serializer = opts[:serializer]
|
|
18
|
+
raise SeregaError, "Option :preset can be provided only together with :serializer option or a block" unless serializer || block
|
|
19
|
+
|
|
20
|
+
check_defined(serializer, SeregaPlan::Presets.names(opts[:preset]))
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def check_defined(serializer, names)
|
|
26
|
+
return unless serializer.is_a?(Class)
|
|
27
|
+
|
|
28
|
+
names.each { |name| SeregaPlan::Presets.fetch_definition(serializer, name) }
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module SeregaValidations # :nodoc:
|
|
5
|
+
module Attribute # :nodoc:
|
|
6
|
+
#
|
|
7
|
+
# Attribute `:only`, `:with` and `:except` options validator
|
|
8
|
+
#
|
|
9
|
+
class CheckOptSelection # :nodoc:
|
|
10
|
+
# Field selection options of a relation
|
|
11
|
+
KEYS = %i[only with except].freeze
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
# Checks that the options are given with a relation, that they name
|
|
15
|
+
# attributes, and that the attributes exist in a serializer class.
|
|
16
|
+
# Another serializer is checked when a plan is built.
|
|
17
|
+
def call(name, opts, block = nil)
|
|
18
|
+
key = KEYS.find { |option| opts.key?(option) }
|
|
19
|
+
return unless key
|
|
20
|
+
|
|
21
|
+
serializer = opts[:serializer]
|
|
22
|
+
raise SeregaError, "Option #{key.inspect} can be provided only together with :serializer option or a block" unless serializer || block
|
|
23
|
+
|
|
24
|
+
selection = KEYS.to_h { |option| [option, parse(option, opts[option])] }
|
|
25
|
+
return unless serializer.is_a?(Class)
|
|
26
|
+
|
|
27
|
+
CheckRelationSelection.check(name, serializer, selection, CheckDefinitionSelection)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def parse(key, value)
|
|
33
|
+
SeregaPlan::SelectionPlanner.parse_option(value)
|
|
34
|
+
rescue SeregaError
|
|
35
|
+
raise SeregaError, "Invalid option #{key.inspect} => #{value.inspect}. Must be a Symbol, a String, an Array or a Hash of attribute names"
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -1,31 +1,17 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class Serega
|
|
4
|
-
#
|
|
5
|
-
|
|
6
|
-
# @private
|
|
7
|
-
module Attribute
|
|
4
|
+
module SeregaValidations # :nodoc:
|
|
5
|
+
module Attribute # :nodoc:
|
|
8
6
|
#
|
|
9
7
|
# Attribute `:serializer` option validator
|
|
10
8
|
#
|
|
11
|
-
#
|
|
12
|
-
class CheckOptSerializer
|
|
9
|
+
class CheckOptSerializer # :nodoc:
|
|
13
10
|
class << self
|
|
14
|
-
#
|
|
15
11
|
# Checks attribute :serializer option
|
|
16
|
-
|
|
17
|
-
# @param opts [Hash] Attribute options
|
|
18
|
-
# @param block [nil, Proc] Attribute block (defines a nested serializer)
|
|
19
|
-
#
|
|
20
|
-
# @raise [SeregaError] SeregaError that option has invalid value
|
|
21
|
-
#
|
|
22
|
-
# @return [void]
|
|
23
|
-
#
|
|
24
|
-
def call(opts, block = nil)
|
|
12
|
+
def call(opts)
|
|
25
13
|
return unless opts.key?(:serializer)
|
|
26
14
|
|
|
27
|
-
raise SeregaError, "Option :serializer can not be used together with block" if block
|
|
28
|
-
|
|
29
15
|
value = opts[:serializer]
|
|
30
16
|
return if valid_serializer?(value)
|
|
31
17
|
|
|
@@ -1,40 +1,22 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class Serega
|
|
4
|
-
#
|
|
5
|
-
|
|
6
|
-
# @private
|
|
7
|
-
module Attribute
|
|
4
|
+
module SeregaValidations # :nodoc:
|
|
5
|
+
module Attribute # :nodoc:
|
|
8
6
|
#
|
|
9
7
|
# Attribute `:value` option validator
|
|
10
8
|
#
|
|
11
|
-
#
|
|
12
|
-
class CheckOptValue
|
|
9
|
+
class CheckOptValue # :nodoc:
|
|
13
10
|
class << self
|
|
14
|
-
#
|
|
15
11
|
# Checks attribute :value option
|
|
16
|
-
#
|
|
17
|
-
# @param opts [Hash] Attribute options
|
|
18
|
-
#
|
|
19
|
-
# @raise [SeregaError] SeregaError that option has invalid value
|
|
20
|
-
#
|
|
21
|
-
# @return [void]
|
|
22
|
-
#
|
|
23
12
|
def call(opts)
|
|
24
13
|
return unless opts.key?(:value)
|
|
25
14
|
|
|
26
|
-
check_usage_with_other_params(opts)
|
|
27
|
-
|
|
28
15
|
check_value(opts[:value])
|
|
29
16
|
end
|
|
30
17
|
|
|
31
18
|
private
|
|
32
19
|
|
|
33
|
-
def check_usage_with_other_params(opts)
|
|
34
|
-
raise SeregaError, "Option :value can not be used together with option :method" if opts.key?(:method)
|
|
35
|
-
raise SeregaError, "Option :value can not be used together with option :const" if opts.key?(:const)
|
|
36
|
-
end
|
|
37
|
-
|
|
38
20
|
def check_value(value)
|
|
39
21
|
check_value_type(value)
|
|
40
22
|
signature = SeregaUtils::MethodSignature.call(value, pos_limit: 2, keyword_args: %i[ctx batches])
|
|
@@ -4,92 +4,113 @@ class Serega
|
|
|
4
4
|
#
|
|
5
5
|
# Validations
|
|
6
6
|
#
|
|
7
|
-
#
|
|
8
|
-
module SeregaValidations
|
|
7
|
+
module SeregaValidations # :nodoc:
|
|
9
8
|
#
|
|
10
9
|
# Validations for attribute params
|
|
11
10
|
#
|
|
12
|
-
#
|
|
13
|
-
|
|
11
|
+
class CheckAttributeParams # :nodoc:
|
|
12
|
+
# Options of `Serega.attribute`
|
|
13
|
+
ATTRIBUTE_KEYS = %i[
|
|
14
|
+
method value serializer many hide const delegate default format preload batch
|
|
15
|
+
base_serializer hash_access preset only with except if unless if_value unless_value
|
|
16
|
+
].freeze
|
|
17
|
+
|
|
18
|
+
# Options that can't be given together, `block` for the attribute
|
|
19
|
+
# block. Each option lists every option it conflicts with, and the error
|
|
20
|
+
# names the option that comes first here. :hash_access with :delegate is
|
|
21
|
+
# left to CheckOptHashAccess, which points to the delegate sub-options.
|
|
22
|
+
CONFLICTS = {
|
|
23
|
+
method: %i[value const delegate batch],
|
|
24
|
+
value: %i[method const delegate hash_access],
|
|
25
|
+
const: %i[method value delegate batch hash_access preload],
|
|
26
|
+
delegate: %i[method value const batch],
|
|
27
|
+
batch: %i[method const delegate hash_access],
|
|
28
|
+
hash_access: %i[value const batch],
|
|
29
|
+
preload: %i[const],
|
|
30
|
+
serializer: %i[block]
|
|
31
|
+
}.freeze
|
|
32
|
+
|
|
33
|
+
# Validated attribute name
|
|
34
|
+
attr_reader :name
|
|
35
|
+
|
|
36
|
+
# Validated attribute options
|
|
37
|
+
attr_reader :opts
|
|
38
|
+
|
|
39
|
+
# Validated attribute block
|
|
40
|
+
attr_reader :block
|
|
41
|
+
|
|
42
|
+
# Initializes attribute params validator
|
|
43
|
+
def initialize(serializer_class, name, opts, block)
|
|
44
|
+
@serializer_class = serializer_class
|
|
45
|
+
@name = name
|
|
46
|
+
@opts = opts
|
|
47
|
+
@block = block
|
|
48
|
+
end
|
|
49
|
+
|
|
14
50
|
#
|
|
15
|
-
#
|
|
51
|
+
# Validates attribute params
|
|
16
52
|
#
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
# @return [Hash] validated attribute options
|
|
23
|
-
attr_reader :opts
|
|
24
|
-
|
|
25
|
-
# @return [nil, Proc] validated attribute block
|
|
26
|
-
attr_reader :block
|
|
27
|
-
|
|
28
|
-
#
|
|
29
|
-
# Initializes attribute params validator
|
|
30
|
-
#
|
|
31
|
-
# @param name [Symbol] attribute name
|
|
32
|
-
# @param opts [Hash] attribute options
|
|
33
|
-
# @param block [nil, Proc] block provided to attribute
|
|
34
|
-
#
|
|
35
|
-
# @return [void]
|
|
36
|
-
#
|
|
37
|
-
def initialize(name, opts, block)
|
|
38
|
-
@name = name
|
|
39
|
-
@opts = opts
|
|
40
|
-
@block = block
|
|
41
|
-
end
|
|
53
|
+
def validate
|
|
54
|
+
check_name if check_attribute_name
|
|
55
|
+
check_opts
|
|
56
|
+
check_block
|
|
57
|
+
end
|
|
42
58
|
|
|
43
|
-
|
|
44
|
-
# Validates attribute params
|
|
45
|
-
#
|
|
46
|
-
def validate
|
|
47
|
-
check_name if check_attribute_name
|
|
48
|
-
check_opts
|
|
49
|
-
check_block
|
|
50
|
-
end
|
|
59
|
+
private
|
|
51
60
|
|
|
52
|
-
|
|
61
|
+
def check_name
|
|
62
|
+
Attribute::CheckName.call(name)
|
|
63
|
+
end
|
|
53
64
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
65
|
+
def check_opts
|
|
66
|
+
Utils::CheckAllowedKeys.call(opts, ATTRIBUTE_KEYS, :attribute)
|
|
67
|
+
check_conflicts
|
|
57
68
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
end
|
|
69
|
+
Attribute::CheckOptBaseSerializer.call(opts, block)
|
|
70
|
+
Attribute::CheckOptCondition.call(opts, :if)
|
|
71
|
+
Attribute::CheckOptCondition.call(opts, :unless)
|
|
72
|
+
Attribute::CheckOptCondition.call(opts, :if_value)
|
|
73
|
+
Attribute::CheckOptCondition.call(opts, :unless_value)
|
|
74
|
+
Attribute::CheckOptDelegate.call(opts)
|
|
75
|
+
Attribute::CheckOptFormat.call(opts, @serializer_class)
|
|
76
|
+
Attribute::CheckOptHashAccess.call(opts)
|
|
77
|
+
Attribute::CheckOptHide.call(opts)
|
|
78
|
+
Attribute::CheckOptMethod.call(opts)
|
|
79
|
+
Attribute::CheckOptMany.call(opts, block, require_many)
|
|
80
|
+
Attribute::CheckOptPreload.call(opts)
|
|
81
|
+
Attribute::CheckOptPreset.call(opts, block)
|
|
82
|
+
Attribute::CheckOptSelection.call(name, opts, block)
|
|
83
|
+
Attribute::CheckOptSerializer.call(opts)
|
|
84
|
+
Attribute::CheckOptValue.call(opts)
|
|
85
|
+
Attribute::CheckOptBatch.call(@serializer_class, name, opts)
|
|
86
|
+
end
|
|
77
87
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
88
|
+
def check_block
|
|
89
|
+
Attribute::CheckBlock.call(block)
|
|
90
|
+
end
|
|
81
91
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
92
|
+
def check_conflicts
|
|
93
|
+
CONFLICTS.each do |option, others|
|
|
94
|
+
next unless opts.key?(option)
|
|
85
95
|
|
|
86
|
-
|
|
87
|
-
|
|
96
|
+
other = others.find { |key| (key == :block) ? block : opts.key?(key) }
|
|
97
|
+
raise SeregaError, conflict_message(option, other) if other
|
|
88
98
|
end
|
|
89
99
|
end
|
|
90
100
|
|
|
91
|
-
|
|
92
|
-
|
|
101
|
+
def conflict_message(option, other)
|
|
102
|
+
return "Option #{option.inspect} can not be used together with block" if other == :block
|
|
103
|
+
|
|
104
|
+
"Option #{option.inspect} can not be used together with option #{other.inspect}"
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def check_attribute_name
|
|
108
|
+
@serializer_class.config.check_attribute_name
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def require_many
|
|
112
|
+
@serializer_class.config.require_many
|
|
113
|
+
end
|
|
93
114
|
end
|
|
94
115
|
end
|
|
95
116
|
end
|