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
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: serega
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.51.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andrey
|
|
@@ -9,9 +9,9 @@ bindir: bin
|
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies: []
|
|
12
|
-
description: Serializes Ruby objects
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
description: Serializes Ruby objects to Hashes, JSON, Data or Struct. Clients choose
|
|
13
|
+
the fields, and Serega preloads associations and runs batch loaders only for those
|
|
14
|
+
fields, with no N+1 queries. No runtime dependencies.
|
|
15
15
|
email:
|
|
16
16
|
- andreyruby@yandex.ru
|
|
17
17
|
executables: []
|
|
@@ -24,13 +24,17 @@ files:
|
|
|
24
24
|
- VERSION
|
|
25
25
|
- lib/serega.rb
|
|
26
26
|
- lib/serega/attribute.rb
|
|
27
|
+
- lib/serega/attribute/serializer_reference.rb
|
|
28
|
+
- lib/serega/attribute/value_code.rb
|
|
27
29
|
- lib/serega/attribute_normalizer.rb
|
|
28
30
|
- lib/serega/attribute_normalizers/batch.rb
|
|
29
31
|
- lib/serega/attribute_normalizers/block.rb
|
|
30
32
|
- lib/serega/attribute_normalizers/delegate.rb
|
|
31
33
|
- lib/serega/attribute_normalizers/hash_access.rb
|
|
32
34
|
- lib/serega/attribute_normalizers/preloads.rb
|
|
35
|
+
- lib/serega/attribute_value_resolvers/base.rb
|
|
33
36
|
- lib/serega/attribute_value_resolvers/batch.rb
|
|
37
|
+
- lib/serega/attribute_value_resolvers/callable.rb
|
|
34
38
|
- lib/serega/attribute_value_resolvers/const.rb
|
|
35
39
|
- lib/serega/attribute_value_resolvers/delegate.rb
|
|
36
40
|
- lib/serega/attribute_value_resolvers/hash_access.rb
|
|
@@ -42,11 +46,17 @@ files:
|
|
|
42
46
|
- lib/serega/engine/pulls.rb
|
|
43
47
|
- lib/serega/engine/run.rb
|
|
44
48
|
- lib/serega/errors.rb
|
|
45
|
-
- lib/serega/
|
|
49
|
+
- lib/serega/formatters.rb
|
|
50
|
+
- lib/serega/formatters/callable.rb
|
|
51
|
+
- lib/serega/formatters/method.rb
|
|
46
52
|
- lib/serega/plan.rb
|
|
47
53
|
- lib/serega/plan/cache.rb
|
|
54
|
+
- lib/serega/plan/depth_limit.rb
|
|
48
55
|
- lib/serega/plan/per_ractor_cache.rb
|
|
49
|
-
- lib/serega/
|
|
56
|
+
- lib/serega/plan/point.rb
|
|
57
|
+
- lib/serega/plan/presets.rb
|
|
58
|
+
- lib/serega/plan/selection.rb
|
|
59
|
+
- lib/serega/plan/selection_planner.rb
|
|
50
60
|
- lib/serega/plugins.rb
|
|
51
61
|
- lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb
|
|
52
62
|
- lib/serega/plugins/activerecord_preloads/lib/preloader.rb
|
|
@@ -58,15 +68,20 @@ files:
|
|
|
58
68
|
- lib/serega/presenter.rb
|
|
59
69
|
- lib/serega/result_builder.rb
|
|
60
70
|
- lib/serega/result_code.rb
|
|
71
|
+
- lib/serega/result_code/attribute_point_code.rb
|
|
72
|
+
- lib/serega/result_code/point_code.rb
|
|
73
|
+
- lib/serega/result_code/relation_point_code.rb
|
|
61
74
|
- lib/serega/shareable.rb
|
|
62
75
|
- lib/serega/source_group.rb
|
|
76
|
+
- lib/serega/utils/camel_case.rb
|
|
63
77
|
- lib/serega/utils/collection_detector.rb
|
|
64
78
|
- lib/serega/utils/deep_merge.rb
|
|
65
79
|
- lib/serega/utils/enum_deep_dup.rb
|
|
66
80
|
- lib/serega/utils/enum_deep_freeze.rb
|
|
67
81
|
- lib/serega/utils/method_signature.rb
|
|
68
|
-
- lib/serega/utils/
|
|
82
|
+
- lib/serega/utils/parse_string_selection.rb
|
|
69
83
|
- lib/serega/utils/ractor_local.rb
|
|
84
|
+
- lib/serega/utils/ruby_literal.rb
|
|
70
85
|
- lib/serega/utils/serialized_attribute_error.rb
|
|
71
86
|
- lib/serega/utils/to_hash.rb
|
|
72
87
|
- lib/serega/validations/attribute/check_block.rb
|
|
@@ -81,13 +96,18 @@ files:
|
|
|
81
96
|
- lib/serega/validations/attribute/check_opt_many.rb
|
|
82
97
|
- lib/serega/validations/attribute/check_opt_method.rb
|
|
83
98
|
- lib/serega/validations/attribute/check_opt_preload.rb
|
|
99
|
+
- lib/serega/validations/attribute/check_opt_preset.rb
|
|
100
|
+
- lib/serega/validations/attribute/check_opt_selection.rb
|
|
84
101
|
- lib/serega/validations/attribute/check_opt_serializer.rb
|
|
85
102
|
- lib/serega/validations/attribute/check_opt_value.rb
|
|
86
103
|
- lib/serega/validations/check_attribute_params.rb
|
|
87
104
|
- lib/serega/validations/check_batch_loader_params.rb
|
|
105
|
+
- lib/serega/validations/check_definition_selection.rb
|
|
88
106
|
- lib/serega/validations/check_formatter.rb
|
|
107
|
+
- lib/serega/validations/check_preset_params.rb
|
|
108
|
+
- lib/serega/validations/check_relation_selection.rb
|
|
109
|
+
- lib/serega/validations/check_selection.rb
|
|
89
110
|
- lib/serega/validations/check_serialize_keywords.rb
|
|
90
|
-
- lib/serega/validations/initiate/check_modifiers.rb
|
|
91
111
|
- lib/serega/validations/utils/check_allowed_keys.rb
|
|
92
112
|
- lib/serega/validations/utils/check_hash_access_value.rb
|
|
93
113
|
- lib/serega/validations/utils/check_opt_is_bool.rb
|
|
@@ -103,6 +123,7 @@ metadata:
|
|
|
103
123
|
source_code_uri: https://github.com/andreyruby/serega
|
|
104
124
|
documentation_uri: https://andreyruby.github.io/serega/
|
|
105
125
|
changelog_uri: https://github.com/andreyruby/serega/blob/master/CHANGELOG.md
|
|
126
|
+
bug_tracker_uri: https://github.com/andreyruby/serega/issues
|
|
106
127
|
rdoc_options:
|
|
107
128
|
- "--markup"
|
|
108
129
|
- markdown
|
|
@@ -123,7 +144,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
123
144
|
- !ruby/object:Gem::Version
|
|
124
145
|
version: '0'
|
|
125
146
|
requirements: []
|
|
126
|
-
rubygems_version: 4.0.
|
|
147
|
+
rubygems_version: 4.0.24
|
|
127
148
|
specification_version: 4
|
|
128
|
-
summary: Ruby
|
|
149
|
+
summary: 'Fast Ruby serializer: clients choose the fields, Serega loads only what
|
|
150
|
+
they use'
|
|
129
151
|
test_files: []
|
data/lib/serega/formatter.rb
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
class Serega
|
|
4
|
-
#
|
|
5
|
-
# Formats attribute values. A formatter 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
|
-
class SeregaFormatter # :nodoc:
|
|
15
|
-
# Callable that formats a value, nil for a method formatter
|
|
16
|
-
attr_reader :callable
|
|
17
|
-
|
|
18
|
-
# Arguments of the method, nil for a callable formatter
|
|
19
|
-
attr_reader :args
|
|
20
|
-
|
|
21
|
-
def initialize(formatter)
|
|
22
|
-
formatter = {use: formatter} unless formatter.is_a?(Hash)
|
|
23
|
-
use = formatter[:use]
|
|
24
|
-
@allow_nil = formatter.fetch(:allow_nil, false)
|
|
25
|
-
|
|
26
|
-
if use.respond_to?(:call)
|
|
27
|
-
@method_name = nil
|
|
28
|
-
@args = nil
|
|
29
|
-
@callable = use
|
|
30
|
-
@signature = SeregaUtils::MethodSignature.call(use, pos_limit: 2, keyword_args: [:ctx])
|
|
31
|
-
else
|
|
32
|
-
method_name, *args = use
|
|
33
|
-
@method_name = method_name
|
|
34
|
-
@args = args
|
|
35
|
-
@callable = nil
|
|
36
|
-
@signature = nil
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# Formats the value
|
|
41
|
-
def call(value, context)
|
|
42
|
-
return if value.nil? && @allow_nil
|
|
43
|
-
return value.public_send(@method_name, *@args) if @method_name
|
|
44
|
-
|
|
45
|
-
case @signature
|
|
46
|
-
when "1" then @callable.call(value)
|
|
47
|
-
when "1_ctx" then @callable.call(value, ctx: context)
|
|
48
|
-
else @callable.call(value, context) # "2"
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
# Ruby code that formats the value of `value_code`. A method is called
|
|
53
|
-
# directly, for example `(source.created_at).iso8601(3)`. Literal
|
|
54
|
-
# arguments are written in the code, other arguments are read from the
|
|
55
|
-
# Array that `args_code` reads. A callable is read with `callable_code`.
|
|
56
|
-
def code(value_code, callable_code, args_code)
|
|
57
|
-
return method_call_code(value_code, args_code) if @method_name
|
|
58
|
-
return unless @signature == "1"
|
|
59
|
-
return "#{callable_code}.call(#{value_code})" unless @allow_nil
|
|
60
|
-
|
|
61
|
-
"(value = #{value_code}).nil? ? nil : #{callable_code}.call(value)"
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
private
|
|
65
|
-
|
|
66
|
-
def method_call_code(value_code, args_code)
|
|
67
|
-
operator = @allow_nil ? "&." : "."
|
|
68
|
-
arguments = @args.each_with_index.map { |arg, index| argument_code(arg, index, args_code) }
|
|
69
|
-
|
|
70
|
-
unless SeregaResultCode::PLAIN_METHOD_NAME.match?(@method_name)
|
|
71
|
-
send_arguments = [@method_name.inspect, *arguments]
|
|
72
|
-
return "(#{value_code})#{operator}public_send(#{send_arguments.join(", ")})"
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
return "(#{value_code})#{operator}#{@method_name}" if arguments.empty?
|
|
76
|
-
|
|
77
|
-
"(#{value_code})#{operator}#{@method_name}(#{arguments.join(", ")})"
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
# Literal arguments are written in the code, other arguments are read
|
|
81
|
-
# from the Array of arguments
|
|
82
|
-
def argument_code(arg, index, args_code)
|
|
83
|
-
literal?(arg) ? literal_code(arg) : "#{args_code}[#{index}]"
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
def literal?(arg)
|
|
87
|
-
case arg
|
|
88
|
-
when Integer, Symbol, String, true, false, nil then true
|
|
89
|
-
when Float then arg.finite?
|
|
90
|
-
else false
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
def literal_code(arg)
|
|
95
|
-
arg.is_a?(String) ? "#{arg.inspect}.freeze" : arg.inspect
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
end
|
data/lib/serega/plan_point.rb
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
class Serega
|
|
4
|
-
#
|
|
5
|
-
# Combines attribute and nested attributes
|
|
6
|
-
#
|
|
7
|
-
class SeregaPlanPoint # :nodoc:
|
|
8
|
-
# Link to current plan this point belongs to
|
|
9
|
-
attr_reader :plan
|
|
10
|
-
|
|
11
|
-
# Shows current attribute
|
|
12
|
-
attr_reader :attribute
|
|
13
|
-
|
|
14
|
-
# Attribute `name`
|
|
15
|
-
attr_reader :name
|
|
16
|
-
|
|
17
|
-
# Shows child plan if exists
|
|
18
|
-
attr_reader :child_plan
|
|
19
|
-
|
|
20
|
-
# Whether the relation values of a class are collections, for a point
|
|
21
|
-
# with a child plan
|
|
22
|
-
attr_reader :collection_classes
|
|
23
|
-
|
|
24
|
-
# Initializes plan point
|
|
25
|
-
def initialize(plan, attribute, modifiers = nil)
|
|
26
|
-
@plan = plan
|
|
27
|
-
@attribute = attribute
|
|
28
|
-
@name = attribute.name
|
|
29
|
-
@child_plan = serializer ? SeregaPlan.new(serializer, self, modifiers || FROZEN_EMPTY_HASH) : nil
|
|
30
|
-
@collection_classes = @child_plan ? {}.compare_by_identity : nil
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
# Attribute `many` option
|
|
34
|
-
def many
|
|
35
|
-
attribute.many
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
# Attribute `serializer` option
|
|
39
|
-
def serializer
|
|
40
|
-
attribute.serializer
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
# Attribute `batch_loaders`
|
|
44
|
-
def batch_loaders
|
|
45
|
-
attribute.batch_loaders
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
# Attribute `preloads`
|
|
49
|
-
def preloads
|
|
50
|
-
attribute.preloads
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
# Attribute `conditional?`
|
|
54
|
-
def conditional?
|
|
55
|
-
attribute.conditional?
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
end
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
class Serega
|
|
4
|
-
module SeregaValidations # :nodoc:
|
|
5
|
-
#
|
|
6
|
-
# Validations that take place when initializing serializer
|
|
7
|
-
#
|
|
8
|
-
module Initiate # :nodoc:
|
|
9
|
-
#
|
|
10
|
-
# Modifiers validation
|
|
11
|
-
#
|
|
12
|
-
class CheckModifiers # :nodoc:
|
|
13
|
-
#
|
|
14
|
-
# Instantiates modifiers validator
|
|
15
|
-
#
|
|
16
|
-
def initialize
|
|
17
|
-
@parents_names = nil
|
|
18
|
-
@error_attributes = nil
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
# Validates provided fields names are existing attributes
|
|
22
|
-
def call(serializer_class, only, with, except)
|
|
23
|
-
validate(serializer_class, only)
|
|
24
|
-
validate(serializer_class, with)
|
|
25
|
-
validate(serializer_class, except)
|
|
26
|
-
|
|
27
|
-
raise_errors(serializer_class) if any_error?
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
private
|
|
31
|
-
|
|
32
|
-
def validate(serializer_class, fields)
|
|
33
|
-
fields.each do |name, nested_fields|
|
|
34
|
-
attribute = serializer_class && serializer_class.attributes[name]
|
|
35
|
-
|
|
36
|
-
# Save error when no attribute with checked name exists
|
|
37
|
-
unless attribute
|
|
38
|
-
save_error(name)
|
|
39
|
-
next
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
# Return when attribute has no nested fields
|
|
43
|
-
next if nested_fields.empty?
|
|
44
|
-
|
|
45
|
-
with_parent_name(name) do
|
|
46
|
-
validate(attribute.serializer, nested_fields)
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def parents_names
|
|
52
|
-
@parents_names ||= []
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
def with_parent_name(name)
|
|
56
|
-
parents_names << name
|
|
57
|
-
yield
|
|
58
|
-
parents_names.pop
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
def error_attributes
|
|
62
|
-
@error_attributes ||= []
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def save_error(name)
|
|
66
|
-
error_attributes << build_full_attribute_name(*parents_names, name)
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
def build_full_attribute_name(*names)
|
|
70
|
-
head, *nested = *names
|
|
71
|
-
result = +head.to_s # names are symbols, we need not frozen string
|
|
72
|
-
nested.each { |nested_name| result << "(" << nested_name.to_s }
|
|
73
|
-
nested.each { result << ")" }
|
|
74
|
-
result
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
def raise_errors(serializer_class)
|
|
78
|
-
raise Serega::AttributeNotExist.new("Not existing attributes: #{error_attributes.join(", ")}", serializer_class, error_attributes)
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
def any_error?
|
|
82
|
-
!@error_attributes.nil?
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
end
|
|
86
|
-
end
|
|
87
|
-
end
|