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
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
0.51.0
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
class SeregaAttribute # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# The :serializer option of an attribute: a serializer class, the name
|
|
7
|
+
# of a serializer class, or a Proc that returns a serializer class. The
|
|
8
|
+
# name and the Proc let serializers refer to classes defined later.
|
|
9
|
+
#
|
|
10
|
+
class SerializerReference # :nodoc:
|
|
11
|
+
def initialize(serializer)
|
|
12
|
+
@serializer = serializer
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Returns the serializer class. Resolves a name or a Proc once, and
|
|
16
|
+
# keeps the class. A frozen reference, shared between Ractors,
|
|
17
|
+
# resolves it on each call.
|
|
18
|
+
def resolve
|
|
19
|
+
serializer = @serializer
|
|
20
|
+
return serializer if serializer.is_a?(Class) && serializer < Serega
|
|
21
|
+
|
|
22
|
+
serializer = serializer.is_a?(String) ? Object.const_get(serializer, false) : serializer.call
|
|
23
|
+
frozen? ? serializer : (@serializer = serializer)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
class SeregaAttribute # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# Ruby code that reads the value of an attribute from the `source`
|
|
7
|
+
# variable of generated code. The `:const` and `:default` values and
|
|
8
|
+
# the formatters come from the
|
|
9
|
+
# `CONSTANTS`, `DEFAULTS`, `FORMATTERS` and `FORMATTER_ARGS` of
|
|
10
|
+
# SeregaAttributeValues, which `constants_prefix` names.
|
|
11
|
+
#
|
|
12
|
+
class ValueCode # :nodoc:
|
|
13
|
+
def initialize(name, value_block, default, formatter)
|
|
14
|
+
@name = name
|
|
15
|
+
@value_block = value_block
|
|
16
|
+
@default = default
|
|
17
|
+
@formatter = formatter
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Code that reads the value of the source with a resolver, nil
|
|
21
|
+
# for a :value callable and for a resolver without code
|
|
22
|
+
def plain(constants_prefix = "")
|
|
23
|
+
resolver = @value_block
|
|
24
|
+
return unless resolver.is_a?(AttributeValueResolvers::Base)
|
|
25
|
+
|
|
26
|
+
code = resolver.const? ? "#{constants_prefix}CONSTANTS[#{@name.inspect}]" : resolver.code("source")
|
|
27
|
+
code && finish(code, constants_prefix)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Code that calls the :value callable, which `callable_variable`
|
|
31
|
+
# holds. `batches_variable` holds the loaded batches. nil without the
|
|
32
|
+
# :value option.
|
|
33
|
+
def callable(callable_variable, batches_variable, constants_prefix)
|
|
34
|
+
callable = @value_block
|
|
35
|
+
return unless callable.is_a?(AttributeValueResolvers::Callable)
|
|
36
|
+
|
|
37
|
+
finish(callable.code("source", callable_variable, batches_variable), constants_prefix)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Code that reads the value of a :batch attribute without the :value
|
|
41
|
+
# option from the loaded batch, which `batch_variable` holds. nil for
|
|
42
|
+
# other attributes.
|
|
43
|
+
def batch(batch_variable, constants_prefix)
|
|
44
|
+
resolver = @value_block
|
|
45
|
+
return unless resolver.is_a?(AttributeValueResolvers::Batch)
|
|
46
|
+
|
|
47
|
+
finish(resolver.batch_code("source", batch_variable), constants_prefix)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
# Adds the `:default` value and the formatter to the code that reads
|
|
53
|
+
# the value. nil for a formatter that takes the context.
|
|
54
|
+
def finish(code, constants_prefix)
|
|
55
|
+
name = @name.inspect
|
|
56
|
+
code = "(value = #{code}).nil? ? #{constants_prefix}DEFAULTS[#{name}] : value" unless @default.nil?
|
|
57
|
+
return code unless @formatter
|
|
58
|
+
|
|
59
|
+
@formatter.code(code, "#{constants_prefix}FORMATTERS[#{name}]", "#{constants_prefix}FORMATTER_ARGS[#{name}]")
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
data/lib/serega/attribute.rb
CHANGED
|
@@ -4,148 +4,181 @@ class Serega
|
|
|
4
4
|
#
|
|
5
5
|
# Stores serialized attribute data
|
|
6
6
|
#
|
|
7
|
-
#
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
class SeregaAttribute # :nodoc:
|
|
8
|
+
# Serializer of the attribute
|
|
9
|
+
attr_reader :serializer_class
|
|
10
|
+
|
|
11
|
+
# Attribute initial params
|
|
12
|
+
attr_reader :initials
|
|
13
|
+
|
|
14
|
+
# Attribute name
|
|
15
|
+
attr_reader :name
|
|
16
|
+
|
|
17
|
+
# Serialized key, a Symbol or a String
|
|
18
|
+
attr_reader :key
|
|
19
|
+
|
|
20
|
+
# Where the attribute is defined
|
|
21
|
+
def location
|
|
22
|
+
initials[:location]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Attribute :many option
|
|
26
|
+
attr_reader :many
|
|
27
|
+
|
|
28
|
+
# Preset definition of the :preset, :only, :with and :except options of
|
|
29
|
+
# a relation, nil without these options
|
|
30
|
+
attr_reader :selection
|
|
31
|
+
|
|
32
|
+
# Attribute :default option
|
|
33
|
+
attr_reader :default
|
|
34
|
+
|
|
35
|
+
# Attribute :hide option
|
|
36
|
+
attr_reader :hide
|
|
37
|
+
|
|
38
|
+
# Attribute :preloads option
|
|
39
|
+
attr_reader :preloads
|
|
40
|
+
|
|
41
|
+
# Batch loader names required to detect attribute value
|
|
42
|
+
attr_reader :batch_loaders
|
|
43
|
+
|
|
44
|
+
# Batch loader given directly as the :batch option
|
|
45
|
+
attr_reader :inline_batch_loader
|
|
46
|
+
|
|
47
|
+
# Attribute :if, :unless, :if_value and :unless_value conditions
|
|
48
|
+
attr_reader :conditions
|
|
49
|
+
|
|
50
|
+
# Initializes new attribute
|
|
51
|
+
def initialize(serializer_class, name:, opts: {}, block: nil, location: nil)
|
|
52
|
+
SeregaValidations::CheckAttributeParams.new(serializer_class, name, opts, block).validate
|
|
53
|
+
|
|
54
|
+
@serializer_class = serializer_class
|
|
55
|
+
@initials = SeregaUtils::EnumDeepFreeze.call(
|
|
56
|
+
name: name,
|
|
57
|
+
opts: SeregaUtils::EnumDeepDup.call(opts),
|
|
58
|
+
block: block,
|
|
59
|
+
location: location
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
normalizer = SeregaAttributeNormalizer.new(serializer_class, initials)
|
|
63
|
+
@name = normalizer.name
|
|
64
|
+
@key = normalizer.key
|
|
65
|
+
@many = normalizer.many
|
|
66
|
+
@default = normalizer.default
|
|
67
|
+
@value_block = normalizer.value_block
|
|
68
|
+
@value_block_signature = normalizer.value_block_signature
|
|
69
|
+
@value_callable = @value_block.is_a?(AttributeValueResolvers::Callable) ? @value_block.callable : nil
|
|
70
|
+
@hide = normalizer.hide
|
|
71
|
+
serializer = normalizer.serializer
|
|
72
|
+
@serializer = serializer && SerializerReference.new(serializer)
|
|
73
|
+
@selection = normalizer.selection
|
|
74
|
+
@preloads = normalizer.preloads
|
|
75
|
+
@batch_loaders = normalizer.batch_loaders
|
|
76
|
+
@inline_batch_loader = normalizer.inline_batch_loader
|
|
77
|
+
@formatter = normalizer.formatter
|
|
78
|
+
@value_code = ValueCode.new(@name, @value_block, @default, @formatter)
|
|
79
|
+
conditions = normalizer.conditions
|
|
80
|
+
@conditions = conditions && SeregaConditions.new(self, conditions)
|
|
81
|
+
store_values(serializer_class)
|
|
82
|
+
@reads_presenter = false
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Method of the serialized object that gives the value, `nil` for a value
|
|
86
|
+
# from :value, :const, :delegate, :hash_access or :batch
|
|
87
|
+
def object_method
|
|
88
|
+
@value_block.object_method if resolver?
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Ruby code that reads the value in generated code
|
|
92
|
+
attr_reader :value_code
|
|
93
|
+
|
|
94
|
+
# The :value callable, nil without the :value option
|
|
95
|
+
attr_reader :value_callable
|
|
96
|
+
|
|
97
|
+
# Methods that reading the value and checking the :if and :unless
|
|
98
|
+
# conditions call on the object, nil when a callable or a batch loader
|
|
99
|
+
# gets the object
|
|
100
|
+
def source_methods
|
|
101
|
+
return unless resolver?
|
|
102
|
+
|
|
103
|
+
methods = @value_block.source_methods
|
|
104
|
+
return methods unless methods && @conditions
|
|
105
|
+
|
|
106
|
+
condition_methods = @conditions.source_methods
|
|
107
|
+
condition_methods && (methods + condition_methods)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Notes whether the attribute reads the object through the presenter: it
|
|
111
|
+
# calls a presenter method, or it gives the object to a callable or a
|
|
112
|
+
# batch loader. The serializer calls it when it is locked, because
|
|
113
|
+
# presenter methods can be added after the attribute.
|
|
114
|
+
def check_presenter(presenter_class)
|
|
115
|
+
methods = source_methods
|
|
116
|
+
@reads_presenter = methods.nil? || presenter_class.presents_any?(methods)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Whether the attribute reads the object through the presenter, known
|
|
120
|
+
# after the serializer is locked
|
|
121
|
+
def reads_presenter?
|
|
122
|
+
@reads_presenter
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Whether the attribute has an :if, :unless, :if_value or :unless_value condition
|
|
126
|
+
def conditional?
|
|
127
|
+
!@conditions.nil?
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Shows whether attribute has specified serializer
|
|
131
|
+
def relation?
|
|
132
|
+
!@serializer.nil?
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Serializer class of the :serializer option or the block, nil without
|
|
136
|
+
# a relation
|
|
137
|
+
def serializer
|
|
138
|
+
@serializer&.resolve
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# Finds attribute value
|
|
11
142
|
#
|
|
12
|
-
#
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
attr_reader :hide
|
|
33
|
-
|
|
34
|
-
# Attribute :preloads option
|
|
35
|
-
# @return [Hash, nil] Attribute :preloads option
|
|
36
|
-
attr_reader :preloads
|
|
37
|
-
|
|
38
|
-
# Batch loader names required to detect attribute value
|
|
39
|
-
# @return [Array<Symbol>] Batch loader names
|
|
40
|
-
attr_reader :batch_loaders
|
|
41
|
-
|
|
42
|
-
#
|
|
43
|
-
# Initializes new attribute
|
|
44
|
-
#
|
|
45
|
-
# @param name [Symbol, String] Name of attribute
|
|
46
|
-
# @param opts [Hash] Attribute options
|
|
47
|
-
# @option opts [Symbol] :method Object method name to fetch attribute value
|
|
48
|
-
# @option opts [Hash] :delegate Allows to fetch value from nested object
|
|
49
|
-
# @option opts [Boolean] :hide Specify `true` to not serialize this attribute by default
|
|
50
|
-
# @option opts [Boolean] :many Specifies has_many relationship. By default is detected via object.is_a?(Enumerable) && !object.is_a?(Hash) && !object.is_a?(Struct)
|
|
51
|
-
# @option opts [Proc, #call] :value Custom block or callable to find attribute value
|
|
52
|
-
# @option opts [Serega, Proc] :serializer Relationship serializer class. Use `proc { MySerializer }` if serializers have cross references
|
|
53
|
-
# @param block [Proc] Defines attributes of a nested anonymous serializer
|
|
54
|
-
#
|
|
55
|
-
def initialize(name:, opts: {}, block: nil)
|
|
56
|
-
serializer_class = self.class.serializer_class
|
|
57
|
-
serializer_class::CheckAttributeParams.new(name, opts, block).validate
|
|
58
|
-
|
|
59
|
-
@initials = SeregaUtils::EnumDeepFreeze.call(
|
|
60
|
-
name: name,
|
|
61
|
-
opts: SeregaUtils::EnumDeepDup.call(opts),
|
|
62
|
-
block: block
|
|
63
|
-
)
|
|
64
|
-
|
|
65
|
-
normalizer = serializer_class::SeregaAttributeNormalizer.new(initials)
|
|
66
|
-
set_normalized_vars(normalizer)
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
# Shows whether attribute has specified serializer
|
|
70
|
-
# @return [Boolean] Checks if attribute is relationship (if :serializer option exists)
|
|
71
|
-
def relation?
|
|
72
|
-
!@serializer.nil?
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
# Shows specified serializer class
|
|
76
|
-
# @return [Serega, nil] Attribute serializer if exists
|
|
77
|
-
def serializer
|
|
78
|
-
serializer = @serializer
|
|
79
|
-
return serializer if (serializer.is_a?(Class) && (serializer < Serega)) || !serializer
|
|
80
|
-
|
|
81
|
-
@serializer = serializer.is_a?(String) ? Object.const_get(serializer, false) : serializer.call
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
#
|
|
85
|
-
# Finds attribute value
|
|
86
|
-
#
|
|
87
|
-
# @param object [Object] Serialized object
|
|
88
|
-
# @param context [Hash, nil] Serialization context
|
|
89
|
-
#
|
|
90
|
-
# @return [Object] Serialized attribute value
|
|
91
|
-
#
|
|
92
|
-
def value(object, context, batches: nil)
|
|
93
|
-
# Signatures should match allowed signatures in CheckOptValue
|
|
94
|
-
result =
|
|
95
|
-
case value_block_signature
|
|
96
|
-
when "1" then value_block.call(object)
|
|
97
|
-
when "1_ctx" then value_block.call(object, ctx: context)
|
|
98
|
-
when "1_batches" then value_block.call(object, batches: batches)
|
|
99
|
-
when "1_batches_ctx" then value_block.call(object, ctx: context, batches: batches)
|
|
100
|
-
when "2" then value_block.call(object, context)
|
|
101
|
-
when "2_batches_ctx" then value_block.call(object, context, ctx: context, batches: batches)
|
|
102
|
-
else value_block.call # signature is "0" - no parameters
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
result.nil? ? default : result
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
#
|
|
109
|
-
# Checks if attribute must be added to serialized response
|
|
110
|
-
#
|
|
111
|
-
# @param modifiers [Hash] Serialization modifiers
|
|
112
|
-
# @option modifiers [Hash] :only The only attributes to serialize
|
|
113
|
-
# @option modifiers [Hash] :except Attributes to hide
|
|
114
|
-
# @option modifiers [Hash] :with Hidden attributes to serialize additionally
|
|
115
|
-
#
|
|
116
|
-
# @return [Boolean]
|
|
117
|
-
#
|
|
118
|
-
def visible?(modifiers)
|
|
119
|
-
except = modifiers[:except] || FROZEN_EMPTY_HASH
|
|
120
|
-
only = modifiers[:only] || FROZEN_EMPTY_HASH
|
|
121
|
-
with = modifiers[:with] || FROZEN_EMPTY_HASH
|
|
122
|
-
|
|
123
|
-
return false if except.member?(name) && except[name].empty?
|
|
124
|
-
return true if only.member?(name)
|
|
125
|
-
return true if with.member?(name)
|
|
126
|
-
return false unless only.empty?
|
|
127
|
-
|
|
128
|
-
!hide
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
private
|
|
132
|
-
|
|
133
|
-
attr_reader :value_block, :value_block_signature
|
|
134
|
-
|
|
135
|
-
def set_normalized_vars(normalizer)
|
|
136
|
-
@name = normalizer.name
|
|
137
|
-
@many = normalizer.many
|
|
138
|
-
@default = normalizer.default
|
|
139
|
-
@value_block = normalizer.value_block
|
|
140
|
-
@value_block_signature = normalizer.value_block_signature
|
|
141
|
-
@hide = normalizer.hide
|
|
142
|
-
@serializer = normalizer.serializer
|
|
143
|
-
@preloads = normalizer.preloads
|
|
144
|
-
@batch_loaders = normalizer.batch_loaders
|
|
145
|
-
end
|
|
143
|
+
# Generated code reads plain attributes and :value callables without
|
|
144
|
+
# this method, with their #value_code.
|
|
145
|
+
def value(object, context, batches: nil)
|
|
146
|
+
result =
|
|
147
|
+
case @value_block_signature
|
|
148
|
+
when "1" then @value_block.call(object) # resolvers that read the object
|
|
149
|
+
when "1_batches" then @value_block.call(object, batches: batches) # the Batch resolver
|
|
150
|
+
when "1_batches_ctx" then @value_block.call(object, ctx: context, batches: batches) # Callable
|
|
151
|
+
else @value_block.call # the Const resolver
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
result = @default if result.nil?
|
|
155
|
+
@formatter ? @formatter.call(result, context) : result
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Defines the methods of the code on the owner, at the file and line of
|
|
159
|
+
# the attribute, thus their backtraces point to the attribute
|
|
160
|
+
def define_methods(owner, code)
|
|
161
|
+
file, _, line = location ? location.rpartition(":") : ["(attribute #{name})", nil, "1"]
|
|
162
|
+
owner.class_eval(code, file, line.to_i)
|
|
146
163
|
end
|
|
147
164
|
|
|
148
|
-
|
|
149
|
-
|
|
165
|
+
private
|
|
166
|
+
|
|
167
|
+
# Whether the value comes from a resolver that Serega built from the
|
|
168
|
+
# options, and not from a `:value` callable of the user
|
|
169
|
+
def resolver?
|
|
170
|
+
@value_block.is_a?(AttributeValueResolvers::Base)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# Stores the values of the attribute that generated code reads in the
|
|
174
|
+
# SeregaAttributeValues of the serializer: the `:const` and `:default`
|
|
175
|
+
# values and the formatter
|
|
176
|
+
def store_values(serializer_class)
|
|
177
|
+
values_class = serializer_class::SeregaAttributeValues
|
|
178
|
+
values_class::CONSTANTS[name] = @value_block.call if resolver? && @value_block.const?
|
|
179
|
+
values_class::DEFAULTS[name] = @default unless @default.nil?
|
|
180
|
+
values_class::FORMATTERS[name] = @formatter.callable if @formatter&.callable
|
|
181
|
+
values_class::FORMATTER_ARGS[name] = @formatter.args if @formatter&.args
|
|
182
|
+
end
|
|
150
183
|
end
|
|
151
184
|
end
|