serega 0.50.0 → 0.51.1
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/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 +80 -102
- data/lib/serega/attribute_normalizer.rb +43 -17
- data/lib/serega/attribute_normalizers/preloads.rb +5 -4
- data/lib/serega/attribute_value_resolvers/base.rb +35 -0
- data/lib/serega/attribute_value_resolvers/batch.rb +14 -1
- data/lib/serega/attribute_value_resolvers/callable.rb +52 -0
- data/lib/serega/attribute_value_resolvers/const.rb +11 -1
- data/lib/serega/attribute_value_resolvers/delegate.rb +12 -2
- data/lib/serega/attribute_value_resolvers/hash_access.rb +19 -4
- data/lib/serega/attribute_value_resolvers/keyword.rb +10 -3
- data/lib/serega/attribute_values.rb +4 -18
- data/lib/serega/conditions.rb +26 -1
- data/lib/serega/config.rb +45 -6
- data/lib/serega/engine/pulls.rb +3 -16
- data/lib/serega/engine/run.rb +4 -9
- data/lib/serega/errors.rb +28 -3
- 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 +83 -60
- data/lib/serega/plan/depth_limit.rb +55 -0
- data/lib/serega/plan/per_ractor_cache.rb +11 -6
- data/lib/serega/plan/point.rb +67 -0
- data/lib/serega/plan/presets.rb +116 -0
- data/lib/serega/plan/selection.rb +104 -0
- data/lib/serega/plan/selection_planner.rb +50 -0
- data/lib/serega/plan.rb +45 -55
- data/lib/serega/plugins/metadata/meta_attribute.rb +17 -1
- data/lib/serega/plugins/metadata/metadata.rb +1 -10
- data/lib/serega/plugins/root/root.rb +11 -8
- data/lib/serega/presenter.rb +20 -0
- data/lib/serega/result_builder.rb +1 -1
- 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 +41 -85
- data/lib/serega/source_group.rb +37 -68
- data/lib/serega/utils/camel_case.rb +22 -0
- data/lib/serega/utils/collection_detector.rb +3 -2
- data/lib/serega/utils/{parse_string_modifiers.rb → parse_string_selection.rb} +2 -2
- data/lib/serega/utils/ruby_literal.rb +28 -0
- 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/check_attribute_params.rb +4 -4
- data/lib/serega/validations/check_definition_selection.rb +21 -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.rb +118 -50
- data/sig/serega.rbs +23 -7
- metadata +32 -10
- data/lib/serega/formatter.rb +0 -98
- data/lib/serega/plan_point.rb +0 -58
- data/lib/serega/validations/initiate/check_modifiers.rb +0 -87
|
@@ -24,12 +24,17 @@ class Serega
|
|
|
24
24
|
#
|
|
25
25
|
# Value resolver for attributes with the :hash_access option
|
|
26
26
|
#
|
|
27
|
-
class HashAccessKeyword # :nodoc:
|
|
27
|
+
class HashAccessKeyword < Base # :nodoc:
|
|
28
28
|
def initialize(name, mode, allow_missing_key)
|
|
29
29
|
@key = (mode == :symbol) ? name.to_sym : name.to_s
|
|
30
30
|
@allow_missing_key = allow_missing_key
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
+
# Methods that the value read calls on the source
|
|
34
|
+
def source_methods
|
|
35
|
+
@allow_missing_key ? %i[[]] : %i[fetch []]
|
|
36
|
+
end
|
|
37
|
+
|
|
33
38
|
# Reads the key from the record
|
|
34
39
|
def call(object)
|
|
35
40
|
return object[@key] if @allow_missing_key
|
|
@@ -42,7 +47,7 @@ class Serega
|
|
|
42
47
|
|
|
43
48
|
# Ruby code that reads the key, the same way as #call
|
|
44
49
|
def code(source_variable)
|
|
45
|
-
key =
|
|
50
|
+
key = SeregaUtils::RubyLiteral.code(@key)
|
|
46
51
|
return "#{source_variable}[#{key}]" if @allow_missing_key
|
|
47
52
|
|
|
48
53
|
"#{source_variable}.fetch(#{key}) { (found = #{source_variable}[#{key}]).nil? ? #{source_variable}.fetch(#{key}) : found }"
|
|
@@ -52,12 +57,17 @@ class Serega
|
|
|
52
57
|
#
|
|
53
58
|
# Value resolver for attributes with :hash_access and :delegate (without :allow_nil) options
|
|
54
59
|
#
|
|
55
|
-
class HashAccessDelegate # :nodoc:
|
|
60
|
+
class HashAccessDelegate < Base # :nodoc:
|
|
56
61
|
def initialize(to_step, final_step)
|
|
57
62
|
@to_step = to_step
|
|
58
63
|
@final_step = final_step
|
|
59
64
|
end
|
|
60
65
|
|
|
66
|
+
# Methods that the value read calls on the source: those of the first step
|
|
67
|
+
def source_methods
|
|
68
|
+
@to_step.source_methods
|
|
69
|
+
end
|
|
70
|
+
|
|
61
71
|
# Delegates the value reading through the intermediate object
|
|
62
72
|
def call(object)
|
|
63
73
|
@final_step.call(@to_step.call(object))
|
|
@@ -76,12 +86,17 @@ class Serega
|
|
|
76
86
|
#
|
|
77
87
|
# Value resolver for attributes with :hash_access and :delegate (with :allow_nil) options
|
|
78
88
|
#
|
|
79
|
-
class HashAccessDelegateAllowNil # :nodoc:
|
|
89
|
+
class HashAccessDelegateAllowNil < Base # :nodoc:
|
|
80
90
|
def initialize(to_step, final_step)
|
|
81
91
|
@to_step = to_step
|
|
82
92
|
@final_step = final_step
|
|
83
93
|
end
|
|
84
94
|
|
|
95
|
+
# Methods that the value read calls on the source: those of the first step
|
|
96
|
+
def source_methods
|
|
97
|
+
@to_step.source_methods
|
|
98
|
+
end
|
|
99
|
+
|
|
85
100
|
# Delegates the value reading through the intermediate object,
|
|
86
101
|
# resolving a nil intermediate to nil
|
|
87
102
|
def call(object)
|
|
@@ -8,13 +8,20 @@ class Serega
|
|
|
8
8
|
#
|
|
9
9
|
# Value resolver class for attributes with :keyword option
|
|
10
10
|
#
|
|
11
|
-
class Keyword # :nodoc:
|
|
11
|
+
class Keyword < Base # :nodoc:
|
|
12
12
|
def initialize(keyword)
|
|
13
13
|
@keyword = keyword
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
#
|
|
17
|
-
|
|
16
|
+
# Methods that the value read calls on the source: the keyword
|
|
17
|
+
def source_methods
|
|
18
|
+
[@keyword]
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Method of the object that gives the value: the keyword
|
|
22
|
+
def object_method
|
|
23
|
+
@keyword
|
|
24
|
+
end
|
|
18
25
|
|
|
19
26
|
# Calls the keyword method on the object
|
|
20
27
|
def call(object)
|
|
@@ -2,20 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
class Serega
|
|
4
4
|
#
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
# `CONSTANTS` and `DEFAULTS` hold the `:const` and `:default` values of the
|
|
11
|
-
# attributes, `FORMATTERS` the formatter callables and `FORMATTER_ARGS` the
|
|
12
|
-
# arguments of method formatters, by attribute name.
|
|
13
|
-
#
|
|
14
|
-
# Each method name is `read_` and the attribute name with `_` in place of
|
|
15
|
-
# other characters than letters, digits and `_`, for example
|
|
16
|
-
# `read_first_name` for `first-name`. A name of another attribute gets a
|
|
17
|
-
# number: `read_first_name_2`. Thus attribute names do not clash with
|
|
18
|
-
# methods of Object, and generated code calls the methods directly.
|
|
5
|
+
# Values of the attributes that generated code reads, by attribute name:
|
|
6
|
+
# `CONSTANTS` and `DEFAULTS` hold the `:const` and `:default` values,
|
|
7
|
+
# `FORMATTERS` the formatter callables and `FORMATTER_ARGS` the arguments
|
|
8
|
+
# of method formatters.
|
|
19
9
|
#
|
|
20
10
|
class SeregaAttributeValues # :nodoc:
|
|
21
11
|
# `:const` values by attribute name
|
|
@@ -30,9 +20,6 @@ class Serega
|
|
|
30
20
|
# Arguments of method formatters by attribute name
|
|
31
21
|
FORMATTER_ARGS = {}
|
|
32
22
|
|
|
33
|
-
# Method names by attribute name
|
|
34
|
-
METHOD_NAMES = {}
|
|
35
|
-
|
|
36
23
|
# Gives each subclass its own values
|
|
37
24
|
def self.inherited(subclass)
|
|
38
25
|
super
|
|
@@ -40,7 +27,6 @@ class Serega
|
|
|
40
27
|
subclass.const_set(:DEFAULTS, {})
|
|
41
28
|
subclass.const_set(:FORMATTERS, {})
|
|
42
29
|
subclass.const_set(:FORMATTER_ARGS, {})
|
|
43
|
-
subclass.const_set(:METHOD_NAMES, {})
|
|
44
30
|
end
|
|
45
31
|
end
|
|
46
32
|
end
|
data/lib/serega/conditions.rb
CHANGED
|
@@ -11,14 +11,39 @@ class Serega
|
|
|
11
11
|
# def satisfy?(object, context) = (begin; object.active; rescue => error; ...; end)
|
|
12
12
|
#
|
|
13
13
|
class SeregaConditions # :nodoc:
|
|
14
|
+
# Makes the conditions of the attribute. A Symbol condition calls the
|
|
15
|
+
# method of this name.
|
|
14
16
|
def initialize(attribute, conditions)
|
|
15
17
|
@attribute = attribute
|
|
16
|
-
@conditions = conditions
|
|
18
|
+
@conditions = conditions.transform_values do |condition|
|
|
19
|
+
condition.is_a?(Symbol) ? AttributeValueResolvers::Keyword.new(condition) : condition
|
|
20
|
+
end
|
|
21
|
+
@object_conditions = @conditions.key?(:if) || @conditions.key?(:unless)
|
|
22
|
+
@value_conditions = @conditions.key?(:if_value) || @conditions.key?(:unless_value)
|
|
17
23
|
satisfy_code = "def satisfy?(object, context) = #{check_code(:if, :unless)}"
|
|
18
24
|
satisfy_value_code = "def satisfy_value?(object, context) = #{check_code(:if_value, :unless_value)}"
|
|
19
25
|
attribute.define_methods(singleton_class, "#{satisfy_code}; #{satisfy_value_code}")
|
|
20
26
|
end
|
|
21
27
|
|
|
28
|
+
# Whether there is an :if or :unless condition
|
|
29
|
+
def object_conditions?
|
|
30
|
+
@object_conditions
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Whether there is an :if_value or :unless_value condition
|
|
34
|
+
def value_conditions?
|
|
35
|
+
@value_conditions
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Methods that the :if and :unless conditions call on the object, nil
|
|
39
|
+
# when one of them is a callable, which gets the object
|
|
40
|
+
def source_methods
|
|
41
|
+
conditions = @conditions.values_at(:if, :unless).compact
|
|
42
|
+
return unless conditions.all?(AttributeValueResolvers::Keyword)
|
|
43
|
+
|
|
44
|
+
conditions.flat_map(&:source_methods)
|
|
45
|
+
end
|
|
46
|
+
|
|
22
47
|
private
|
|
23
48
|
|
|
24
49
|
# Code that is true when the object passes the :if and does not match the
|
data/lib/serega/config.rb
CHANGED
|
@@ -23,13 +23,15 @@ class Serega
|
|
|
23
23
|
serialize_keys: %i[context many].freeze,
|
|
24
24
|
check_attribute_name: true,
|
|
25
25
|
require_many: false,
|
|
26
|
-
|
|
26
|
+
check_selection: true,
|
|
27
27
|
delegate_default_allow_nil: false,
|
|
28
28
|
max_cached_plans_per_serializer_count: 20,
|
|
29
29
|
depth_limit: nil,
|
|
30
30
|
auto_preload: {has_delegate_option: false, has_serializer_option: false},
|
|
31
31
|
auto_preload_excluded_methods: %i[itself __getobj__].freeze,
|
|
32
32
|
hide_by_default: false,
|
|
33
|
+
compact: false,
|
|
34
|
+
key_transform: nil,
|
|
33
35
|
batch_id_option: :id,
|
|
34
36
|
base_serializer: nil,
|
|
35
37
|
hash_access: {default_mode: :symbol, default_allow_missing_key: false}
|
|
@@ -61,13 +63,13 @@ class Serega
|
|
|
61
63
|
|
|
62
64
|
# Whether unknown fields of `only:`, `except:` and `with:` raise
|
|
63
65
|
# `Serega::AttributeNotExist`. `true` by default.
|
|
64
|
-
def
|
|
65
|
-
opts.fetch(:
|
|
66
|
+
def check_selection
|
|
67
|
+
opts.fetch(:check_selection)
|
|
66
68
|
end
|
|
67
69
|
|
|
68
|
-
# Sets #
|
|
69
|
-
def
|
|
70
|
-
opts[:
|
|
70
|
+
# Sets #check_selection
|
|
71
|
+
def check_selection=(value)
|
|
72
|
+
opts[:check_selection] = checked_boolean(value)
|
|
71
73
|
end
|
|
72
74
|
|
|
73
75
|
# Default `allow_nil:` of the `:delegate` attribute option. `false` by
|
|
@@ -131,6 +133,31 @@ class Serega
|
|
|
131
133
|
end
|
|
132
134
|
end
|
|
133
135
|
|
|
136
|
+
# Whether serialized Hashes leave out keys with `nil` values, as
|
|
137
|
+
# `Hash#compact` does. `false` by default.
|
|
138
|
+
def compact
|
|
139
|
+
opts.fetch(:compact)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Sets #compact
|
|
143
|
+
def compact=(value)
|
|
144
|
+
opts[:compact] = checked_boolean(value)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# How attribute names become serialized keys: `nil` (names as they are,
|
|
148
|
+
# the default), `:camel_case` (`first_name` gives `firstName`), or a
|
|
149
|
+
# callable that receives the name Symbol and returns the key. A String
|
|
150
|
+
# key stays a String in the serialized Hash. The attribute name is the
|
|
151
|
+
# key as a Symbol, thus `only:`, `except:` and `with:` take it.
|
|
152
|
+
def key_transform
|
|
153
|
+
opts.fetch(:key_transform)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# Sets #key_transform
|
|
157
|
+
def key_transform=(value)
|
|
158
|
+
opts[:key_transform] = checked_key_transform(value)
|
|
159
|
+
end
|
|
160
|
+
|
|
134
161
|
# Attributes that get preloads automatically: `:delegate` attributes with
|
|
135
162
|
# `has_delegate_option` and `:serializer` attributes with
|
|
136
163
|
# `has_serializer_option`. Both are `false` by default.
|
|
@@ -226,6 +253,18 @@ class Serega
|
|
|
226
253
|
|
|
227
254
|
private
|
|
228
255
|
|
|
256
|
+
def checked_key_transform(value)
|
|
257
|
+
return value if value.nil? || value == :camel_case
|
|
258
|
+
|
|
259
|
+
params =
|
|
260
|
+
if value.respond_to?(:parameters) then value.parameters
|
|
261
|
+
elsif value.respond_to?(:call) then value.method(:call).parameters
|
|
262
|
+
end
|
|
263
|
+
return value if params&.size == 1 && %i[req opt].include?(params[0][0])
|
|
264
|
+
|
|
265
|
+
raise SeregaError, "Must be nil, :camel_case, or a callable with one parameter, #{value.inspect} provided"
|
|
266
|
+
end
|
|
267
|
+
|
|
229
268
|
def checked_boolean(value)
|
|
230
269
|
raise SeregaError, "Must have boolean value, #{value.inspect} provided" unless value == true || value == false
|
|
231
270
|
|
data/lib/serega/engine/pulls.rb
CHANGED
|
@@ -18,26 +18,13 @@ class Serega
|
|
|
18
18
|
|
|
19
19
|
module_function
|
|
20
20
|
|
|
21
|
-
# Appends the sources of the relation sources to sources, and returns
|
|
22
|
-
# the pull of each relation source
|
|
23
|
-
def collect(sources, relation_sources, many, known_classes)
|
|
24
|
-
relation_sources.map { |relation_source| append(sources, relation_source, many, known_classes) }
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
# Collects like `.collect`, and keeps SKIP of a skipped relation source
|
|
28
|
-
# as its pull
|
|
29
|
-
def collect_conditional(sources, relation_sources, many, known_classes)
|
|
30
|
-
relation_sources.map do |relation_source|
|
|
31
|
-
SKIP.equal?(relation_source) ? relation_source : append(sources, relation_source, many, known_classes)
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
|
|
35
21
|
# Appends the source(s) of one relation source to sources, and returns
|
|
36
|
-
# its pull
|
|
22
|
+
# its pull. The generated `#collect` of a result builder writes the
|
|
23
|
+
# same steps inline.
|
|
37
24
|
def append(sources, relation_source, many, known_classes)
|
|
38
25
|
return if relation_source.nil?
|
|
39
26
|
|
|
40
|
-
if many != false && SeregaUtils::CollectionDetector.call(relation_source, known_classes)
|
|
27
|
+
if many != false && (relation_source.instance_of?(Array) || SeregaUtils::CollectionDetector.call(relation_source, known_classes))
|
|
41
28
|
collection = relation_source.to_a
|
|
42
29
|
sources.concat(collection)
|
|
43
30
|
collection.size
|
data/lib/serega/engine/run.rb
CHANGED
|
@@ -25,7 +25,9 @@ class Serega
|
|
|
25
25
|
class << self
|
|
26
26
|
# Serializes the object(s) with the plan in a new run. A flat plan
|
|
27
27
|
# builds its serialized object(s) with its result builder, without a
|
|
28
|
-
# run, and one source without the sources Array.
|
|
28
|
+
# run, and one source without the sources Array. Skipping the run and
|
|
29
|
+
# its source group takes 136 ns and 3 objects instead of 343 ns and 8
|
|
30
|
+
# objects for one Hash source (Ruby 4.0, YJIT).
|
|
29
31
|
def call(plan, object, many, mode, context)
|
|
30
32
|
return new(mode, context).call(plan, object, many: many) unless plan.flat?
|
|
31
33
|
return if object.nil?
|
|
@@ -57,7 +59,7 @@ class Serega
|
|
|
57
59
|
root_group = root_source_group(plan, object, many)
|
|
58
60
|
source_groups = discover(root_group)
|
|
59
61
|
build(source_groups)
|
|
60
|
-
root_value
|
|
62
|
+
root_group.root_value
|
|
61
63
|
end
|
|
62
64
|
|
|
63
65
|
# Makes a source group of the plan.
|
|
@@ -94,13 +96,6 @@ class Serega
|
|
|
94
96
|
def build(source_groups)
|
|
95
97
|
source_groups.reverse_each(&:build)
|
|
96
98
|
end
|
|
97
|
-
|
|
98
|
-
# Returns the serialized root: one serialized object for SINGLE_SOURCE,
|
|
99
|
-
# or all serialized objects of the root group
|
|
100
|
-
def root_value(root_group)
|
|
101
|
-
serialized = root_group.serialized
|
|
102
|
-
Pulls::SINGLE_SOURCE.equal?(root_group.pulls[0]) ? serialized[0] : serialized
|
|
103
|
-
end
|
|
104
99
|
end
|
|
105
100
|
end
|
|
106
101
|
end
|
data/lib/serega/errors.rb
CHANGED
|
@@ -29,8 +29,31 @@ class Serega
|
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
+
# Raised when `preset:` names an undefined preset
|
|
33
|
+
#
|
|
34
|
+
# ```ruby
|
|
35
|
+
# UserSerializer.to_h(user, preset: :admin)
|
|
36
|
+
# # => Serega::PresetNotExist: Preset :admin is not defined in UserSerializer
|
|
37
|
+
# ```
|
|
38
|
+
#
|
|
39
|
+
# See [Presets](https://github.com/andreyruby/serega/blob/master/docs/selecting-fields.md#presets).
|
|
40
|
+
class PresetNotExist < SeregaError
|
|
41
|
+
# Serializer that has no such preset
|
|
42
|
+
attr_reader :serializer
|
|
43
|
+
|
|
44
|
+
# Name of the undefined preset, for example `:admin`
|
|
45
|
+
attr_reader :preset
|
|
46
|
+
|
|
47
|
+
def initialize(message = nil, serializer = nil, preset = nil) # :nodoc:
|
|
48
|
+
super(message)
|
|
49
|
+
@serializer = serializer
|
|
50
|
+
@preset = preset
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
32
54
|
# Raised when fields nest deeper than `config.depth_limit` of the
|
|
33
|
-
# serializer that starts the serialization
|
|
55
|
+
# serializer that starts the serialization, or, without a limit, when
|
|
56
|
+
# relations nest in a cycle
|
|
34
57
|
#
|
|
35
58
|
# ```ruby
|
|
36
59
|
# UserSerializer.to_h(user, with: {friends: :friends})
|
|
@@ -39,8 +62,10 @@ class Serega
|
|
|
39
62
|
#
|
|
40
63
|
# See [Depth limit](https://github.com/andreyruby/serega/blob/master/docs/configuration.md#depth-limit).
|
|
41
64
|
class DepthLimitError < SeregaError
|
|
42
|
-
# Path that exceeds the limit,
|
|
43
|
-
#
|
|
65
|
+
# Path that exceeds the limit or closes the cycle, and on the next line a hint:
|
|
66
|
+
#
|
|
67
|
+
# UserSerializer (depth limit: 2) -> friends -> friends
|
|
68
|
+
# (hint: if the nesting is not a cycle, increase config.depth_limit of UserSerializer)
|
|
44
69
|
attr_reader :details
|
|
45
70
|
|
|
46
71
|
def initialize(message = nil, details = nil) # :nodoc:
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module SeregaFormatters # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# Formatter that calls a callable with the value, and with the context
|
|
7
|
+
# when the callable takes it
|
|
8
|
+
#
|
|
9
|
+
class Callable # :nodoc:
|
|
10
|
+
# Callable that formats a value
|
|
11
|
+
attr_reader :callable
|
|
12
|
+
|
|
13
|
+
def initialize(callable, allow_nil)
|
|
14
|
+
@callable = callable
|
|
15
|
+
@signature = SeregaUtils::MethodSignature.call(callable, pos_limit: 2, keyword_args: [:ctx])
|
|
16
|
+
@allow_nil = allow_nil
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# A callable formatter has no method arguments
|
|
20
|
+
def args
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Formats the value
|
|
24
|
+
def call(value, context)
|
|
25
|
+
return if value.nil? && @allow_nil
|
|
26
|
+
|
|
27
|
+
case @signature
|
|
28
|
+
when "1" then @callable.call(value)
|
|
29
|
+
when "1_ctx" then @callable.call(value, ctx: context)
|
|
30
|
+
else @callable.call(value, context) # "2"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Ruby code that calls the callable that `callable_code` reads with the
|
|
35
|
+
# value of `value_code`. A callable that takes the context has no code.
|
|
36
|
+
def code(value_code, callable_code, _args_code)
|
|
37
|
+
return unless @signature == "1"
|
|
38
|
+
return "#{callable_code}.call(#{value_code})" unless @allow_nil
|
|
39
|
+
|
|
40
|
+
"(value = #{value_code}).nil? ? nil : #{callable_code}.call(value)"
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module SeregaFormatters # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# Formatter that calls a method of the value, for example `[:round, 2]`
|
|
7
|
+
#
|
|
8
|
+
class Method # :nodoc:
|
|
9
|
+
# Arguments of the method
|
|
10
|
+
attr_reader :args
|
|
11
|
+
|
|
12
|
+
def initialize(use, allow_nil)
|
|
13
|
+
method_name, *args = use
|
|
14
|
+
@method_name = method_name
|
|
15
|
+
@args = args
|
|
16
|
+
@allow_nil = allow_nil
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# A method formatter has no callable
|
|
20
|
+
def callable
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Formats the value
|
|
24
|
+
def call(value, _context)
|
|
25
|
+
return if value.nil? && @allow_nil
|
|
26
|
+
|
|
27
|
+
value.public_send(@method_name, *@args)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Ruby code that calls the method on the value of `value_code`, for
|
|
31
|
+
# example `(source.created_at).iso8601(3)`. Literal arguments are
|
|
32
|
+
# written in the code, other arguments are read from the Array that
|
|
33
|
+
# `args_code` reads.
|
|
34
|
+
def code(value_code, _callable_code, args_code)
|
|
35
|
+
operator = @allow_nil ? "&." : "."
|
|
36
|
+
arguments = @args.each_with_index.map { |arg, index| argument_code(arg, index, args_code) }
|
|
37
|
+
|
|
38
|
+
unless SeregaResultCode::PLAIN_METHOD_NAME.match?(@method_name)
|
|
39
|
+
send_arguments = [@method_name.inspect, *arguments]
|
|
40
|
+
return "(#{value_code})#{operator}public_send(#{send_arguments.join(", ")})"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
return "(#{value_code})#{operator}#{@method_name}" if arguments.empty?
|
|
44
|
+
|
|
45
|
+
"(#{value_code})#{operator}#{@method_name}(#{arguments.join(", ")})"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def argument_code(arg, index, args_code)
|
|
51
|
+
SeregaUtils::RubyLiteral.literal?(arg) ? SeregaUtils::RubyLiteral.code(arg) : "#{args_code}[#{index}]"
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
#
|
|
5
|
+
# Formatters of attribute values. A formatter declaration is one of:
|
|
6
|
+
# - a callable that gets the value, and the context when it has a second
|
|
7
|
+
# parameter or the :ctx keyword;
|
|
8
|
+
# - a method name, called on the value;
|
|
9
|
+
# - an Array of a method name and its arguments, for example `[:round, 2]`;
|
|
10
|
+
# - a Hash with one of them as `:use` and the `:allow_nil` option.
|
|
11
|
+
#
|
|
12
|
+
# With `allow_nil: true` a nil value stays nil and is not formatted.
|
|
13
|
+
#
|
|
14
|
+
module SeregaFormatters # :nodoc:
|
|
15
|
+
class << self
|
|
16
|
+
# Returns the formatter of the declaration: a Callable for a callable,
|
|
17
|
+
# a Method for a method name with its arguments
|
|
18
|
+
def build(declaration)
|
|
19
|
+
declaration = {use: declaration} unless declaration.is_a?(Hash)
|
|
20
|
+
use = declaration[:use]
|
|
21
|
+
allow_nil = declaration.fetch(:allow_nil, false)
|
|
22
|
+
|
|
23
|
+
use.respond_to?(:call) ? Callable.new(use, allow_nil) : Method.new(use, allow_nil)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|