serega 0.38.0 → 0.40.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 +198 -28
- data/VERSION +1 -1
- data/lib/serega/attribute.rb +3 -3
- data/lib/serega/attribute_normalizer.rb +140 -9
- data/lib/serega/attribute_value_resolvers/batch.rb +4 -0
- data/lib/serega/attribute_value_resolvers/hash_access.rb +116 -0
- data/lib/serega/config.rb +107 -1
- data/lib/serega/object_serializer.rb +3 -3
- data/lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb +2 -0
- data/lib/serega/plugins/explicit_many_option/explicit_many_option.rb +3 -2
- data/lib/serega/plugins/explicit_many_option/validations/check_opt_many.rb +6 -4
- data/lib/serega/plugins/presenter/presenter.rb +99 -2
- data/lib/serega/plugins/root/root.rb +1 -1
- data/lib/serega/utils/collection_detector.rb +26 -0
- data/lib/serega/validations/attribute/check_block.rb +20 -55
- data/lib/serega/validations/attribute/check_opt_base_serializer.rb +37 -0
- data/lib/serega/validations/attribute/check_opt_batch.rb +6 -6
- data/lib/serega/validations/attribute/check_opt_const.rb +3 -4
- data/lib/serega/validations/attribute/check_opt_delegate.rb +45 -5
- data/lib/serega/validations/attribute/check_opt_hash_access.rb +78 -0
- data/lib/serega/validations/attribute/check_opt_many.rb +8 -7
- data/lib/serega/validations/attribute/check_opt_method.rb +3 -4
- data/lib/serega/validations/attribute/check_opt_serializer.rb +4 -1
- data/lib/serega/validations/attribute/check_opt_value.rb +3 -4
- data/lib/serega/validations/check_attribute_params.rb +9 -7
- data/lib/serega.rb +22 -6
- metadata +5 -1
|
@@ -17,11 +17,11 @@ class Serega
|
|
|
17
17
|
#
|
|
18
18
|
# @return [void]
|
|
19
19
|
#
|
|
20
|
-
def call(serializer_class, opts
|
|
20
|
+
def call(serializer_class, opts)
|
|
21
21
|
return unless opts.key?(:batch)
|
|
22
22
|
|
|
23
23
|
check_opt_batch(opts, serializer_class)
|
|
24
|
-
check_usage_with_other_params(opts
|
|
24
|
+
check_usage_with_other_params(opts)
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
private
|
|
@@ -72,22 +72,22 @@ class Serega
|
|
|
72
72
|
Utils::CheckAllowedKeys.call(batch_opts, %i[use id], :batch)
|
|
73
73
|
end
|
|
74
74
|
|
|
75
|
-
def check_usage_with_other_params(opts
|
|
75
|
+
def check_usage_with_other_params(opts)
|
|
76
76
|
batch = opts[:batch]
|
|
77
77
|
use_id = batch.is_a?(Hash) && batch.key?(:id)
|
|
78
78
|
use_multiple = batch.is_a?(Hash) && (Array(batch[:use]).size > 1)
|
|
79
|
-
value_added = opts.key?(:value)
|
|
79
|
+
value_added = opts.key?(:value)
|
|
80
80
|
|
|
81
81
|
if use_multiple && use_id
|
|
82
82
|
raise SeregaError, "Option `batch.id` should not be used with multiple loaders provided in `batch.use`"
|
|
83
83
|
end
|
|
84
84
|
|
|
85
85
|
if use_multiple && !value_added
|
|
86
|
-
raise SeregaError, "Attribute :value option
|
|
86
|
+
raise SeregaError, "Attribute :value option should be provided when selecting multiple batch loaders"
|
|
87
87
|
end
|
|
88
88
|
|
|
89
89
|
if use_id && value_added
|
|
90
|
-
raise SeregaError, "Option `batch.id` should not be used when :value
|
|
90
|
+
raise SeregaError, "Option `batch.id` should not be used when :value option provided directly"
|
|
91
91
|
end
|
|
92
92
|
|
|
93
93
|
raise SeregaError, "Option :batch can not be used together with option :method" if opts.key?(:method)
|
|
@@ -17,19 +17,18 @@ class Serega
|
|
|
17
17
|
#
|
|
18
18
|
# @return [void]
|
|
19
19
|
#
|
|
20
|
-
def call(opts
|
|
20
|
+
def call(opts)
|
|
21
21
|
return unless opts.key?(:const)
|
|
22
22
|
|
|
23
|
-
check_usage_with_other_params(opts
|
|
23
|
+
check_usage_with_other_params(opts)
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
private
|
|
27
27
|
|
|
28
|
-
def check_usage_with_other_params(opts
|
|
28
|
+
def check_usage_with_other_params(opts)
|
|
29
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 option :batch" if opts.key?(:batch)
|
|
32
|
-
raise SeregaError, "Option :const can not be used together with block" if block
|
|
33
32
|
end
|
|
34
33
|
end
|
|
35
34
|
end
|
|
@@ -18,11 +18,11 @@ class Serega
|
|
|
18
18
|
#
|
|
19
19
|
# @return [void]
|
|
20
20
|
#
|
|
21
|
-
def call(opts
|
|
21
|
+
def call(opts)
|
|
22
22
|
return unless opts.key?(:delegate)
|
|
23
23
|
|
|
24
24
|
check_opt_delegate(opts)
|
|
25
|
-
check_usage_with_other_params(opts
|
|
25
|
+
check_usage_with_other_params(opts)
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
private
|
|
@@ -34,6 +34,8 @@ class Serega
|
|
|
34
34
|
check_opt_delegate_to(delegate_opts)
|
|
35
35
|
check_opt_delegate_method(delegate_opts)
|
|
36
36
|
check_opt_delegate_allow_nil(delegate_opts)
|
|
37
|
+
check_opt_delegate_to_hash_access(delegate_opts)
|
|
38
|
+
check_opt_delegate_hash_access(delegate_opts)
|
|
37
39
|
check_opt_delegate_extra_opts(delegate_opts)
|
|
38
40
|
end
|
|
39
41
|
|
|
@@ -48,20 +50,58 @@ class Serega
|
|
|
48
50
|
Utils::CheckOptIsStringOrSymbol.call(delegate_opts, :method)
|
|
49
51
|
end
|
|
50
52
|
|
|
53
|
+
# Covers only the intermediate object being nil — a missing key on
|
|
54
|
+
# either step is each step's own :allow_missing_key sub-option.
|
|
51
55
|
def check_opt_delegate_allow_nil(delegate_opts)
|
|
52
56
|
Utils::CheckOptIsBool.call(delegate_opts, :allow_nil)
|
|
53
57
|
end
|
|
54
58
|
|
|
59
|
+
# Hash access of the intermediate (:to) step. Accepts the same
|
|
60
|
+
# forms as the final (:method) step.
|
|
61
|
+
def check_opt_delegate_to_hash_access(delegate_opts)
|
|
62
|
+
check_hash_access_value(delegate_opts, :to_hash_access)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Hash access of the final (:method) step. Accepts the same forms
|
|
66
|
+
# as the attribute :hash_access option.
|
|
67
|
+
def check_opt_delegate_hash_access(delegate_opts)
|
|
68
|
+
check_hash_access_value(delegate_opts, :hash_access)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def check_hash_access_value(delegate_opts, opt_name)
|
|
72
|
+
return unless delegate_opts.key?(opt_name)
|
|
73
|
+
|
|
74
|
+
value = delegate_opts[opt_name]
|
|
75
|
+
case value
|
|
76
|
+
when true, false then nil
|
|
77
|
+
when Symbol then check_hash_access_mode(value)
|
|
78
|
+
when Hash
|
|
79
|
+
Utils::CheckAllowedKeys.call(value, %i[mode allow_missing_key], opt_name)
|
|
80
|
+
check_hash_access_mode(value[:mode]) if value.key?(:mode)
|
|
81
|
+
Utils::CheckOptIsBool.call(value, :allow_missing_key)
|
|
82
|
+
else
|
|
83
|
+
raise SeregaError,
|
|
84
|
+
"Invalid delegate option :#{opt_name} => #{value.inspect}." \
|
|
85
|
+
" It must be a Boolean, a Symbol mode (:symbol, :string)" \
|
|
86
|
+
" or a Hash with :mode and :allow_missing_key keys"
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def check_hash_access_mode(mode)
|
|
91
|
+
return true if AttributeValueResolvers::HashAccessResolver::MODES.include?(mode)
|
|
92
|
+
|
|
93
|
+
raise SeregaError, "Invalid :hash_access mode #{mode.inspect}. Allowed modes: :symbol, :string"
|
|
94
|
+
end
|
|
95
|
+
|
|
55
96
|
def check_opt_delegate_extra_opts(delegate_opts)
|
|
56
|
-
Utils::CheckAllowedKeys.call(delegate_opts, %i[to method allow_nil], :delegate)
|
|
97
|
+
Utils::CheckAllowedKeys.call(delegate_opts, %i[to method allow_nil to_hash_access hash_access], :delegate)
|
|
57
98
|
end
|
|
58
99
|
|
|
59
|
-
def check_usage_with_other_params(opts
|
|
100
|
+
def check_usage_with_other_params(opts)
|
|
60
101
|
raise SeregaError, "Option :delegate can not be used together with option :method" if opts.key?(:method)
|
|
61
102
|
raise SeregaError, "Option :delegate can not be used together with option :const" if opts.key?(:const)
|
|
62
103
|
raise SeregaError, "Option :delegate can not be used together with option :value" if opts.key?(:value)
|
|
63
104
|
raise SeregaError, "Option :delegate can not be used together with option :batch" if opts.key?(:batch)
|
|
64
|
-
raise SeregaError, "Option :delegate can not be used together with block" if block
|
|
65
105
|
end
|
|
66
106
|
end
|
|
67
107
|
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module SeregaValidations
|
|
5
|
+
module Attribute
|
|
6
|
+
#
|
|
7
|
+
# Attribute `:hash_access` option validator
|
|
8
|
+
#
|
|
9
|
+
class CheckOptHashAccess
|
|
10
|
+
class << self
|
|
11
|
+
#
|
|
12
|
+
# Checks attribute :hash_access option
|
|
13
|
+
#
|
|
14
|
+
# @param opts [Hash] Attribute options
|
|
15
|
+
#
|
|
16
|
+
# @raise [SeregaError] Attribute validation error
|
|
17
|
+
#
|
|
18
|
+
# @return [void]
|
|
19
|
+
#
|
|
20
|
+
def call(opts)
|
|
21
|
+
return unless opts.key?(:hash_access)
|
|
22
|
+
|
|
23
|
+
value = opts[:hash_access]
|
|
24
|
+
check_value(value)
|
|
25
|
+
check_usage_with_other_params(opts) if value
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def check_value(value)
|
|
31
|
+
case value
|
|
32
|
+
when true, false then nil
|
|
33
|
+
when Symbol then check_mode(value)
|
|
34
|
+
when Hash
|
|
35
|
+
Utils::CheckAllowedKeys.call(value, %i[mode allow_missing_key], :hash_access)
|
|
36
|
+
check_mode(value[:mode]) if value.key?(:mode)
|
|
37
|
+
check_allow_missing_key(value[:allow_missing_key]) if value.key?(:allow_missing_key)
|
|
38
|
+
else
|
|
39
|
+
raise SeregaError,
|
|
40
|
+
"Invalid option :hash_access => #{value.inspect}." \
|
|
41
|
+
" It must be a Boolean, a Symbol mode (:symbol, :string)" \
|
|
42
|
+
" or a Hash with :mode and :allow_missing_key keys"
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def check_mode(mode)
|
|
47
|
+
return if AttributeValueResolvers::HashAccessResolver::MODES.include?(mode)
|
|
48
|
+
|
|
49
|
+
raise SeregaError, "Invalid :hash_access mode #{mode.inspect}. Allowed modes: :symbol, :string"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def check_allow_missing_key(allow_missing_key)
|
|
53
|
+
return if allow_missing_key == true || allow_missing_key == false
|
|
54
|
+
|
|
55
|
+
raise SeregaError, "Invalid :hash_access option :allow_missing_key => #{allow_missing_key.inspect}. Must be a Boolean"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# The :const, :value and :batch options do not read the serialized
|
|
59
|
+
# object by attribute name, so hash access can not affect them.
|
|
60
|
+
# Delegated attributes configure hash access per step with the
|
|
61
|
+
# `delegate: {to_hash_access:, hash_access:}` sub-options.
|
|
62
|
+
def check_usage_with_other_params(opts)
|
|
63
|
+
raise SeregaError, "Option :hash_access can not be used together with option :const" if opts.key?(:const)
|
|
64
|
+
raise SeregaError, "Option :hash_access can not be used together with option :value" if opts.key?(:value)
|
|
65
|
+
raise SeregaError, "Option :hash_access can not be used together with option :batch" if opts.key?(:batch)
|
|
66
|
+
|
|
67
|
+
if opts.key?(:delegate)
|
|
68
|
+
raise SeregaError,
|
|
69
|
+
"Option :hash_access can not be used together with option :delegate." \
|
|
70
|
+
" Use the delegate :to_hash_access (intermediate step) and" \
|
|
71
|
+
" :hash_access (final step) sub-options instead"
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -12,28 +12,29 @@ class Serega
|
|
|
12
12
|
# Checks attribute :many option
|
|
13
13
|
#
|
|
14
14
|
# @param opts [Hash] Attribute options
|
|
15
|
+
# @param block [nil, Proc] Attribute block
|
|
15
16
|
#
|
|
16
17
|
# @raise [SeregaError] SeregaError that option has invalid value
|
|
17
18
|
#
|
|
18
19
|
# @return [void]
|
|
19
20
|
#
|
|
20
|
-
def call(opts)
|
|
21
|
+
def call(opts, block = nil)
|
|
21
22
|
return unless opts.key?(:many)
|
|
22
23
|
|
|
23
|
-
check_many_option_makes_sence(opts)
|
|
24
|
+
check_many_option_makes_sence(opts, block)
|
|
24
25
|
Utils::CheckOptIsBool.call(opts, :many)
|
|
25
26
|
end
|
|
26
27
|
|
|
27
28
|
private
|
|
28
29
|
|
|
29
|
-
def check_many_option_makes_sence(opts)
|
|
30
|
-
return if many_option_makes_sence?(opts)
|
|
30
|
+
def check_many_option_makes_sence(opts, block)
|
|
31
|
+
return if many_option_makes_sence?(opts, block)
|
|
31
32
|
|
|
32
|
-
raise SeregaError, "Option :many can be provided only together with :serializer
|
|
33
|
+
raise SeregaError, "Option :many can be provided only together with :serializer, :batch option or a block"
|
|
33
34
|
end
|
|
34
35
|
|
|
35
|
-
def many_option_makes_sence?(opts)
|
|
36
|
-
opts[:serializer] || opts[:batch]
|
|
36
|
+
def many_option_makes_sence?(opts, block)
|
|
37
|
+
opts[:serializer] || opts[:batch] || block
|
|
37
38
|
end
|
|
38
39
|
end
|
|
39
40
|
end
|
|
@@ -17,20 +17,19 @@ class Serega
|
|
|
17
17
|
#
|
|
18
18
|
# @return [void]
|
|
19
19
|
#
|
|
20
|
-
def call(opts
|
|
20
|
+
def call(opts)
|
|
21
21
|
return unless opts.key?(:method)
|
|
22
22
|
|
|
23
|
-
check_usage_with_other_params(opts
|
|
23
|
+
check_usage_with_other_params(opts)
|
|
24
24
|
Utils::CheckOptIsStringOrSymbol.call(opts, :method)
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
private
|
|
28
28
|
|
|
29
|
-
def check_usage_with_other_params(opts
|
|
29
|
+
def check_usage_with_other_params(opts)
|
|
30
30
|
raise SeregaError, "Option :method can not be used together with option :const" if opts.key?(:const)
|
|
31
31
|
raise SeregaError, "Option :method can not be used together with option :value" if opts.key?(:value)
|
|
32
32
|
raise SeregaError, "Option :method can not be used together with option :batch" if opts.key?(:batch)
|
|
33
|
-
raise SeregaError, "Option :method can not be used together with block" if block
|
|
34
33
|
end
|
|
35
34
|
end
|
|
36
35
|
end
|
|
@@ -12,14 +12,17 @@ class Serega
|
|
|
12
12
|
# Checks attribute :serializer option
|
|
13
13
|
#
|
|
14
14
|
# @param opts [Hash] Attribute options
|
|
15
|
+
# @param block [nil, Proc] Attribute block (defines a nested serializer)
|
|
15
16
|
#
|
|
16
17
|
# @raise [SeregaError] SeregaError that option has invalid value
|
|
17
18
|
#
|
|
18
19
|
# @return [void]
|
|
19
20
|
#
|
|
20
|
-
def call(opts)
|
|
21
|
+
def call(opts, block = nil)
|
|
21
22
|
return unless opts.key?(:serializer)
|
|
22
23
|
|
|
24
|
+
raise SeregaError, "Option :serializer can not be used together with block" if block
|
|
25
|
+
|
|
23
26
|
value = opts[:serializer]
|
|
24
27
|
return if valid_serializer?(value)
|
|
25
28
|
|
|
@@ -17,20 +17,19 @@ class Serega
|
|
|
17
17
|
#
|
|
18
18
|
# @return [void]
|
|
19
19
|
#
|
|
20
|
-
def call(opts
|
|
20
|
+
def call(opts)
|
|
21
21
|
return unless opts.key?(:value)
|
|
22
22
|
|
|
23
|
-
check_usage_with_other_params(opts
|
|
23
|
+
check_usage_with_other_params(opts)
|
|
24
24
|
|
|
25
25
|
check_value(opts[:value])
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
private
|
|
29
29
|
|
|
30
|
-
def check_usage_with_other_params(opts
|
|
30
|
+
def check_usage_with_other_params(opts)
|
|
31
31
|
raise SeregaError, "Option :value can not be used together with option :method" if opts.key?(:method)
|
|
32
32
|
raise SeregaError, "Option :value can not be used together with option :const" if opts.key?(:const)
|
|
33
|
-
raise SeregaError, "Option :value can not be used together with block" if block
|
|
34
33
|
end
|
|
35
34
|
|
|
36
35
|
def check_value(value)
|
|
@@ -59,15 +59,17 @@ class Serega
|
|
|
59
59
|
def check_opts
|
|
60
60
|
Utils::CheckAllowedKeys.call(opts, allowed_opts_keys, :attribute)
|
|
61
61
|
|
|
62
|
-
Attribute::
|
|
63
|
-
Attribute::
|
|
62
|
+
Attribute::CheckOptBaseSerializer.call(opts, block)
|
|
63
|
+
Attribute::CheckOptConst.call(opts)
|
|
64
|
+
Attribute::CheckOptDelegate.call(opts)
|
|
65
|
+
Attribute::CheckOptHashAccess.call(opts)
|
|
64
66
|
Attribute::CheckOptHide.call(opts)
|
|
65
|
-
Attribute::CheckOptMethod.call(opts
|
|
66
|
-
Attribute::CheckOptMany.call(opts)
|
|
67
|
+
Attribute::CheckOptMethod.call(opts)
|
|
68
|
+
Attribute::CheckOptMany.call(opts, block)
|
|
67
69
|
Attribute::CheckOptPreload.call(opts)
|
|
68
|
-
Attribute::CheckOptSerializer.call(opts)
|
|
69
|
-
Attribute::CheckOptValue.call(opts
|
|
70
|
-
Attribute::CheckOptBatch.call(self.class.serializer_class, opts
|
|
70
|
+
Attribute::CheckOptSerializer.call(opts, block)
|
|
71
|
+
Attribute::CheckOptValue.call(opts)
|
|
72
|
+
Attribute::CheckOptBatch.call(self.class.serializer_class, opts)
|
|
71
73
|
end
|
|
72
74
|
|
|
73
75
|
def check_block
|
data/lib/serega.rb
CHANGED
|
@@ -19,6 +19,7 @@ end
|
|
|
19
19
|
|
|
20
20
|
require_relative "serega/errors"
|
|
21
21
|
require_relative "serega/helpers/serializer_class_helper"
|
|
22
|
+
require_relative "serega/utils/collection_detector"
|
|
22
23
|
require_relative "serega/utils/enum_deep_dup"
|
|
23
24
|
require_relative "serega/utils/enum_deep_freeze"
|
|
24
25
|
require_relative "serega/utils/method_signature"
|
|
@@ -28,6 +29,7 @@ require_relative "serega/utils/to_hash"
|
|
|
28
29
|
require_relative "serega/attribute_value_resolvers/batch"
|
|
29
30
|
require_relative "serega/attribute_value_resolvers/const"
|
|
30
31
|
require_relative "serega/attribute_value_resolvers/delegate"
|
|
32
|
+
require_relative "serega/attribute_value_resolvers/hash_access"
|
|
31
33
|
require_relative "serega/attribute_value_resolvers/keyword"
|
|
32
34
|
require_relative "serega/attribute"
|
|
33
35
|
require_relative "serega/attribute_normalizer"
|
|
@@ -40,10 +42,12 @@ require_relative "serega/validations/utils/check_opt_is_hash"
|
|
|
40
42
|
require_relative "serega/validations/utils/check_opt_is_string_or_symbol"
|
|
41
43
|
require_relative "serega/validations/attribute/check_block"
|
|
42
44
|
require_relative "serega/validations/attribute/check_name"
|
|
45
|
+
require_relative "serega/validations/attribute/check_opt_base_serializer"
|
|
43
46
|
require_relative "serega/validations/attribute/check_opt_const"
|
|
44
47
|
require_relative "serega/validations/attribute/check_opt_hide"
|
|
45
48
|
require_relative "serega/validations/attribute/check_opt_delegate"
|
|
46
49
|
require_relative "serega/validations/attribute/check_opt_batch"
|
|
50
|
+
require_relative "serega/validations/attribute/check_opt_hash_access"
|
|
47
51
|
require_relative "serega/validations/attribute/check_opt_many"
|
|
48
52
|
require_relative "serega/validations/attribute/check_opt_method"
|
|
49
53
|
require_relative "serega/validations/attribute/check_opt_preload"
|
|
@@ -168,7 +172,19 @@ class Serega
|
|
|
168
172
|
#
|
|
169
173
|
# @param name [Symbol] Attribute name. Attribute value will be found by executing `object.<name>`
|
|
170
174
|
# @param opts [Hash] Options to serialize attribute
|
|
171
|
-
# @param block [Proc]
|
|
175
|
+
# @param block [Proc] Defines attributes of a nested anonymous serializer.
|
|
176
|
+
# The block is executed in the context of that serializer, so everything
|
|
177
|
+
# available in a serializer class body can be used inside. The attribute
|
|
178
|
+
# value (found by name/:method/:value/:delegate/:const/:batch as usual)
|
|
179
|
+
# is serialized with this nested serializer. The nested serializer
|
|
180
|
+
# inherits from the `:base_serializer` attribute option or
|
|
181
|
+
# `config.base_serializer` (one of them is required).
|
|
182
|
+
#
|
|
183
|
+
# @example
|
|
184
|
+
# attribute :statistics, method: :itself do
|
|
185
|
+
# attribute :likes_count
|
|
186
|
+
# attribute :comments_count
|
|
187
|
+
# end
|
|
172
188
|
#
|
|
173
189
|
# @return [Serega::SeregaAttribute] Added attribute
|
|
174
190
|
#
|
|
@@ -254,7 +270,7 @@ class Serega
|
|
|
254
270
|
# @option opts [Array, Hash, String, Symbol] :with Attributes (usually hidden) to serialize additionally
|
|
255
271
|
# @option opts [Boolean] :validate Validates provided modifiers (Default is true)
|
|
256
272
|
# @option opts [Hash] :context Serialization context
|
|
257
|
-
# @option opts [Boolean] :many Set true if provided multiple objects (Default `object.is_a?(Enumerable)`)
|
|
273
|
+
# @option opts [Boolean] :many Set true if provided multiple objects (Default `object.is_a?(Enumerable) && !object.is_a?(Hash) && !object.is_a?(Struct)`)
|
|
258
274
|
#
|
|
259
275
|
# @return [Hash] Serialization result
|
|
260
276
|
#
|
|
@@ -275,7 +291,7 @@ class Serega
|
|
|
275
291
|
# @option opts [Array, Hash, String, Symbol] :with Attributes (usually hidden) to serialize additionally
|
|
276
292
|
# @option opts [Boolean] :validate Validates provided modifiers (Default is true)
|
|
277
293
|
# @option opts [Hash] :context Serialization context
|
|
278
|
-
# @option opts [Boolean] :many Set true if provided multiple objects (Default `object.is_a?(Enumerable)`)
|
|
294
|
+
# @option opts [Boolean] :many Set true if provided multiple objects (Default `object.is_a?(Enumerable) && !object.is_a?(Hash) && !object.is_a?(Struct)`)
|
|
279
295
|
#
|
|
280
296
|
# @return [Data, Array<Data>, nil] Serialization result as Data object(s)
|
|
281
297
|
#
|
|
@@ -409,7 +425,7 @@ class Serega
|
|
|
409
425
|
# @param object [Object] Serialized object
|
|
410
426
|
# @param opts [Hash, nil] Serializing options
|
|
411
427
|
# @option opts [Hash] :context Serialization context
|
|
412
|
-
# @option opts [Boolean] :many Set true if provided multiple objects (Default `object.is_a?(Enumerable)`)
|
|
428
|
+
# @option opts [Boolean] :many Set true if provided multiple objects (Default `object.is_a?(Enumerable) && !object.is_a?(Hash) && !object.is_a?(Struct)`)
|
|
413
429
|
#
|
|
414
430
|
# @return [Hash] Serialization result
|
|
415
431
|
#
|
|
@@ -431,7 +447,7 @@ class Serega
|
|
|
431
447
|
# @param object [Object] Serialized object
|
|
432
448
|
# @param opts [Hash, nil] Serializing options
|
|
433
449
|
# @option opts [Hash] :context Serialization context
|
|
434
|
-
# @option opts [Boolean] :many Set true if provided multiple objects (Default `object.is_a?(Enumerable)`)
|
|
450
|
+
# @option opts [Boolean] :many Set true if provided multiple objects (Default `object.is_a?(Enumerable) && !object.is_a?(Hash) && !object.is_a?(Struct)`)
|
|
435
451
|
#
|
|
436
452
|
# @return [Data] Serialization result
|
|
437
453
|
#
|
|
@@ -472,7 +488,7 @@ class Serega
|
|
|
472
488
|
|
|
473
489
|
opts[:context] ||= {}
|
|
474
490
|
opts[:level_queue] = SeregaEngine::LevelQueue.new
|
|
475
|
-
opts[:many] =
|
|
491
|
+
opts[:many] = SeregaUtils::CollectionDetector.call(object) unless opts.key?(:many)
|
|
476
492
|
opts[:plan] = plan
|
|
477
493
|
opts
|
|
478
494
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: serega
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.40.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andrey Glushkov
|
|
@@ -32,6 +32,7 @@ files:
|
|
|
32
32
|
- lib/serega/attribute_value_resolvers/batch.rb
|
|
33
33
|
- lib/serega/attribute_value_resolvers/const.rb
|
|
34
34
|
- lib/serega/attribute_value_resolvers/delegate.rb
|
|
35
|
+
- lib/serega/attribute_value_resolvers/hash_access.rb
|
|
35
36
|
- lib/serega/attribute_value_resolvers/keyword.rb
|
|
36
37
|
- lib/serega/config.rb
|
|
37
38
|
- lib/serega/data_builder.rb
|
|
@@ -70,6 +71,7 @@ files:
|
|
|
70
71
|
- lib/serega/plugins/root/root.rb
|
|
71
72
|
- lib/serega/plugins/string_modifiers/parse_string_modifiers.rb
|
|
72
73
|
- lib/serega/plugins/string_modifiers/string_modifiers.rb
|
|
74
|
+
- lib/serega/utils/collection_detector.rb
|
|
73
75
|
- lib/serega/utils/enum_deep_dup.rb
|
|
74
76
|
- lib/serega/utils/enum_deep_freeze.rb
|
|
75
77
|
- lib/serega/utils/method_signature.rb
|
|
@@ -78,9 +80,11 @@ files:
|
|
|
78
80
|
- lib/serega/utils/to_hash.rb
|
|
79
81
|
- lib/serega/validations/attribute/check_block.rb
|
|
80
82
|
- lib/serega/validations/attribute/check_name.rb
|
|
83
|
+
- lib/serega/validations/attribute/check_opt_base_serializer.rb
|
|
81
84
|
- lib/serega/validations/attribute/check_opt_batch.rb
|
|
82
85
|
- lib/serega/validations/attribute/check_opt_const.rb
|
|
83
86
|
- lib/serega/validations/attribute/check_opt_delegate.rb
|
|
87
|
+
- lib/serega/validations/attribute/check_opt_hash_access.rb
|
|
84
88
|
- lib/serega/validations/attribute/check_opt_hide.rb
|
|
85
89
|
- lib/serega/validations/attribute/check_opt_many.rb
|
|
86
90
|
- lib/serega/validations/attribute/check_opt_method.rb
|