serega 0.51.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/errors.rb +3 -2
- data/lib/serega/plan/depth_limit.rb +27 -8
- data/lib/serega/plan/selection.rb +10 -0
- data/lib/serega/plan.rb +6 -2
- data/lib/serega/source_group.rb +1 -1
- data/lib/serega.rb +24 -20
- metadata +8 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b39190c1ff39ec02e08dfadb1139c1e48f79d7494f2570053c9af44ea2d76b17
|
|
4
|
+
data.tar.gz: 47977301d95be980ef29d743aac4dcd4034c5a105744c432c9ec2317f9e2ce5f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d4e31b3437303fbb7ae587264ed422d366df92c23d0ef3532b31c3b545c5c25572ab57a7965061f42d9f1825d674a5164c12369ee02b3c6deb642403ee230216
|
|
7
|
+
data.tar.gz: f4ec5db061690a24daba5f810d5cc4f42c660a5c8a5e25d55cdf70474253ab58af03893937646133624cddba7c39db7b4bcef9d819ad5586e2016e0a94f76a6e
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.51.
|
|
1
|
+
0.51.1
|
data/lib/serega/errors.rb
CHANGED
|
@@ -52,7 +52,8 @@ class Serega
|
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
# Raised when fields nest deeper than `config.depth_limit` of the
|
|
55
|
-
# serializer that starts the serialization
|
|
55
|
+
# serializer that starts the serialization, or, without a limit, when
|
|
56
|
+
# relations nest in a cycle
|
|
56
57
|
#
|
|
57
58
|
# ```ruby
|
|
58
59
|
# UserSerializer.to_h(user, with: {friends: :friends})
|
|
@@ -61,7 +62,7 @@ class Serega
|
|
|
61
62
|
#
|
|
62
63
|
# See [Depth limit](https://github.com/andreyruby/serega/blob/master/docs/configuration.md#depth-limit).
|
|
63
64
|
class DepthLimitError < SeregaError
|
|
64
|
-
# Path that exceeds the limit, and on the next line a hint
|
|
65
|
+
# Path that exceeds the limit or closes the cycle, and on the next line a hint:
|
|
65
66
|
#
|
|
66
67
|
# UserSerializer (depth limit: 2) -> friends -> friends
|
|
67
68
|
# (hint: if the nesting is not a cycle, increase config.depth_limit of UserSerializer)
|
|
@@ -4,19 +4,21 @@ class Serega
|
|
|
4
4
|
class SeregaPlan # :nodoc:
|
|
5
5
|
#
|
|
6
6
|
# Depth limit of plans: the levels of child plans that `config.depth_limit`
|
|
7
|
-
# of the root serializer allows below a plan.
|
|
7
|
+
# of the root serializer allows below a plan. Without a limit, it stops a
|
|
8
|
+
# cycle of relations.
|
|
8
9
|
#
|
|
9
10
|
module DepthLimit # :nodoc:
|
|
10
11
|
class << self
|
|
11
|
-
# Depth left below a
|
|
12
|
-
# root plan, one level less than the parent plan's
|
|
13
|
-
# `nil` without a limit. Raises DepthLimitError when
|
|
14
|
-
# has no level left
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
# Depth left below the plan of a selection: the depth limit of the
|
|
13
|
+
# serializer for a root plan, one level less than the parent plan's
|
|
14
|
+
# for a child plan, `nil` without a limit. Raises DepthLimitError when
|
|
15
|
+
# the parent plan has no level left, or, without a limit, when a
|
|
16
|
+
# parent plan has the same selection and would repeat without end.
|
|
17
|
+
def depth_left(selection, parent_plan_point)
|
|
18
|
+
return selection.serializer_class.config.depth_limit unless parent_plan_point
|
|
17
19
|
|
|
18
20
|
parent_depth_left = parent_plan_point.plan.depth_left
|
|
19
|
-
return unless parent_depth_left
|
|
21
|
+
return check_cycle(selection, parent_plan_point) unless parent_depth_left
|
|
20
22
|
return parent_depth_left - 1 if parent_depth_left > 1
|
|
21
23
|
|
|
22
24
|
raise_depth_limit_error(parent_plan_point)
|
|
@@ -24,12 +26,29 @@ class Serega
|
|
|
24
26
|
|
|
25
27
|
private
|
|
26
28
|
|
|
29
|
+
def check_cycle(selection, parent_plan_point)
|
|
30
|
+
point = parent_plan_point
|
|
31
|
+
|
|
32
|
+
while point
|
|
33
|
+
plan = point.plan
|
|
34
|
+
raise_cycle_error(parent_plan_point) if plan.selection == selection
|
|
35
|
+
|
|
36
|
+
point = plan.parent_plan_point
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
27
40
|
def raise_depth_limit_error(parent_plan_point)
|
|
28
41
|
root_serializer = parent_plan_point.plan.root_plan.serializer_class
|
|
29
42
|
limit = root_serializer.config.depth_limit
|
|
30
43
|
path = parent_plan_point.path.join(" -> ")
|
|
31
44
|
raise DepthLimitError.new("Depth limit was exceeded", "#{root_serializer} (depth limit: #{limit}) -> #{path}\n(hint: if the nesting is not a cycle, increase config.depth_limit of #{root_serializer})")
|
|
32
45
|
end
|
|
46
|
+
|
|
47
|
+
def raise_cycle_error(parent_plan_point)
|
|
48
|
+
root_serializer = parent_plan_point.plan.root_plan.serializer_class
|
|
49
|
+
path = parent_plan_point.path.join(" -> ")
|
|
50
|
+
raise DepthLimitError.new("Relations nest in a cycle", "#{root_serializer} -> #{path}\n(hint: hide a relation of the cycle or set config.depth_limit)")
|
|
51
|
+
end
|
|
33
52
|
end
|
|
34
53
|
end
|
|
35
54
|
end
|
|
@@ -13,6 +13,9 @@ class Serega
|
|
|
13
13
|
# Serializer of the selected attributes
|
|
14
14
|
attr_reader :serializer_class
|
|
15
15
|
|
|
16
|
+
# Parsed `only`, `with` and `except` options
|
|
17
|
+
attr_reader :only, :with, :except
|
|
18
|
+
|
|
16
19
|
# Selects the attributes of the serializer with the parsed selection,
|
|
17
20
|
# Hashes of attribute names
|
|
18
21
|
def initialize(serializer_class, selection)
|
|
@@ -46,6 +49,13 @@ class Serega
|
|
|
46
49
|
Selection.new(attribute.serializer, child_selection(attribute))
|
|
47
50
|
end
|
|
48
51
|
|
|
52
|
+
# Whether the other selection selects the same attributes and child
|
|
53
|
+
# selections of the same serializer
|
|
54
|
+
def ==(other)
|
|
55
|
+
other.class == self.class && other.serializer_class == serializer_class &&
|
|
56
|
+
other.only == @only && other.with == @with && other.except == @except
|
|
57
|
+
end
|
|
58
|
+
|
|
49
59
|
# Returns the result builder class of the plan: SeregaResultBuilder
|
|
50
60
|
def builder_class
|
|
51
61
|
SeregaResultBuilder
|
data/lib/serega/plan.rb
CHANGED
|
@@ -9,6 +9,9 @@ class Serega
|
|
|
9
9
|
# Serializer of the plan
|
|
10
10
|
attr_reader :serializer_class
|
|
11
11
|
|
|
12
|
+
# Selection of the attributes of the plan
|
|
13
|
+
attr_reader :selection
|
|
14
|
+
|
|
12
15
|
# Parent plan point
|
|
13
16
|
attr_reader :parent_plan_point
|
|
14
17
|
|
|
@@ -33,13 +36,14 @@ class Serega
|
|
|
33
36
|
# selection's serializer, with the result builder class of the
|
|
34
37
|
# selection. A child plan gets the point of its parent plan. Raises
|
|
35
38
|
# DepthLimitError when the plan nests deeper than the depth limit of the
|
|
36
|
-
# root serializer.
|
|
39
|
+
# root serializer, or repeats a parent plan without a depth limit.
|
|
37
40
|
def initialize(selection, parent_plan_point = nil)
|
|
38
41
|
serializer_class = selection.serializer_class
|
|
39
42
|
serializer_class.lock
|
|
40
43
|
@serializer_class = serializer_class
|
|
44
|
+
@selection = selection
|
|
41
45
|
@parent_plan_point = parent_plan_point
|
|
42
|
-
@depth_left = DepthLimit.depth_left(
|
|
46
|
+
@depth_left = DepthLimit.depth_left(selection, parent_plan_point)
|
|
43
47
|
@builder_class = selection.builder_class
|
|
44
48
|
@points = selected_points(selection)
|
|
45
49
|
@presenter = points_presenter(serializer_class)
|
data/lib/serega/source_group.rb
CHANGED
|
@@ -103,7 +103,7 @@ class Serega
|
|
|
103
103
|
return if points.empty?
|
|
104
104
|
|
|
105
105
|
serializer_class = plan.serializer_class
|
|
106
|
-
handler = serializer_class.
|
|
106
|
+
handler = serializer_class.preload_with_handler
|
|
107
107
|
sources = plan.presenter ? @sources.map(&:__getobj__) : @sources
|
|
108
108
|
|
|
109
109
|
points.each do |point|
|
data/lib/serega.rb
CHANGED
|
@@ -384,17 +384,20 @@ class Serega
|
|
|
384
384
|
#
|
|
385
385
|
# See [Preloads](https://github.com/andreyruby/serega/blob/master/docs/preloads.md).
|
|
386
386
|
#
|
|
387
|
-
def preload_with(value =
|
|
388
|
-
return @
|
|
387
|
+
def preload_with(value = (omitted = true), &block)
|
|
388
|
+
return @preload_with_handler if omitted && block.nil?
|
|
389
389
|
|
|
390
390
|
check_unlocked
|
|
391
|
-
handler =
|
|
391
|
+
handler = omitted ? block : checked_value_handler(:preload_with, value, block)
|
|
392
392
|
signature = SeregaUtils::MethodSignature.call(handler, pos_limit: 2)
|
|
393
393
|
raise SeregaError, "preload_with handler must accept two positional arguments: (objects, preloads)" unless signature == "2"
|
|
394
394
|
|
|
395
|
-
@
|
|
395
|
+
@preload_with_handler = handler
|
|
396
396
|
end
|
|
397
397
|
|
|
398
|
+
# The handler registered with `preload_with`
|
|
399
|
+
attr_reader :preload_with_handler # :nodoc:
|
|
400
|
+
|
|
398
401
|
#
|
|
399
402
|
# Sets the handler that replaces the objects given to the serializer, for
|
|
400
403
|
# example ids with records. It runs once per serialization, before Serega
|
|
@@ -410,18 +413,21 @@ class Serega
|
|
|
410
413
|
#
|
|
411
414
|
# See [Prepare initial objects](https://github.com/andreyruby/serega/blob/master/docs/prepare-initial-objects.md).
|
|
412
415
|
#
|
|
413
|
-
def prepare_initial_objects(value =
|
|
414
|
-
return @
|
|
416
|
+
def prepare_initial_objects(value = (omitted = true), &block)
|
|
417
|
+
return @prepare_initial_objects_handler if omitted && block.nil?
|
|
415
418
|
|
|
416
419
|
check_unlocked
|
|
417
|
-
handler =
|
|
420
|
+
handler = omitted ? block : checked_value_handler(:prepare_initial_objects, value, block)
|
|
418
421
|
signature = SeregaUtils::MethodSignature.call(handler, pos_limit: 2, keyword_args: [:ctx])
|
|
419
422
|
raise SeregaError, prepare_initial_objects_signature_error unless %w[1 2 1_ctx].include?(signature)
|
|
420
423
|
|
|
421
424
|
@prepare_initial_objects_signature = signature
|
|
422
|
-
@
|
|
425
|
+
@prepare_initial_objects_handler = handler
|
|
423
426
|
end
|
|
424
427
|
|
|
428
|
+
# The handler registered with `prepare_initial_objects`
|
|
429
|
+
attr_reader :prepare_initial_objects_handler # :nodoc:
|
|
430
|
+
|
|
425
431
|
# Signature of the registered prepare_initial_objects handler
|
|
426
432
|
attr_reader :prepare_initial_objects_signature # :nodoc:
|
|
427
433
|
|
|
@@ -601,10 +607,10 @@ class Serega
|
|
|
601
607
|
subclass.presets.merge!(presets)
|
|
602
608
|
|
|
603
609
|
# Assign same preload handler
|
|
604
|
-
subclass.preload_with(
|
|
610
|
+
subclass.preload_with(preload_with_handler) if preload_with_handler
|
|
605
611
|
|
|
606
612
|
# Assign same initial objects handler
|
|
607
|
-
subclass.prepare_initial_objects(
|
|
613
|
+
subclass.prepare_initial_objects(prepare_initial_objects_handler) if prepare_initial_objects_handler
|
|
608
614
|
|
|
609
615
|
# Assign same presenter blocks
|
|
610
616
|
presenter_blocks.each do |presenter_block|
|
|
@@ -628,8 +634,8 @@ class Serega
|
|
|
628
634
|
@presenter_key = nil
|
|
629
635
|
@data_classes = {}
|
|
630
636
|
@struct_classes = {}
|
|
631
|
-
@
|
|
632
|
-
@
|
|
637
|
+
@preload_with_handler = nil
|
|
638
|
+
@prepare_initial_objects_handler = nil
|
|
633
639
|
@prepare_initial_objects_signature = nil
|
|
634
640
|
@locked = false
|
|
635
641
|
@shareable = false
|
|
@@ -637,14 +643,12 @@ class Serega
|
|
|
637
643
|
@plan_cache = SeregaPlan::Cache.new(self, SeregaPlan::SelectionPlanner)
|
|
638
644
|
end
|
|
639
645
|
|
|
640
|
-
# The handler given to `method_name` as a value
|
|
641
|
-
def
|
|
642
|
-
raise SeregaError, "#{method_name} accepts a single callable or a block, not both" if
|
|
643
|
-
|
|
644
|
-
handler = value || block
|
|
645
|
-
raise SeregaError, "#{method_name} value must be a Proc or respond to #call" unless handler.respond_to?(:call)
|
|
646
|
+
# The handler given to `method_name` as a value, which must come without a block
|
|
647
|
+
def checked_value_handler(method_name, value, block)
|
|
648
|
+
raise SeregaError, "#{method_name} accepts a single callable or a block, not both" if block
|
|
649
|
+
raise SeregaError, "#{method_name} value must be a Proc or respond to #call" unless value.respond_to?(:call)
|
|
646
650
|
|
|
647
|
-
|
|
651
|
+
value
|
|
648
652
|
end
|
|
649
653
|
|
|
650
654
|
def prepare_initial_objects_signature_error
|
|
@@ -750,7 +754,7 @@ class Serega
|
|
|
750
754
|
end
|
|
751
755
|
|
|
752
756
|
def prepare_objects(objects, context)
|
|
753
|
-
handler = self.class.
|
|
757
|
+
handler = self.class.prepare_initial_objects_handler
|
|
754
758
|
|
|
755
759
|
case self.class.prepare_initial_objects_signature
|
|
756
760
|
when "1" then handler.call(objects)
|
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.51.
|
|
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: []
|
|
@@ -123,6 +123,7 @@ metadata:
|
|
|
123
123
|
source_code_uri: https://github.com/andreyruby/serega
|
|
124
124
|
documentation_uri: https://andreyruby.github.io/serega/
|
|
125
125
|
changelog_uri: https://github.com/andreyruby/serega/blob/master/CHANGELOG.md
|
|
126
|
+
bug_tracker_uri: https://github.com/andreyruby/serega/issues
|
|
126
127
|
rdoc_options:
|
|
127
128
|
- "--markup"
|
|
128
129
|
- markdown
|
|
@@ -143,7 +144,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
143
144
|
- !ruby/object:Gem::Version
|
|
144
145
|
version: '0'
|
|
145
146
|
requirements: []
|
|
146
|
-
rubygems_version: 4.0.
|
|
147
|
+
rubygems_version: 4.0.24
|
|
147
148
|
specification_version: 4
|
|
148
|
-
summary: Ruby
|
|
149
|
+
summary: 'Fast Ruby serializer: clients choose the fields, Serega loads only what
|
|
150
|
+
they use'
|
|
149
151
|
test_files: []
|