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,111 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
class Serega
|
|
4
|
-
module SeregaPlugins
|
|
5
|
-
#
|
|
6
|
-
# Plugin :string_modifiers
|
|
7
|
-
#
|
|
8
|
-
# Allows to specify modifiers as strings.
|
|
9
|
-
#
|
|
10
|
-
# Serialized attributes must be split with `,` and nested attributes can be defined inside brackets `(`, `)`.
|
|
11
|
-
#
|
|
12
|
-
# @example
|
|
13
|
-
# PostSerializer.plugin :string_modifiers
|
|
14
|
-
# PostSerializer.new(only: "id,user(id,username)").to_h(post)
|
|
15
|
-
# PostSerializer.new(except: "user(username,email)").to_h(post)
|
|
16
|
-
# PostSerializer.new(with: "user(email)").to_h(post)
|
|
17
|
-
#
|
|
18
|
-
# # Modifiers can still be provided old way with nested hashes or arrays.
|
|
19
|
-
# PostSerializer.new(with: {user: %i[email, username]}).to_h(post)
|
|
20
|
-
#
|
|
21
|
-
module StringModifiers
|
|
22
|
-
#
|
|
23
|
-
# Modifiers parser
|
|
24
|
-
#
|
|
25
|
-
# @private
|
|
26
|
-
class ParseStringModifiers
|
|
27
|
-
COMMA = ","
|
|
28
|
-
COMMA_CODEPOINT = COMMA.ord
|
|
29
|
-
LPAREN = "("
|
|
30
|
-
LPAREN_CODEPOINT = LPAREN.ord
|
|
31
|
-
RPAREN = ")"
|
|
32
|
-
RPAREN_CODEPOINT = RPAREN.ord
|
|
33
|
-
private_constant :COMMA, :LPAREN, :RPAREN, :COMMA_CODEPOINT, :LPAREN_CODEPOINT, :RPAREN_CODEPOINT
|
|
34
|
-
|
|
35
|
-
class << self
|
|
36
|
-
#
|
|
37
|
-
# Parses string modifiers
|
|
38
|
-
#
|
|
39
|
-
# @param fields [String]
|
|
40
|
-
#
|
|
41
|
-
# @return [Hash] parsed modifiers in form of nested hash
|
|
42
|
-
#
|
|
43
|
-
# @example
|
|
44
|
-
# parse("user") => { user: {} }
|
|
45
|
-
# parse("user(id)") => { user: { id: {} } }
|
|
46
|
-
# parse("user(id,name)") => { user: { id: {}, name: {} } }
|
|
47
|
-
# parse("user,comments") => { user: {}, comments: {} }
|
|
48
|
-
# parse("user(comments(text))") => { user: { comments: { text: {} } } }
|
|
49
|
-
def parse(fields)
|
|
50
|
-
result = {}
|
|
51
|
-
attribute_storage = result
|
|
52
|
-
path_stack = (fields.include?(LPAREN) || fields.include?(RPAREN)) ? [] : nil
|
|
53
|
-
|
|
54
|
-
start_index = 0
|
|
55
|
-
end_index = 0
|
|
56
|
-
fields.each_codepoint do |codepoint|
|
|
57
|
-
case codepoint
|
|
58
|
-
when COMMA_CODEPOINT
|
|
59
|
-
attribute = extract_attribute(fields, start_index, end_index)
|
|
60
|
-
add_attribute(attribute_storage, attribute, FROZEN_EMPTY_HASH) if attribute
|
|
61
|
-
start_index = end_index + 1
|
|
62
|
-
when LPAREN_CODEPOINT
|
|
63
|
-
attribute = extract_attribute(fields, start_index, end_index)
|
|
64
|
-
if attribute
|
|
65
|
-
attribute_storage = add_attribute(attribute_storage, attribute, {})
|
|
66
|
-
path_stack.push(attribute)
|
|
67
|
-
end
|
|
68
|
-
start_index = end_index + 1
|
|
69
|
-
when RPAREN_CODEPOINT
|
|
70
|
-
attribute = extract_attribute(fields, start_index, end_index)
|
|
71
|
-
add_attribute(attribute_storage, attribute, FROZEN_EMPTY_HASH) if attribute
|
|
72
|
-
path_stack.pop
|
|
73
|
-
attribute_storage = dig?(result, path_stack)
|
|
74
|
-
start_index = end_index + 1
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
end_index += 1
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
attribute = extract_attribute(fields, start_index, end_index)
|
|
81
|
-
add_attribute(attribute_storage, attribute, FROZEN_EMPTY_HASH) if attribute
|
|
82
|
-
|
|
83
|
-
result
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
private
|
|
87
|
-
|
|
88
|
-
def extract_attribute(fields, start_index, end_index)
|
|
89
|
-
attribute = fields[start_index, end_index - start_index]
|
|
90
|
-
attribute.strip!
|
|
91
|
-
attribute.empty? ? nil : attribute.to_sym
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
def add_attribute(storage, attribute, nested_attributes = FROZEN_EMPTY_HASH)
|
|
95
|
-
storage[attribute] = nested_attributes
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
def dig?(hash, path)
|
|
99
|
-
return hash if !path || path.empty?
|
|
100
|
-
|
|
101
|
-
path.each do |point|
|
|
102
|
-
hash = hash[point]
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
hash
|
|
106
|
-
end
|
|
107
|
-
end
|
|
108
|
-
end
|
|
109
|
-
end
|
|
110
|
-
end
|
|
111
|
-
end
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
class Serega
|
|
4
|
-
module SeregaPlugins
|
|
5
|
-
#
|
|
6
|
-
# Plugin :string_modifiers
|
|
7
|
-
#
|
|
8
|
-
# Allows `:only`, `:except` and `:with` to be given as a single comma-separated
|
|
9
|
-
# string, with nested attributes in parentheses. Useful for accepting a field
|
|
10
|
-
# list straight from a query parameter.
|
|
11
|
-
#
|
|
12
|
-
# @example
|
|
13
|
-
# class UserSerializer < Serega
|
|
14
|
-
# plugin :string_modifiers
|
|
15
|
-
#
|
|
16
|
-
# attribute :username
|
|
17
|
-
# attribute :first_name
|
|
18
|
-
# attribute :addresses, serializer: AddressSerializer, hide: true
|
|
19
|
-
# end
|
|
20
|
-
#
|
|
21
|
-
# UserSerializer.to_h(user, only: "username,addresses(line1,line2)")
|
|
22
|
-
#
|
|
23
|
-
module StringModifiers
|
|
24
|
-
# @return [Symbol] Plugin name
|
|
25
|
-
# @private
|
|
26
|
-
def self.plugin_name
|
|
27
|
-
:string_modifiers
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
#
|
|
31
|
-
# Applies plugin code to specific serializer
|
|
32
|
-
#
|
|
33
|
-
# @param serializer_class [Class<Serega>] Current serializer class
|
|
34
|
-
# @param _opts [Hash] Plugin options
|
|
35
|
-
#
|
|
36
|
-
# @return [void]
|
|
37
|
-
#
|
|
38
|
-
# @private
|
|
39
|
-
def self.load_plugin(serializer_class, **_opts)
|
|
40
|
-
serializer_class.include(InstanceMethods)
|
|
41
|
-
require_relative "parse_string_modifiers"
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
#
|
|
45
|
-
# Serega additional/patched instance methods
|
|
46
|
-
#
|
|
47
|
-
# @see Serega
|
|
48
|
-
#
|
|
49
|
-
# @private
|
|
50
|
-
module InstanceMethods
|
|
51
|
-
private
|
|
52
|
-
|
|
53
|
-
def parse_modifier(value)
|
|
54
|
-
return ParseStringModifiers.parse(value) if value.is_a?(String)
|
|
55
|
-
|
|
56
|
-
super
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
register_plugin(StringModifiers.plugin_name, StringModifiers)
|
|
62
|
-
end
|
|
63
|
-
end
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
class Serega
|
|
4
|
-
# @private
|
|
5
|
-
module SeregaUtils
|
|
6
|
-
#
|
|
7
|
-
# Utility to get frozen string from symbol in any ruby version
|
|
8
|
-
#
|
|
9
|
-
# @private
|
|
10
|
-
class SymbolName
|
|
11
|
-
class << self
|
|
12
|
-
#
|
|
13
|
-
# Returns frozen string corresponding to provided symbol
|
|
14
|
-
#
|
|
15
|
-
# @param key [Symbol]
|
|
16
|
-
#
|
|
17
|
-
# @return [String] frozen string corresponding to provided symbol
|
|
18
|
-
#
|
|
19
|
-
def call(key)
|
|
20
|
-
key.is_a?(String) ? key : to_frozen_string(key)
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
private
|
|
24
|
-
|
|
25
|
-
# :nocov:
|
|
26
|
-
if RUBY_VERSION < "3"
|
|
27
|
-
def to_frozen_string(key)
|
|
28
|
-
key.to_s.freeze
|
|
29
|
-
end
|
|
30
|
-
else
|
|
31
|
-
def to_frozen_string(key)
|
|
32
|
-
key.name
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
# :nocov:
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
end
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
class Serega
|
|
4
|
-
# @private
|
|
5
|
-
module SeregaValidations
|
|
6
|
-
# @private
|
|
7
|
-
module Attribute
|
|
8
|
-
#
|
|
9
|
-
# Attribute `:const` option validator
|
|
10
|
-
#
|
|
11
|
-
# @private
|
|
12
|
-
class CheckOptConst
|
|
13
|
-
class << self
|
|
14
|
-
#
|
|
15
|
-
# Checks attribute :const option
|
|
16
|
-
#
|
|
17
|
-
# @param opts [Hash] Attribute options
|
|
18
|
-
#
|
|
19
|
-
# @raise [SeregaError] Attribute validation error
|
|
20
|
-
#
|
|
21
|
-
# @return [void]
|
|
22
|
-
#
|
|
23
|
-
def call(opts)
|
|
24
|
-
return unless opts.key?(:const)
|
|
25
|
-
|
|
26
|
-
check_usage_with_other_params(opts)
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
private
|
|
30
|
-
|
|
31
|
-
def check_usage_with_other_params(opts)
|
|
32
|
-
raise SeregaError, "Option :const can not be used together with option :method" if opts.key?(:method)
|
|
33
|
-
raise SeregaError, "Option :const can not be used together with option :value" if opts.key?(:value)
|
|
34
|
-
raise SeregaError, "Option :const can not be used together with option :batch" if opts.key?(:batch)
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
end
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
class Serega
|
|
4
|
-
# @private
|
|
5
|
-
module SeregaValidations
|
|
6
|
-
#
|
|
7
|
-
# Validations of serializer initialization options
|
|
8
|
-
#
|
|
9
|
-
# @private
|
|
10
|
-
class CheckInitiateParams
|
|
11
|
-
#
|
|
12
|
-
# Validations of serializer initialization options instance methods
|
|
13
|
-
#
|
|
14
|
-
# @private
|
|
15
|
-
module InstanceMethods
|
|
16
|
-
# @return [Hash] validated initialization options
|
|
17
|
-
attr_reader :opts
|
|
18
|
-
|
|
19
|
-
#
|
|
20
|
-
# Initializes validator for initialization options
|
|
21
|
-
#
|
|
22
|
-
# @param opts [Hash] initialization options
|
|
23
|
-
#
|
|
24
|
-
# @return [void]
|
|
25
|
-
#
|
|
26
|
-
def initialize(opts)
|
|
27
|
-
@opts = opts
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
#
|
|
31
|
-
# Validates initiating params
|
|
32
|
-
#
|
|
33
|
-
def validate
|
|
34
|
-
check_allowed_keys
|
|
35
|
-
check_modifiers
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
private
|
|
39
|
-
|
|
40
|
-
def check_allowed_keys
|
|
41
|
-
Utils::CheckAllowedKeys.call(opts, serializer_class.config.initiate_keys, :initiate)
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
def check_modifiers
|
|
45
|
-
Initiate::CheckModifiers.new.call(serializer_class, opts[:only], opts[:with], opts[:except])
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def serializer_class
|
|
49
|
-
self.class.serializer_class
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
include InstanceMethods
|
|
54
|
-
extend Serega::SeregaHelpers::SerializerClassHelper
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
end
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
class Serega
|
|
4
|
-
# @private
|
|
5
|
-
module SeregaValidations
|
|
6
|
-
#
|
|
7
|
-
# Validations of serialization options
|
|
8
|
-
#
|
|
9
|
-
# @private
|
|
10
|
-
class CheckSerializeParams
|
|
11
|
-
#
|
|
12
|
-
# Validations of serialization options instance methods
|
|
13
|
-
#
|
|
14
|
-
# @private
|
|
15
|
-
module InstanceMethods
|
|
16
|
-
attr_reader :opts
|
|
17
|
-
|
|
18
|
-
#
|
|
19
|
-
# Initializes validator for serialization options
|
|
20
|
-
#
|
|
21
|
-
# @param opts [Hash] serialization options
|
|
22
|
-
#
|
|
23
|
-
# @return [void]
|
|
24
|
-
#
|
|
25
|
-
def initialize(opts)
|
|
26
|
-
@opts = opts
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
#
|
|
30
|
-
# Validates serialization options
|
|
31
|
-
#
|
|
32
|
-
def validate
|
|
33
|
-
check_opts
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
private
|
|
37
|
-
|
|
38
|
-
def check_opts
|
|
39
|
-
Utils::CheckAllowedKeys.call(opts, serializer_class.config.serialize_keys, :serialize)
|
|
40
|
-
|
|
41
|
-
Utils::CheckOptIsHash.call(opts, :context)
|
|
42
|
-
Utils::CheckOptIsBool.call(opts, :many)
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
def serializer_class
|
|
46
|
-
self.class.serializer_class
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
include InstanceMethods
|
|
51
|
-
extend Serega::SeregaHelpers::SerializerClassHelper
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
end
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
class Serega
|
|
4
|
-
# @private
|
|
5
|
-
module SeregaValidations
|
|
6
|
-
#
|
|
7
|
-
# Validations that take place when initializing serializer
|
|
8
|
-
#
|
|
9
|
-
# @private
|
|
10
|
-
module Initiate
|
|
11
|
-
#
|
|
12
|
-
# Modifiers validation
|
|
13
|
-
#
|
|
14
|
-
# @private
|
|
15
|
-
class CheckModifiers
|
|
16
|
-
#
|
|
17
|
-
# Validates provided fields names are existing attributes
|
|
18
|
-
#
|
|
19
|
-
# @param serializer_class [Serega]
|
|
20
|
-
# @param only [Hash, nil] `only` modifier
|
|
21
|
-
# @param with [Hash, nil] `with` modifier
|
|
22
|
-
# @param except [Hash, nil] `except` modifier
|
|
23
|
-
#
|
|
24
|
-
# @raise [Serega::AttributeNotExist] when some checked modifier has not existing attribute
|
|
25
|
-
#
|
|
26
|
-
# @return [void]
|
|
27
|
-
#
|
|
28
|
-
def call(serializer_class, only, with, except)
|
|
29
|
-
validate(serializer_class, only) if only
|
|
30
|
-
validate(serializer_class, with) if with
|
|
31
|
-
validate(serializer_class, except) if except
|
|
32
|
-
|
|
33
|
-
raise_errors(serializer_class) if any_error?
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
private
|
|
37
|
-
|
|
38
|
-
def validate(serializer_class, fields)
|
|
39
|
-
fields.each do |name, nested_fields|
|
|
40
|
-
attribute = serializer_class && serializer_class.attributes[name]
|
|
41
|
-
|
|
42
|
-
# Save error when no attribute with checked name exists
|
|
43
|
-
unless attribute
|
|
44
|
-
save_error(name)
|
|
45
|
-
next
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
# Return when attribute has no nested fields
|
|
49
|
-
next if nested_fields.empty?
|
|
50
|
-
|
|
51
|
-
with_parent_name(name) do
|
|
52
|
-
validate(attribute.serializer, nested_fields)
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def parents_names
|
|
58
|
-
@parents_names ||= []
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
def with_parent_name(name)
|
|
62
|
-
parents_names << name
|
|
63
|
-
yield
|
|
64
|
-
parents_names.pop
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
def error_attributes
|
|
68
|
-
@error_attributes ||= []
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
def save_error(name)
|
|
72
|
-
error_attributes << build_full_attribute_name(*parents_names, name)
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
def build_full_attribute_name(*names)
|
|
76
|
-
head, *nested = *names
|
|
77
|
-
result = +head.to_s # names are symbols, we need not frozen string
|
|
78
|
-
nested.each { |nested_name| result << "(" << nested_name.to_s }
|
|
79
|
-
nested.each { result << ")" }
|
|
80
|
-
result
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
def raise_errors(serializer_class)
|
|
84
|
-
raise Serega::AttributeNotExist.new("Not existing attributes: #{error_attributes.join(", ")}", serializer_class, error_attributes)
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
def any_error?
|
|
88
|
-
defined?(@error_attributes)
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
end
|