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
|
@@ -4,398 +4,243 @@ class Serega
|
|
|
4
4
|
#
|
|
5
5
|
# Prepares provided attribute options
|
|
6
6
|
#
|
|
7
|
-
#
|
|
8
|
-
|
|
7
|
+
class SeregaAttributeNormalizer # :nodoc:
|
|
8
|
+
# Attribute initial params
|
|
9
|
+
attr_reader :init_name, :init_opts, :init_block
|
|
10
|
+
|
|
11
|
+
# Instantiates attribute options normalizer
|
|
12
|
+
def initialize(serializer_class, initials)
|
|
13
|
+
@serializer_class = serializer_class
|
|
14
|
+
@init_name = initials[:name]
|
|
15
|
+
@init_opts = initials[:opts]
|
|
16
|
+
@init_block = initials[:block]
|
|
17
|
+
@method_name = nil
|
|
18
|
+
@key = nil
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Attribute name, the serialized key as a Symbol
|
|
22
|
+
def name
|
|
23
|
+
@name ||= key.to_sym
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Serialized key, the name changed by `config.key_transform`
|
|
27
|
+
def key
|
|
28
|
+
@key ||= prepare_key
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Symbolized initial attribute method name
|
|
32
|
+
def method_name
|
|
33
|
+
@method_name ||= prepare_method_name
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Combines all options to return single block that will be used to find
|
|
37
|
+
# attribute value during serialization
|
|
38
|
+
def value_block
|
|
39
|
+
@value_block ||= prepare_value_block
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Detects value block parameters signature
|
|
43
|
+
def value_block_signature
|
|
44
|
+
@value_block_signature ||= SeregaUtils::MethodSignature.call(value_block, pos_limit: 2, keyword_args: [:ctx])
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Shows if attribute is specified to be hidden
|
|
48
|
+
def hide
|
|
49
|
+
return @hide if instance_variable_defined?(:@hide)
|
|
50
|
+
|
|
51
|
+
@hide = prepare_hide
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Shows if attribute is specified to be a one-to-many relationship
|
|
55
|
+
def many
|
|
56
|
+
return @many if instance_variable_defined?(:@many)
|
|
57
|
+
|
|
58
|
+
@many = prepare_many
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Shows specified attribute serializer
|
|
62
|
+
def serializer
|
|
63
|
+
return @serializer if instance_variable_defined?(:@serializer)
|
|
64
|
+
|
|
65
|
+
@serializer = prepare_serializer
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Preset definition of the :preset, :only, :with and :except options of
|
|
69
|
+
# a relation, nil without these options
|
|
70
|
+
def selection
|
|
71
|
+
preset, only, with, except = init_opts.values_at(:preset, :only, :with, :except)
|
|
72
|
+
return unless preset || only || with || except
|
|
73
|
+
|
|
74
|
+
SeregaPlan::Presets.definition(preset, only, with, except)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Shows the default attribute value. It is a value that replaces found nils.
|
|
9
78
|
#
|
|
10
|
-
#
|
|
79
|
+
# When custom :default is not specified, we set empty array as default when `many: true` specified
|
|
80
|
+
def default
|
|
81
|
+
return @default if instance_variable_defined?(:@default)
|
|
82
|
+
|
|
83
|
+
@default = prepare_default
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Preloads declared for this attribute, passed through as provided.
|
|
87
|
+
def preloads
|
|
88
|
+
return @preloads if instance_variable_defined?(:@preloads)
|
|
89
|
+
|
|
90
|
+
@preloads = prepare_preloads
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Shows specified batch loaders names
|
|
94
|
+
def batch_loaders
|
|
95
|
+
@batch_loaders ||= prepare_batch_loaders
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Batch loader given directly as the :batch option. It is named after the
|
|
99
|
+
# attribute and belongs to the attribute only.
|
|
100
|
+
def inline_batch_loader
|
|
101
|
+
batch = batch_option
|
|
102
|
+
return unless batch&.inline?
|
|
103
|
+
|
|
104
|
+
SeregaBatchLoader.new(name: name, block: batch.use)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# The :if, :unless, :if_value and :unless_value conditions, nil without
|
|
108
|
+
# conditions
|
|
109
|
+
def conditions
|
|
110
|
+
conditions = init_opts.slice(:if, :unless, :if_value, :unless_value)
|
|
111
|
+
conditions unless conditions.empty?
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Formatter of the :format option. A Symbol option is the name of a
|
|
115
|
+
# formatter defined with `Serega.formatter`.
|
|
116
|
+
def formatter
|
|
117
|
+
format = init_opts[:format]
|
|
118
|
+
return unless format
|
|
119
|
+
|
|
120
|
+
format.is_a?(Symbol) ? @serializer_class.formatters.fetch(format) : SeregaFormatters.build(format)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
private
|
|
124
|
+
|
|
125
|
+
def prepare_key
|
|
126
|
+
transform = @serializer_class.config.key_transform
|
|
127
|
+
name = init_name.to_sym
|
|
128
|
+
return name unless transform
|
|
129
|
+
return SeregaUtils::CamelCase.call(name) if transform == :camel_case
|
|
130
|
+
|
|
131
|
+
key = transform.call(name)
|
|
132
|
+
return key if key.is_a?(Symbol)
|
|
133
|
+
return -key if key.is_a?(String)
|
|
134
|
+
|
|
135
|
+
raise SeregaError, "config.key_transform must return a Symbol or a String, #{key.inspect} returned for #{name.inspect}"
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def prepare_value_block
|
|
139
|
+
prepare_callable_block \
|
|
140
|
+
|| prepare_const_block \
|
|
141
|
+
|| prepare_delegate_block \
|
|
142
|
+
|| prepare_batch_loader_block \
|
|
143
|
+
|| prepare_keyword_block
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def prepare_hide
|
|
147
|
+
# Return provided directly value
|
|
148
|
+
hide = init_opts[:hide]
|
|
149
|
+
return hide if (hide == true) || (hide == false)
|
|
150
|
+
|
|
151
|
+
hide_setting = config.hide_by_default
|
|
152
|
+
return true if hide_setting == true
|
|
153
|
+
return true if hide_setting == :auto && preload_or_batch?
|
|
154
|
+
|
|
155
|
+
# Return nil for undefined value which means "not hide" but allows
|
|
156
|
+
# to change this value by plugins
|
|
157
|
+
nil
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# The `hide_by_default = :auto` criterion: hide attributes that fetch extra
|
|
161
|
+
# data on demand — those declared with `:preload` or an explicit `:batch`.
|
|
11
162
|
#
|
|
12
|
-
#
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
def serializer
|
|
95
|
-
return @serializer if instance_variable_defined?(:@serializer)
|
|
96
|
-
|
|
97
|
-
@serializer = prepare_serializer
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
#
|
|
101
|
-
# Shows the default attribute value. It is a value that replaces found nils.
|
|
102
|
-
#
|
|
103
|
-
# When custom :default is not specified, we set empty array as default when `many: true` specified
|
|
104
|
-
#
|
|
105
|
-
# @return [Object] Attribute default value
|
|
106
|
-
#
|
|
107
|
-
def default
|
|
108
|
-
return @default if instance_variable_defined?(:@default)
|
|
109
|
-
|
|
110
|
-
@default = prepare_default
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
#
|
|
114
|
-
# Preloads declared for this attribute, passed through as provided.
|
|
115
|
-
#
|
|
116
|
-
# @return [Object, nil] declared preloads, or nil when the attribute has none
|
|
117
|
-
#
|
|
118
|
-
def preloads
|
|
119
|
-
return @preloads if instance_variable_defined?(:@preloads)
|
|
120
|
-
|
|
121
|
-
@preloads = prepare_preloads
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
# Shows specified batch loaders names
|
|
125
|
-
# @return [Array<Symbol>] specified serializer
|
|
126
|
-
#
|
|
127
|
-
def batch_loaders
|
|
128
|
-
@batch_loaders ||= prepare_batch_loaders
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
private
|
|
132
|
-
|
|
133
|
-
def prepare_name
|
|
134
|
-
init_name.to_sym
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
def prepare_value_block
|
|
138
|
-
init_opts[:value] \
|
|
139
|
-
|| prepare_const_block \
|
|
140
|
-
|| prepare_delegate_block \
|
|
141
|
-
|| prepare_batch_loader_block \
|
|
142
|
-
|| prepare_keyword_block
|
|
143
|
-
end
|
|
144
|
-
|
|
145
|
-
def prepare_hide
|
|
146
|
-
# Return provided directly value
|
|
147
|
-
hide = init_opts[:hide]
|
|
148
|
-
return hide if (hide == true) || (hide == false)
|
|
149
|
-
|
|
150
|
-
hide_setting = config.hide_by_default
|
|
151
|
-
return true if hide_setting == true
|
|
152
|
-
return true if hide_setting == :auto && preload_or_batch?
|
|
153
|
-
|
|
154
|
-
# Return nil for undefined value which means "not hide" but allows
|
|
155
|
-
# to change this value by plugins
|
|
156
|
-
nil
|
|
157
|
-
end
|
|
158
|
-
|
|
159
|
-
# The `hide_by_default = :auto` criterion: hide attributes that fetch extra
|
|
160
|
-
# data on demand — those declared with `:preload` or an explicit `:batch`.
|
|
161
|
-
#
|
|
162
|
-
# This is intentionally narrower than a "goes through the batch mechanism"
|
|
163
|
-
# check (`SeregaPlanPoint#batch?`): a plain relation (`serializer:` without
|
|
164
|
-
# `:preload`) is batch-processed too, but it only serializes an already-loaded
|
|
165
|
-
# nested object, so it stays visible by default.
|
|
166
|
-
def preload_or_batch?
|
|
167
|
-
!!(preloads || init_opts.key?(:batch))
|
|
168
|
-
end
|
|
169
|
-
|
|
170
|
-
def config
|
|
171
|
-
self.class.serializer_class.config
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
def prepare_many
|
|
175
|
-
init_opts[:many]
|
|
176
|
-
end
|
|
177
|
-
|
|
178
|
-
def prepare_serializer
|
|
179
|
-
block = init_block
|
|
180
|
-
return init_opts[:serializer] unless block
|
|
181
|
-
|
|
182
|
-
prepare_block_serializer(block)
|
|
183
|
-
end
|
|
184
|
-
|
|
185
|
-
# Builds an anonymous nested serializer from the attribute block.
|
|
186
|
-
#
|
|
187
|
-
# The serializer is a regular subclass of an explicitly chosen base —
|
|
188
|
-
# the :base_serializer attribute option or `config.base_serializer` —
|
|
189
|
-
# so it inherits everything the base has: plugins, config, attributes,
|
|
190
|
-
# batch loaders, the preload handler. Nested values can be any objects,
|
|
191
|
-
# which is why the base is chosen explicitly instead of inheriting from
|
|
192
|
-
# the current serializer. Its `inspect` is overridden to
|
|
193
|
-
# `CurrentSerializer.<attribute_name>` so error messages and debugging
|
|
194
|
-
# output point back to the defining attribute.
|
|
195
|
-
# Guards against cyclic definitions before building: inheriting the base
|
|
196
|
-
# serializer copies its attributes, and copying a block attribute builds
|
|
197
|
-
# its nested serializer — meeting the same block again while it is being
|
|
198
|
-
# built means the base serializer (transitively) contains the block
|
|
199
|
-
# attribute being built, so inheriting from it would recurse forever.
|
|
200
|
-
# The "in progress" mark is kept on the block itself — its identity is
|
|
201
|
-
# what defines the cycle (the same Proc object is shared by all copies
|
|
202
|
-
# of the attribute).
|
|
203
|
-
def prepare_block_serializer(block)
|
|
204
|
-
if block.instance_variable_defined?(:@serega_building_nested_serializer)
|
|
205
|
-
raise SeregaError,
|
|
206
|
-
"Can not define a nested serializer for attribute :#{name} —" \
|
|
207
|
-
" its base serializer #{block_base_serializer.inspect} (transitively)" \
|
|
208
|
-
" contains this same block attribute (cyclic definition)"
|
|
209
|
-
end
|
|
210
|
-
|
|
211
|
-
block.instance_variable_set(:@serega_building_nested_serializer, true)
|
|
212
|
-
begin
|
|
213
|
-
build_block_serializer(block)
|
|
214
|
-
ensure
|
|
215
|
-
block.remove_instance_variable(:@serega_building_nested_serializer)
|
|
216
|
-
end
|
|
217
|
-
end
|
|
218
|
-
|
|
219
|
-
def build_block_serializer(block)
|
|
220
|
-
label = "#{self.class.serializer_class.inspect}.<#{name}>"
|
|
221
|
-
|
|
222
|
-
serializer = Class.new(block_base_serializer) do
|
|
223
|
-
define_singleton_method(:inspect) { label }
|
|
224
|
-
define_singleton_method(:to_s) { label }
|
|
225
|
-
instance_exec(&block)
|
|
226
|
-
end
|
|
227
|
-
|
|
228
|
-
# An empty nested serializer means the block was intended as an
|
|
229
|
-
# old-style value block — raise the explaining error.
|
|
230
|
-
raise SeregaError, SeregaValidations::Attribute::CheckBlock::ERROR_MESSAGE if serializer.attributes.empty?
|
|
231
|
-
|
|
232
|
-
serializer
|
|
233
|
-
end
|
|
234
|
-
|
|
235
|
-
# Base class for the nested serializer. Must be chosen explicitly —
|
|
236
|
-
# usually a settings-only serializer holding plugins and configuration
|
|
237
|
-
# (e.g. `config.base_serializer = self` in an application base class).
|
|
238
|
-
def block_base_serializer
|
|
239
|
-
base_serializer = init_opts[:base_serializer] || config.base_serializer
|
|
240
|
-
return base_serializer if base_serializer
|
|
241
|
-
|
|
242
|
-
raise SeregaError,
|
|
243
|
-
"Attribute block requires a base serializer for the nested serializer." \
|
|
244
|
-
" Provide the `base_serializer: <SerializerClass>` attribute option" \
|
|
245
|
-
" or set `config.base_serializer = <SerializerClass>`"
|
|
246
|
-
end
|
|
247
|
-
|
|
248
|
-
def prepare_method_name
|
|
249
|
-
(init_opts[:method] || init_name).to_sym
|
|
250
|
-
end
|
|
251
|
-
|
|
252
|
-
def prepare_const_block
|
|
253
|
-
return unless init_opts.key?(:const)
|
|
254
|
-
|
|
255
|
-
AttributeValueResolvers::ConstResolver.get(init_opts[:const])
|
|
256
|
-
end
|
|
257
|
-
|
|
258
|
-
def prepare_keyword_block
|
|
259
|
-
mode, allow_nil = hash_access
|
|
260
|
-
return AttributeValueResolvers::KeywordResolver.get(method_name) unless mode
|
|
261
|
-
|
|
262
|
-
AttributeValueResolvers::HashAccessResolver.get(method_name, mode, allow_nil)
|
|
263
|
-
end
|
|
264
|
-
|
|
265
|
-
def prepare_batch_loader_block
|
|
266
|
-
batch_opt = init_opts[:batch]
|
|
267
|
-
return unless batch_opt
|
|
268
|
-
|
|
269
|
-
AttributeValueResolvers::BatchResolver.get(self.class.serializer_class, name, batch_opt)
|
|
270
|
-
end
|
|
271
|
-
|
|
272
|
-
# Batch loader names whose loaded data this attribute's value needs. Only
|
|
273
|
-
# explicit `:batch` attributes have any — everything else resolves its value
|
|
274
|
-
# from the record itself, so the list is empty (no loader to run).
|
|
275
|
-
def prepare_batch_loaders
|
|
276
|
-
batch_opt = init_opts[:batch]
|
|
277
|
-
return explicit_batch_loaders(batch_opt) if batch_opt
|
|
278
|
-
|
|
279
|
-
FROZEN_EMPTY_ARRAY
|
|
280
|
-
end
|
|
281
|
-
|
|
282
|
-
def explicit_batch_loaders(batch_opt)
|
|
283
|
-
if batch_opt == true || batch_opt.respond_to?(:call)
|
|
284
|
-
[name]
|
|
285
|
-
elsif batch_opt.is_a?(Symbol)
|
|
286
|
-
[batch_opt]
|
|
287
|
-
elsif batch_opt.is_a?(String)
|
|
288
|
-
[batch_opt.to_sym]
|
|
289
|
-
else
|
|
290
|
-
use_opt = batch_opt.fetch(:use)
|
|
291
|
-
use_opt.respond_to?(:call) ? [name] : Array(use_opt).map(&:to_sym)
|
|
292
|
-
end
|
|
293
|
-
end
|
|
294
|
-
|
|
295
|
-
def prepare_default
|
|
296
|
-
init_opts.fetch(:default) { many ? FROZEN_EMPTY_ARRAY : nil }
|
|
297
|
-
end
|
|
298
|
-
|
|
299
|
-
# `delegate: {to_hash_access: ...}` configures hash access for the
|
|
300
|
-
# intermediate (:to) read, `delegate: {hash_access: ...}` for the
|
|
301
|
-
# final (:method) read. A step without its sub-option keeps a plain
|
|
302
|
-
# method read.
|
|
303
|
-
def prepare_delegate_block
|
|
304
|
-
delegate = init_opts[:delegate]
|
|
305
|
-
return unless delegate
|
|
306
|
-
|
|
307
|
-
key_method_name = delegate[:method] || method_name
|
|
308
|
-
delegate_to = delegate[:to]
|
|
309
|
-
delegate_allow_nil = delegate.fetch(:allow_nil) { config.delegate_default_allow_nil }
|
|
310
|
-
|
|
311
|
-
to_access = delegate[:to_hash_access]
|
|
312
|
-
final_access = delegate[:hash_access]
|
|
313
|
-
unless to_access || final_access
|
|
314
|
-
return AttributeValueResolvers::DelegateResolver.get(delegate_to, key_method_name, delegate_allow_nil)
|
|
315
|
-
end
|
|
316
|
-
|
|
317
|
-
to_step = delegate_to_step(delegate_to, to_access)
|
|
318
|
-
final_step = delegate_final_step(key_method_name, final_access)
|
|
319
|
-
AttributeValueResolvers::HashAccessDelegateResolver.get(to_step, final_step, delegate_allow_nil)
|
|
320
|
-
end
|
|
321
|
-
|
|
322
|
-
def delegate_to_step(delegate_to, access)
|
|
323
|
-
return AttributeValueResolvers::Keyword.new(delegate_to) unless access
|
|
324
|
-
|
|
325
|
-
mode, allow_missing_key = parse_hash_access(access)
|
|
326
|
-
AttributeValueResolvers::HashAccessKeyword.new(delegate_to, mode, allow_missing_key)
|
|
327
|
-
end
|
|
328
|
-
|
|
329
|
-
def delegate_final_step(key_method_name, access)
|
|
330
|
-
return AttributeValueResolvers::Keyword.new(key_method_name) unless access
|
|
331
|
-
|
|
332
|
-
mode, allow_missing_key = parse_hash_access(access)
|
|
333
|
-
AttributeValueResolvers::HashAccessKeyword.new(key_method_name, mode, allow_missing_key)
|
|
334
|
-
end
|
|
335
|
-
|
|
336
|
-
# Resolves the :hash_access option of plain attributes
|
|
337
|
-
# @return [Array(Symbol, Boolean), nil] mode and allow_missing_key pair,
|
|
338
|
-
# or nil when hash access is not enabled for this attribute
|
|
339
|
-
def hash_access
|
|
340
|
-
option = init_opts[:hash_access]
|
|
341
|
-
return unless option
|
|
342
|
-
|
|
343
|
-
parse_hash_access(option)
|
|
344
|
-
end
|
|
345
|
-
|
|
346
|
-
# Resolves a :hash_access value (`true`, a Symbol mode or a
|
|
347
|
-
# `{mode:, allow_missing_key:}` Hash) into a mode and allow_missing_key
|
|
348
|
-
# pair. `true` and a Hash omitting :mode resolve to
|
|
349
|
-
# `config.hash_access.default_mode`.
|
|
350
|
-
def parse_hash_access(option)
|
|
351
|
-
defaults = config.hash_access
|
|
352
|
-
|
|
353
|
-
case option
|
|
354
|
-
when true then [defaults.default_mode, defaults.default_allow_missing_key]
|
|
355
|
-
when Symbol then [option, defaults.default_allow_missing_key]
|
|
356
|
-
else
|
|
357
|
-
mode = option.fetch(:mode) { defaults.default_mode }
|
|
358
|
-
allow_missing_key = option.fetch(:allow_missing_key) { defaults.default_allow_missing_key }
|
|
359
|
-
[mode, allow_missing_key]
|
|
360
|
-
end
|
|
361
|
-
end
|
|
362
|
-
|
|
363
|
-
# Prepares preloads for this attribute.
|
|
364
|
-
#
|
|
365
|
-
# The value is passed through as provided (Symbol, Array, Hash, or any
|
|
366
|
-
# custom value an ORM understands) — it is the data handed to the
|
|
367
|
-
# serializer's `preload_with` handler.
|
|
368
|
-
#
|
|
369
|
-
# @return [Object, nil] preloads as provided, or nil when the attribute has none
|
|
370
|
-
def prepare_preloads
|
|
371
|
-
# Explicit preload option (false or nil disables preloading)
|
|
372
|
-
return init_opts[:preload] || nil if init_opts.key?(:preload)
|
|
373
|
-
|
|
374
|
-
# Auto-preload the delegated association
|
|
375
|
-
if config.auto_preload.fetch(:has_delegate_option) && init_opts[:delegate]
|
|
376
|
-
return auto_preload_value(init_opts[:delegate][:to])
|
|
377
|
-
end
|
|
378
|
-
|
|
379
|
-
# Auto-preload the nested serializer's association
|
|
380
|
-
# (a block defines a nested serializer, same as the :serializer option)
|
|
381
|
-
if config.auto_preload.fetch(:has_serializer_option) && (init_opts[:serializer] || init_block) && !init_opts.key?(:batch)
|
|
382
|
-
return auto_preload_value(method_name)
|
|
383
|
-
end
|
|
384
|
-
|
|
385
|
-
nil
|
|
386
|
-
end
|
|
387
|
-
|
|
388
|
-
# Skips auto-preloading of methods that return the serialized object
|
|
389
|
-
# itself (:itself by default) — they are not associations,
|
|
390
|
-
# so preloading them would fail or make no sense.
|
|
391
|
-
def auto_preload_value(preload_method)
|
|
392
|
-
return nil if config.auto_preload_excluded_methods.include?(preload_method.to_sym)
|
|
393
|
-
|
|
394
|
-
preload_method
|
|
395
|
-
end
|
|
396
|
-
end
|
|
397
|
-
|
|
398
|
-
extend Serega::SeregaHelpers::SerializerClassHelper
|
|
399
|
-
include AttributeNormalizerInstanceMethods
|
|
163
|
+
# This is intentionally narrower than a "goes through the batch mechanism"
|
|
164
|
+
# check: a plain relation (`serializer:` without `:preload`) is
|
|
165
|
+
# batch-processed too, but it only serializes an already-loaded nested
|
|
166
|
+
# object, so it stays visible by default.
|
|
167
|
+
def preload_or_batch?
|
|
168
|
+
!!(preloads || init_opts.key?(:batch))
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def config
|
|
172
|
+
@serializer_class.config
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def prepare_many
|
|
176
|
+
init_opts[:many]
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def prepare_serializer
|
|
180
|
+
block = init_block
|
|
181
|
+
return init_opts[:serializer] unless block
|
|
182
|
+
|
|
183
|
+
AttributeNormalizers::Block.call(@serializer_class, name, block, init_opts[:base_serializer])
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def prepare_method_name
|
|
187
|
+
(init_opts[:method] || init_name).to_sym
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def prepare_callable_block
|
|
191
|
+
callable = init_opts[:value]
|
|
192
|
+
callable && AttributeValueResolvers::Callable.new(callable)
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def prepare_const_block
|
|
196
|
+
return unless init_opts.key?(:const)
|
|
197
|
+
|
|
198
|
+
AttributeValueResolvers::Const.new(init_opts[:const])
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def prepare_keyword_block
|
|
202
|
+
AttributeNormalizers::HashAccess.call(init_opts[:hash_access], method_name, config.hash_access)
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Reads the value from the loaded batch of the only batch loader
|
|
206
|
+
def prepare_batch_loader_block
|
|
207
|
+
batch = batch_option
|
|
208
|
+
return unless batch
|
|
209
|
+
|
|
210
|
+
AttributeValueResolvers::Batch.new(batch_loaders.first, batch.id || config.batch_id_option)
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# Batch loader names whose loaded data this attribute's value needs. An
|
|
214
|
+
# attribute without the :batch option has none.
|
|
215
|
+
def prepare_batch_loaders
|
|
216
|
+
batch = batch_option
|
|
217
|
+
return FROZEN_EMPTY_ARRAY unless batch
|
|
218
|
+
|
|
219
|
+
batch.loader_names
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# The parsed :batch option, `nil` without it
|
|
223
|
+
def batch_option
|
|
224
|
+
return @batch_option if instance_variable_defined?(:@batch_option)
|
|
225
|
+
|
|
226
|
+
batch = init_opts[:batch]
|
|
227
|
+
@batch_option = batch && AttributeNormalizers::Batch.new(batch, name)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def prepare_default
|
|
231
|
+
init_opts.fetch(:default) { many ? FROZEN_EMPTY_ARRAY : nil }
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def prepare_delegate_block
|
|
235
|
+
delegate = init_opts[:delegate]
|
|
236
|
+
return unless delegate
|
|
237
|
+
|
|
238
|
+
AttributeNormalizers::Delegate.call(delegate, method_name, config)
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def prepare_preloads
|
|
242
|
+
relation = !serializer.nil?
|
|
243
|
+
AttributeNormalizers::Preloads.call(init_opts, relation, method_name, config)
|
|
244
|
+
end
|
|
400
245
|
end
|
|
401
246
|
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
#
|
|
5
|
+
# Parsers of attribute options, used by SeregaAttributeNormalizer
|
|
6
|
+
#
|
|
7
|
+
module AttributeNormalizers # :nodoc:
|
|
8
|
+
#
|
|
9
|
+
# The attribute :batch option, parsed into the loaders and the id method.
|
|
10
|
+
# `true`, a loader name, a callable or a Hash with `:use` and `:id`; `true`
|
|
11
|
+
# and a Hash without `:use` use the loader named after the attribute.
|
|
12
|
+
#
|
|
13
|
+
class Batch # :nodoc:
|
|
14
|
+
# Name of the attribute with the option
|
|
15
|
+
attr_reader :attribute_name
|
|
16
|
+
|
|
17
|
+
# Loader name, Array of loader names, or a callable inline loader
|
|
18
|
+
attr_reader :use
|
|
19
|
+
|
|
20
|
+
# Method that gives the id of an object, `nil` when not given
|
|
21
|
+
attr_reader :id
|
|
22
|
+
|
|
23
|
+
# Parses the :batch option of the attribute
|
|
24
|
+
def initialize(batch, attribute_name)
|
|
25
|
+
batch = {use: batch} unless batch.is_a?(Hash)
|
|
26
|
+
use = batch.fetch(:use, true)
|
|
27
|
+
|
|
28
|
+
@attribute_name = attribute_name
|
|
29
|
+
@use = (use == true) ? attribute_name : use
|
|
30
|
+
@id = batch[:id]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Whether the option is the loader itself, a callable
|
|
34
|
+
def inline?
|
|
35
|
+
use.respond_to?(:call)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Names of the batch loaders the attribute value needs. An inline loader
|
|
39
|
+
# is named after the attribute.
|
|
40
|
+
def loader_names
|
|
41
|
+
inline? ? [attribute_name.to_sym] : Array(use).map(&:to_sym)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|