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
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module AttributeNormalizers # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# Builds an anonymous nested serializer from the attribute block.
|
|
7
|
+
#
|
|
8
|
+
# The serializer is a regular subclass of an explicitly chosen base —
|
|
9
|
+
# the :base_serializer attribute option or `config.base_serializer` —
|
|
10
|
+
# so it inherits everything the base has: plugins, config, attributes,
|
|
11
|
+
# batch loaders, the preload handler. Nested values can be any objects,
|
|
12
|
+
# which is why the base is chosen explicitly instead of inheriting from
|
|
13
|
+
# the current serializer. Its `inspect` is overridden to
|
|
14
|
+
# `CurrentSerializer.<attribute_name>` so error messages and debugging
|
|
15
|
+
# output point back to the defining attribute.
|
|
16
|
+
#
|
|
17
|
+
class Block # :nodoc:
|
|
18
|
+
class << self
|
|
19
|
+
# Builds the nested serializer of the attribute block. `base_serializer`
|
|
20
|
+
# is the :base_serializer attribute option.
|
|
21
|
+
#
|
|
22
|
+
# Guards against cyclic definitions before building: inheriting the base
|
|
23
|
+
# serializer copies its attributes, and copying a block attribute builds
|
|
24
|
+
# its nested serializer — meeting the same block again while it is being
|
|
25
|
+
# built means the base serializer (transitively) contains the block
|
|
26
|
+
# attribute being built, so inheriting from it would recurse forever.
|
|
27
|
+
# The "in progress" mark is kept on the block itself — its identity is
|
|
28
|
+
# what defines the cycle (the same Proc object is shared by all copies
|
|
29
|
+
# of the attribute).
|
|
30
|
+
def call(serializer_class, attribute_name, block, base_serializer)
|
|
31
|
+
base_serializer = chosen_base_serializer(serializer_class, base_serializer)
|
|
32
|
+
|
|
33
|
+
if block.instance_variable_defined?(:@serega_building_nested_serializer)
|
|
34
|
+
raise SeregaError,
|
|
35
|
+
"Can not define a nested serializer for attribute :#{attribute_name} —" \
|
|
36
|
+
" its base serializer #{base_serializer.inspect} (transitively)" \
|
|
37
|
+
" contains this same block attribute (cyclic definition)"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
block.instance_variable_set(:@serega_building_nested_serializer, true)
|
|
41
|
+
begin
|
|
42
|
+
build(serializer_class, attribute_name, block, base_serializer)
|
|
43
|
+
ensure
|
|
44
|
+
block.remove_instance_variable(:@serega_building_nested_serializer)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def build(serializer_class, attribute_name, block, base_serializer)
|
|
51
|
+
label = "#{serializer_class.inspect}.<#{attribute_name}>"
|
|
52
|
+
|
|
53
|
+
serializer = Class.new(base_serializer) do
|
|
54
|
+
define_singleton_method(:inspect) { label }
|
|
55
|
+
define_singleton_method(:to_s) { label }
|
|
56
|
+
instance_exec(&block)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# An empty nested serializer means the block was intended as an
|
|
60
|
+
# old-style value block — raise the explaining error.
|
|
61
|
+
raise SeregaError, SeregaValidations::Attribute::CheckBlock::ERROR_MESSAGE if serializer.attributes.empty?
|
|
62
|
+
|
|
63
|
+
serializer
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Base class for the nested serializer. Must be chosen explicitly —
|
|
67
|
+
# usually a settings-only serializer holding plugins and configuration
|
|
68
|
+
# (e.g. `config.base_serializer = self` in an application base class).
|
|
69
|
+
def chosen_base_serializer(serializer_class, base_serializer)
|
|
70
|
+
base_serializer ||= serializer_class.config.base_serializer
|
|
71
|
+
return base_serializer if base_serializer
|
|
72
|
+
|
|
73
|
+
raise SeregaError,
|
|
74
|
+
"Attribute block requires a base serializer for the nested serializer." \
|
|
75
|
+
" Provide the `base_serializer: <SerializerClass>` attribute option" \
|
|
76
|
+
" or set `config.base_serializer = <SerializerClass>`"
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module AttributeNormalizers # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# Builds the value resolver of the attribute :delegate option
|
|
7
|
+
#
|
|
8
|
+
class Delegate # :nodoc:
|
|
9
|
+
class << self
|
|
10
|
+
# Resolver that reads `delegate[:to]` of the object, then
|
|
11
|
+
# `delegate[:method]` (the attribute method by default) of the result.
|
|
12
|
+
# `delegate: {to_hash_access: ...}` configures hash access for the
|
|
13
|
+
# intermediate (:to) read, `delegate: {hash_access: ...}` for the final
|
|
14
|
+
# (:method) read. A step without its sub-option keeps a plain method
|
|
15
|
+
# read.
|
|
16
|
+
def call(delegate, method_name, config)
|
|
17
|
+
key_method_name = delegate[:method] || method_name
|
|
18
|
+
delegate_to = delegate[:to]
|
|
19
|
+
allow_nil = delegate.fetch(:allow_nil) { config.delegate_default_allow_nil }
|
|
20
|
+
|
|
21
|
+
to_access = delegate[:to_hash_access]
|
|
22
|
+
final_access = delegate[:hash_access]
|
|
23
|
+
unless to_access || final_access
|
|
24
|
+
return AttributeValueResolvers::DelegateResolver.get(delegate_to, key_method_name, allow_nil)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
to_step = HashAccess.call(to_access, delegate_to, config.hash_access)
|
|
28
|
+
final_step = HashAccess.call(final_access, key_method_name, config.hash_access)
|
|
29
|
+
AttributeValueResolvers::HashAccessDelegateResolver.get(to_step, final_step, allow_nil)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module AttributeNormalizers # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# Builds the value resolver of one read: a method call, or a Hash read when
|
|
7
|
+
# a :hash_access option is given
|
|
8
|
+
#
|
|
9
|
+
class HashAccess # :nodoc:
|
|
10
|
+
class << self
|
|
11
|
+
# Resolver that reads `key` of the object. `option` is a :hash_access
|
|
12
|
+
# value: `true`, a Symbol mode or a `{mode:, allow_missing_key:}` Hash;
|
|
13
|
+
# `true` and a Hash without a key take `config.hash_access` defaults.
|
|
14
|
+
# Without the option the resolver calls the method `key`.
|
|
15
|
+
def call(option, key, defaults)
|
|
16
|
+
return AttributeValueResolvers::Keyword.new(key) unless option
|
|
17
|
+
|
|
18
|
+
mode, allow_missing_key = parse(option, defaults)
|
|
19
|
+
AttributeValueResolvers::HashAccessKeyword.new(key, mode, allow_missing_key)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def parse(option, defaults)
|
|
25
|
+
case option
|
|
26
|
+
when true then [defaults.default_mode, defaults.default_allow_missing_key]
|
|
27
|
+
when Symbol then [option, defaults.default_allow_missing_key]
|
|
28
|
+
else
|
|
29
|
+
mode = option.fetch(:mode) { defaults.default_mode }
|
|
30
|
+
allow_missing_key = option.fetch(:allow_missing_key) { defaults.default_allow_missing_key }
|
|
31
|
+
[mode, allow_missing_key]
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module AttributeNormalizers # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# Finds the preloads of an attribute
|
|
7
|
+
#
|
|
8
|
+
class Preloads # :nodoc:
|
|
9
|
+
class << self
|
|
10
|
+
# Preloads of the attribute with options `opts`. `relation` is true
|
|
11
|
+
# for an attribute with a nested serializer: the :serializer option
|
|
12
|
+
# or a block.
|
|
13
|
+
#
|
|
14
|
+
# The value is passed through as provided (Symbol, Array, Hash, or any
|
|
15
|
+
# custom value an ORM understands) — it is the data handed to the
|
|
16
|
+
# serializer's `preload_with` handler.
|
|
17
|
+
def call(opts, relation, method_name, config)
|
|
18
|
+
# Explicit preload option (false or nil disables preloading)
|
|
19
|
+
return opts[:preload] || nil if opts.key?(:preload)
|
|
20
|
+
|
|
21
|
+
# Auto-preload the delegated association
|
|
22
|
+
if config.auto_preload.fetch(:has_delegate_option) && opts[:delegate]
|
|
23
|
+
return auto_preload_value(opts[:delegate][:to], config)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Auto-preload the nested serializer's association
|
|
27
|
+
if config.auto_preload.fetch(:has_serializer_option) && relation && !opts.key?(:batch)
|
|
28
|
+
return auto_preload_value(method_name, config)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
nil
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
# Skips auto-preloading of methods that return the object
|
|
37
|
+
# itself (:itself by default) — they are not associations,
|
|
38
|
+
# so preloading them would fail or make no sense.
|
|
39
|
+
def auto_preload_value(preload_method, config)
|
|
40
|
+
return nil if config.auto_preload_excluded_methods.include?(preload_method.to_sym)
|
|
41
|
+
|
|
42
|
+
preload_method
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
#
|
|
5
|
+
# Attribute value resolvers
|
|
6
|
+
#
|
|
7
|
+
module AttributeValueResolvers # :nodoc:
|
|
8
|
+
#
|
|
9
|
+
# Parent class of the value resolvers that Serega builds from the
|
|
10
|
+
# attribute options. A `:value` callable of the user is no resolver, thus
|
|
11
|
+
# SeregaAttribute asks only resolvers for their code.
|
|
12
|
+
#
|
|
13
|
+
class Base # :nodoc:
|
|
14
|
+
# Ruby code that reads the value of the source, nil when the value
|
|
15
|
+
# has no code
|
|
16
|
+
def code(_source_variable)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Methods that the value read calls on the source, nil when the
|
|
20
|
+
# source reaches other code
|
|
21
|
+
def source_methods
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Method of the object that gives the value, nil when no method of
|
|
25
|
+
# the object gives it
|
|
26
|
+
def object_method
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Whether the value is the `:const` option
|
|
30
|
+
def const?
|
|
31
|
+
false
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -4,70 +4,34 @@ class Serega
|
|
|
4
4
|
#
|
|
5
5
|
# Attribute value resolvers
|
|
6
6
|
#
|
|
7
|
-
#
|
|
8
|
-
module AttributeValueResolvers
|
|
7
|
+
module AttributeValueResolvers # :nodoc:
|
|
9
8
|
#
|
|
10
|
-
#
|
|
9
|
+
# Value resolver of attributes with the :batch option and without the
|
|
10
|
+
# :value option. Reads the value of the object from the loaded batch.
|
|
11
11
|
#
|
|
12
|
-
#
|
|
13
|
-
class BatchResolver
|
|
14
|
-
#
|
|
15
|
-
# Generates callable block to find attribute value when attribute with :batch
|
|
16
|
-
# option has no block or manual :value option.
|
|
17
|
-
#
|
|
18
|
-
# In other cases we should never get here as attribute value/block option must be manually defined.
|
|
19
|
-
#
|
|
20
|
-
# It handles this cases:
|
|
21
|
-
# - `attribute :foo, batch: true`
|
|
22
|
-
# - `attribute :foo, batch: FooLoader`
|
|
23
|
-
# - `attribute :foo, batch: :foo_loader`
|
|
24
|
-
# - `attribute :foo, batch: { id: :foo_id }`
|
|
25
|
-
# - `attribute :foo, batch: { use: FooLoader, id: foo_id }`
|
|
26
|
-
# - `attribute :foo, batch: { use: :foo_loader, id: foo_id }`
|
|
27
|
-
#
|
|
28
|
-
def self.get(serializer_class, attribute_name, batch_opt)
|
|
29
|
-
default_method = serializer_class.config.batch_id_option
|
|
30
|
-
|
|
31
|
-
if batch_opt == true # ex: `batch: true`
|
|
32
|
-
batch_name = attribute_name
|
|
33
|
-
batch_id_method = default_method
|
|
34
|
-
elsif batch_opt.respond_to?(:call) # ex: `batch: FooLoader`
|
|
35
|
-
serializer_class.batch(attribute_name, batch_opt)
|
|
36
|
-
batch_name = attribute_name
|
|
37
|
-
batch_id_method = default_method
|
|
38
|
-
elsif batch_opt.is_a?(Symbol) || batch_opt.is_a?(String) # ex: `batch: :foo_loader`
|
|
39
|
-
batch_name = batch_opt.to_sym
|
|
40
|
-
batch_id_method = default_method
|
|
41
|
-
else
|
|
42
|
-
use = batch_opt[:use]
|
|
43
|
-
batch_id_method = batch_opt[:id] || default_method
|
|
44
|
-
|
|
45
|
-
if use.respond_to?(:call) # ex: `batch: { use: FooLoader }`
|
|
46
|
-
batch_name = attribute_name
|
|
47
|
-
serializer_class.batch(batch_name, use)
|
|
48
|
-
else # ex: `batch: { use: :foo }` || batch: { id: :some_id }
|
|
49
|
-
batch_name = use || attribute_name
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
Batch.new(batch_name, batch_id_method)
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
#
|
|
58
|
-
# Builds value resolver class for attributes with :batch option
|
|
59
|
-
#
|
|
60
|
-
# @private
|
|
61
|
-
class Batch
|
|
12
|
+
class Batch < Base # :nodoc:
|
|
62
13
|
def initialize(loader_name, id_method)
|
|
63
14
|
@loader_name = loader_name
|
|
64
15
|
@id_method = id_method
|
|
65
16
|
end
|
|
66
17
|
|
|
67
|
-
# Finds object attribute value
|
|
18
|
+
# Finds the object attribute value in the loaded batches
|
|
68
19
|
def call(obj, batches:)
|
|
69
20
|
batches.fetch(@loader_name)[obj.public_send(@id_method)]
|
|
70
21
|
end
|
|
22
|
+
|
|
23
|
+
# Ruby code that reads the value of the source from the loaded batch
|
|
24
|
+
# of the loader, which `batch_variable` holds
|
|
25
|
+
def batch_code(source_variable, batch_variable)
|
|
26
|
+
id_method = @id_method
|
|
27
|
+
id_code =
|
|
28
|
+
if SeregaResultCode::PLAIN_METHOD_NAME.match?(id_method)
|
|
29
|
+
"#{source_variable}.#{id_method}"
|
|
30
|
+
else
|
|
31
|
+
"#{source_variable}.public_send(#{id_method.inspect})"
|
|
32
|
+
end
|
|
33
|
+
"#{batch_variable}[#{id_code}]"
|
|
34
|
+
end
|
|
71
35
|
end
|
|
72
36
|
end
|
|
73
37
|
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
#
|
|
5
|
+
# Attribute value resolvers
|
|
6
|
+
#
|
|
7
|
+
module AttributeValueResolvers # :nodoc:
|
|
8
|
+
#
|
|
9
|
+
# Value of attributes with the :value option. Calls the callable with
|
|
10
|
+
# the arguments that its parameters take. It is no resolver: generated
|
|
11
|
+
# code calls the callable itself, with #code.
|
|
12
|
+
#
|
|
13
|
+
class Callable # :nodoc:
|
|
14
|
+
# The :value callable
|
|
15
|
+
attr_reader :callable
|
|
16
|
+
|
|
17
|
+
def initialize(callable)
|
|
18
|
+
@callable = callable
|
|
19
|
+
@signature = SeregaUtils::MethodSignature.call(callable, pos_limit: 2, keyword_args: [:ctx])
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Calls the callable with the object, the context and the batches that
|
|
23
|
+
# its parameters take
|
|
24
|
+
def call(object, ctx:, batches:)
|
|
25
|
+
case @signature
|
|
26
|
+
when "1" then @callable.call(object)
|
|
27
|
+
when "1_ctx" then @callable.call(object, ctx: ctx)
|
|
28
|
+
when "1_batches" then @callable.call(object, batches: batches)
|
|
29
|
+
when "1_batches_ctx" then @callable.call(object, ctx: ctx, batches: batches)
|
|
30
|
+
when "2" then @callable.call(object, ctx)
|
|
31
|
+
when "2_batches_ctx" then @callable.call(object, ctx, ctx: ctx, batches: batches)
|
|
32
|
+
else @callable.call # no parameters
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Ruby code that calls the callable, which `callable_variable` holds,
|
|
37
|
+
# the same way as #call. `batches_variable` holds the loaded batches.
|
|
38
|
+
def code(source_variable, callable_variable, batches_variable)
|
|
39
|
+
arguments =
|
|
40
|
+
case @signature
|
|
41
|
+
when "1" then source_variable
|
|
42
|
+
when "1_ctx" then "#{source_variable}, ctx: context"
|
|
43
|
+
when "1_batches" then "#{source_variable}, batches: #{batches_variable}"
|
|
44
|
+
when "1_batches_ctx" then "#{source_variable}, ctx: context, batches: #{batches_variable}"
|
|
45
|
+
when "2" then "#{source_variable}, context"
|
|
46
|
+
when "2_batches_ctx" then "#{source_variable}, context, ctx: context, batches: #{batches_variable}"
|
|
47
|
+
end
|
|
48
|
+
arguments ? "#{callable_variable}.call(#{arguments})" : "#{callable_variable}.call"
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -4,41 +4,29 @@ class Serega
|
|
|
4
4
|
#
|
|
5
5
|
# Attribute value resolvers
|
|
6
6
|
#
|
|
7
|
-
#
|
|
8
|
-
module AttributeValueResolvers
|
|
9
|
-
#
|
|
10
|
-
# Builds value resolver class for attributes with :const option
|
|
11
|
-
#
|
|
12
|
-
# @private
|
|
13
|
-
class ConstResolver
|
|
14
|
-
#
|
|
15
|
-
# Creates resolver that returns constant value
|
|
16
|
-
#
|
|
17
|
-
# @param const_value [Object] constant value to return
|
|
18
|
-
# @return [Const] resolver instance
|
|
19
|
-
#
|
|
20
|
-
def self.get(const_value)
|
|
21
|
-
Const.new(const_value)
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
7
|
+
module AttributeValueResolvers # :nodoc:
|
|
25
8
|
#
|
|
26
9
|
# Value resolver class for attributes with :const option
|
|
27
10
|
#
|
|
28
|
-
#
|
|
29
|
-
class Const
|
|
11
|
+
class Const < Base # :nodoc:
|
|
30
12
|
def initialize(const_value)
|
|
31
13
|
@const_value = const_value
|
|
32
14
|
end
|
|
33
15
|
|
|
34
|
-
#
|
|
35
16
|
# Returns the constant value
|
|
36
|
-
#
|
|
37
|
-
# @return [Object] the constant value
|
|
38
|
-
#
|
|
39
17
|
def call
|
|
40
18
|
@const_value
|
|
41
19
|
end
|
|
20
|
+
|
|
21
|
+
# A constant reads no source
|
|
22
|
+
def source_methods
|
|
23
|
+
FROZEN_EMPTY_ARRAY
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# The value is the `:const` option
|
|
27
|
+
def const?
|
|
28
|
+
true
|
|
29
|
+
end
|
|
42
30
|
end
|
|
43
31
|
end
|
|
44
32
|
end
|
|
@@ -4,21 +4,12 @@ class Serega
|
|
|
4
4
|
#
|
|
5
5
|
# Attribute value resolvers
|
|
6
6
|
#
|
|
7
|
-
#
|
|
8
|
-
module AttributeValueResolvers
|
|
7
|
+
module AttributeValueResolvers # :nodoc:
|
|
9
8
|
#
|
|
10
9
|
# Builds value resolver class for attributes with :delegate option
|
|
11
10
|
#
|
|
12
|
-
#
|
|
13
|
-
class DelegateResolver
|
|
14
|
-
#
|
|
11
|
+
class DelegateResolver # :nodoc:
|
|
15
12
|
# Creates resolver that delegates method call to another object
|
|
16
|
-
#
|
|
17
|
-
# @param delegate_to [Symbol] method to call on object to get delegated object
|
|
18
|
-
# @param method_name [Symbol] method to call on delegated object
|
|
19
|
-
# @param allow_nil [Boolean] whether to use safe navigation when delegated object is nil
|
|
20
|
-
# @return [Delegate, DelegateAllowNil] resolver instance
|
|
21
|
-
#
|
|
22
13
|
def self.get(delegate_to, method_name, allow_nil)
|
|
23
14
|
allow_nil ? DelegateAllowNil.new(delegate_to, method_name) : Delegate.new(delegate_to, method_name)
|
|
24
15
|
end
|
|
@@ -27,47 +18,55 @@ class Serega
|
|
|
27
18
|
#
|
|
28
19
|
# Value resolver class for attributes with :delegate (with :allow_nil) option
|
|
29
20
|
#
|
|
30
|
-
#
|
|
31
|
-
class DelegateAllowNil
|
|
21
|
+
class DelegateAllowNil < Base # :nodoc:
|
|
32
22
|
def initialize(delegate_to, method_name)
|
|
33
23
|
@delegate_to = delegate_to
|
|
34
24
|
@method_name = method_name
|
|
35
25
|
end
|
|
36
26
|
|
|
37
|
-
#
|
|
27
|
+
# Methods that the value read calls on the source: the delegate target
|
|
28
|
+
def source_methods
|
|
29
|
+
[@delegate_to]
|
|
30
|
+
end
|
|
31
|
+
|
|
38
32
|
# Delegates method call to another object with safe navigation
|
|
39
|
-
#
|
|
40
|
-
# @param object [Object] the object to delegate from
|
|
41
|
-
# @return [Object, nil] result of delegated method call or nil if delegated object is nil
|
|
42
|
-
#
|
|
43
33
|
def call(object)
|
|
44
|
-
object.public_send(delegate_to)&.public_send(method_name)
|
|
34
|
+
object.public_send(@delegate_to)&.public_send(@method_name)
|
|
45
35
|
end
|
|
46
36
|
|
|
47
|
-
|
|
37
|
+
# Ruby code that delegates the method call
|
|
38
|
+
def code(source_variable)
|
|
39
|
+
return unless SeregaResultCode::PLAIN_METHOD_NAME.match?(@delegate_to) && SeregaResultCode::PLAIN_METHOD_NAME.match?(@method_name)
|
|
48
40
|
|
|
49
|
-
|
|
41
|
+
"#{source_variable}.#{@delegate_to}&.#{@method_name}"
|
|
42
|
+
end
|
|
50
43
|
end
|
|
51
44
|
|
|
52
45
|
#
|
|
53
46
|
# Value resolver class for attributes with :delegate (without :allow_nil) option
|
|
54
47
|
#
|
|
55
|
-
#
|
|
56
|
-
class Delegate
|
|
48
|
+
class Delegate < Base # :nodoc:
|
|
57
49
|
def initialize(delegate_to, method_name)
|
|
58
50
|
@delegate_to = delegate_to
|
|
59
51
|
@method_name = method_name
|
|
60
52
|
end
|
|
61
53
|
|
|
62
|
-
#
|
|
54
|
+
# Methods that the value read calls on the source: the delegate target
|
|
55
|
+
def source_methods
|
|
56
|
+
[@delegate_to]
|
|
57
|
+
end
|
|
58
|
+
|
|
63
59
|
# Delegates method call to another object
|
|
64
|
-
#
|
|
65
|
-
# @param object [Object] the object to delegate from
|
|
66
|
-
# @return [Object] result of delegated method call
|
|
67
|
-
#
|
|
68
60
|
def call(object)
|
|
69
61
|
object.public_send(@delegate_to).public_send(@method_name)
|
|
70
62
|
end
|
|
63
|
+
|
|
64
|
+
# Ruby code that delegates the method call
|
|
65
|
+
def code(source_variable)
|
|
66
|
+
return unless SeregaResultCode::PLAIN_METHOD_NAME.match?(@delegate_to) && SeregaResultCode::PLAIN_METHOD_NAME.match?(@method_name)
|
|
67
|
+
|
|
68
|
+
"#{source_variable}.#{@delegate_to}.#{@method_name}"
|
|
69
|
+
end
|
|
71
70
|
end
|
|
72
71
|
end
|
|
73
72
|
end
|