serega 0.51.0 → 0.52.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.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +48 -21
  3. data/VERSION +1 -1
  4. data/docs/README.md +41 -0
  5. data/docs/attributes.md +248 -0
  6. data/docs/batch-loading.md +207 -0
  7. data/docs/camel-case.md +86 -0
  8. data/docs/conditional-attributes.md +165 -0
  9. data/docs/configuration.md +47 -0
  10. data/docs/context.md +34 -0
  11. data/docs/errors.md +38 -0
  12. data/docs/formatters.md +231 -0
  13. data/docs/hash-records.md +93 -0
  14. data/docs/migrating-from-root-and-metadata.md +50 -0
  15. data/docs/preloads.md +125 -0
  16. data/docs/prepare-initial-objects.md +72 -0
  17. data/docs/presenter.md +87 -0
  18. data/docs/presets.md +174 -0
  19. data/docs/ractors.md +54 -0
  20. data/docs/relations.md +233 -0
  21. data/docs/responses.md +548 -0
  22. data/docs/root-and-metadata.md +110 -0
  23. data/docs/selecting-fields.md +157 -0
  24. data/docs/serializing.md +113 -0
  25. data/docs/testing.md +230 -0
  26. data/lib/serega/attribute_normalizer.rb +26 -8
  27. data/lib/serega/attribute_normalizers/block.rb +3 -3
  28. data/lib/serega/config.rb +28 -13
  29. data/lib/serega/errors.rb +5 -4
  30. data/lib/serega/plan/cache.rb +13 -29
  31. data/lib/serega/plan/depth_limit.rb +27 -8
  32. data/lib/serega/plan/per_ractor_cache.rb +2 -2
  33. data/lib/serega/plan/point.rb +13 -2
  34. data/lib/serega/plan/selection.rb +10 -0
  35. data/lib/serega/plan.rb +7 -3
  36. data/lib/serega/plugins/context_metadata/context_metadata.rb +1 -1
  37. data/lib/serega/plugins/root/root.rb +10 -10
  38. data/lib/serega/plugins.rb +13 -0
  39. data/lib/serega/result_code/relation_point_code.rb +3 -2
  40. data/lib/serega/source_group.rb +2 -2
  41. data/lib/serega/validations/attribute/check_block.rb +4 -7
  42. data/lib/serega/validations/attribute/check_opt_hash_access.rb +2 -3
  43. data/lib/serega/validations/check_batch_loader_params.rb +2 -1
  44. data/lib/serega.rb +54 -46
  45. data/sig/serega.rbs +10 -8
  46. metadata +30 -6
data/docs/relations.md ADDED
@@ -0,0 +1,233 @@
1
+ # Relations
2
+
3
+ A relation is an attribute serialized with another serializer: an attribute
4
+ with the `:serializer` option, or with a block that defines a
5
+ [nested serializer](#nested-serializers-defined-with-a-block).
6
+
7
+ ```ruby
8
+ class PostSerializer < Serega
9
+ attribute :title
10
+ end
11
+
12
+ class UserSerializer < Serega
13
+ attribute :username
14
+ attribute :posts, serializer: PostSerializer, many: true
15
+ end
16
+
17
+ UserSerializer.to_h(user)
18
+ # => {username: "gru", posts: [{title: "Stealing the Moon"}]}
19
+ ```
20
+
21
+ ## Table of Contents
22
+
23
+ <!-- toc -->
24
+
25
+ - [Relation options](#relation-options)
26
+ - [`:serializer`](#serializer)
27
+ - [`:many`](#many)
28
+ - [`:preset`](#preset)
29
+ - [`:only`, `:with`, `:except`](#only-with-except)
30
+ - [`:preload`](#preload)
31
+ - [Nested serializers defined with a block](#nested-serializers-defined-with-a-block)
32
+ - [Limiting nesting depth](#limiting-nesting-depth)
33
+
34
+ <!-- tocstop -->
35
+
36
+ ## Relation options
37
+
38
+ ### `:serializer`
39
+
40
+ Serializes the value with another serializer. Use a String or a Proc when
41
+ serializers refer to each other.
42
+
43
+ ```ruby
44
+ attribute :posts, serializer: PostSerializer
45
+ attribute :posts, serializer: "PostSerializer"
46
+ attribute :posts, serializer: -> { PostSerializer }
47
+ # => {posts: [{title: "Stealing the Moon"}]}
48
+ ```
49
+
50
+ ### `:many`
51
+
52
+ Serega serializes a value as a list when it is an `Enumerable`, except `Hash`
53
+ and `Struct`. `many: true` marks a list explicitly and serializes `nil` as
54
+ `[]`.
55
+
56
+ ```ruby
57
+ attribute :posts, serializer: PostSerializer, many: true
58
+ # => {posts: []} when user.posts is nil
59
+ # => {posts: [{title: "Stealing the Moon"}]} when user.posts is one post
60
+ ```
61
+
62
+ A relation without `:many` follows `config.many` of its serializer, so a
63
+ serializer with `config.many = false` gets a list as one object, and a
64
+ serializer with `config.many = true` serializes `nil` as `[]`.
65
+
66
+ `config.require_many = true` requires `:many` on every relation, an
67
+ attribute with `:serializer` or a block. A relation without `:many` raises
68
+ `Serega::SeregaError`.
69
+
70
+ ```ruby
71
+ class AppSerializer < Serega
72
+ config.require_many = true
73
+ config.base_serializer = self
74
+ end
75
+
76
+ class PostSerializer < AppSerializer
77
+ attribute :comments, serializer: CommentSerializer, many: true
78
+ attribute :author, many: false do
79
+ attribute :name
80
+ end
81
+ end
82
+ ```
83
+
84
+ ### `:preset`
85
+
86
+ Serializes the fields of a preset of the relation serializer by default. The
87
+ options of the call merge with the preset. See [Preset on a relation](presets.md#preset-on-a-relation).
88
+
89
+ ```ruby
90
+ attribute :posts, serializer: PostSerializer, many: true, preset: :summary
91
+ # => {posts: [{id: 1, title: "Stealing the Moon"}]}
92
+ ```
93
+
94
+ ### `:only`, `:with`, `:except`
95
+
96
+ Select the fields of the relation by default, like the same options of a
97
+ call. See [Field selection options of a relation](presets.md#field-selection-options-of-a-relation).
98
+
99
+ ```ruby
100
+ attribute :posts, serializer: PostSerializer, many: true, only: [:id, :title]
101
+ # => {posts: [{id: 1, title: "Stealing the Moon"}]}
102
+ ```
103
+
104
+ ### `:preload`
105
+
106
+ Names the associations to preload for the value, so the value is read without
107
+ N+1 queries. See [Preloads](preloads.md).
108
+
109
+ ```ruby
110
+ attribute :email, preload: :emails, value: proc { |user| user.emails.find(&:verified?) }
111
+ ```
112
+
113
+ ## Nested serializers defined with a block
114
+
115
+ A block defines an anonymous nested serializer for the attribute value. The
116
+ value is found as usual, by the attribute name or the `:method`, `:value`,
117
+ `:delegate` or `:batch` option.
118
+
119
+ ```ruby
120
+ class UserSerializer < AppSerializer
121
+ attribute :first_name
122
+
123
+ # user.profile
124
+ attribute :profile do
125
+ attribute :city
126
+ end
127
+
128
+ # The user itself, with only its counters
129
+ attribute :statistics, method: :itself do
130
+ attribute :likes_count
131
+ attribute :comments_count
132
+ end
133
+ end
134
+
135
+ UserSerializer.to_h(user)
136
+ # => {first_name: "Felonious", profile: {city: "Freedonia"}, statistics: {likes_count: 10, comments_count: 3}}
137
+ ```
138
+
139
+ The nested serializer is a subclass of a base serializer, set with
140
+ `config.base_serializer` or the `base_serializer:` attribute option. The
141
+ attribute option takes precedence. An attribute block raises
142
+ `Serega::SeregaError` when neither is set.
143
+
144
+ ```ruby
145
+ attribute :statistics, method: :itself, base_serializer: StatisticsSerializer do
146
+ attribute :likes_count
147
+ end
148
+ ```
149
+
150
+ The nested serializer inherits everything from its base serializer: plugins,
151
+ config, attributes, batch loaders and the preload handler. Anything else can be
152
+ declared inside the block:
153
+
154
+ ```ruby
155
+ attribute :statistics, method: :itself do
156
+ batch :statistics, ->(users) { Statistics.for_users(users) } # => {user_id => statistics}
157
+
158
+ attribute :likes_count, batch: :statistics,
159
+ value: proc { |user, batches:| batches[:statistics][user.id].likes }
160
+ end
161
+ ```
162
+
163
+ Good to know:
164
+
165
+ - The nested serializer is created when the attribute is defined. Later
166
+ changes to the base serializer do not reach it.
167
+ - With a [presenter](presenter.md), `method: :itself` serializes the presenter.
168
+ `method: :__getobj__` serializes the original object.
169
+ - Errors name the nested serializer and the line where the attribute is
170
+ defined:
171
+
172
+ ```text
173
+ (when serializing the 'likes_count' attribute in UserSerializer.<statistics>, app/serializers/user_serializer.rb:12)
174
+ ```
175
+
176
+ ## Limiting nesting depth
177
+
178
+ `config.depth_limit` guards against expensive requests and cyclic relations.
179
+ The fields of the serializer itself are depth 1, so `1` allows no relations
180
+ and `2` allows one level of relations. The limit of the serializer that starts
181
+ the serialization applies to all nested serializers.
182
+
183
+ The limit counts only relations, the attributes with a `:serializer`. It does
184
+ not check attribute values: a Hash or an Array that a value returns is
185
+ serialized as it is, at any depth.
186
+
187
+ ```ruby
188
+ class AppSerializer < Serega
189
+ config.depth_limit = 10
190
+ end
191
+
192
+ class UserSerializer < AppSerializer
193
+ config.depth_limit = 2
194
+ attribute :friends, serializer: -> { UserSerializer }, hide: true
195
+ end
196
+
197
+ UserSerializer.to_h(user, with: :friends) # depth 2
198
+ UserSerializer.to_h(user, with: {friends: :friends})
199
+ # => Serega::DepthLimitError: Depth limit was exceeded
200
+ ```
201
+
202
+ `Serega::DepthLimitError#details` shows the path, and on the next line the
203
+ serializer whose limit applies:
204
+
205
+ ```text
206
+ UserSerializer (depth limit: 2) -> friends -> friends
207
+ (hint: if the nesting is not a cycle, increase config.depth_limit of UserSerializer)
208
+ ```
209
+
210
+ The message is `Depth limit was exceeded`, safe to show to API clients.
211
+
212
+ Without a limit, relations that nest in a cycle raise `Serega::DepthLimitError`
213
+ with the message `Relations nest in a cycle`. A cycle is a relation that
214
+ selects the same fields of a serializer that a level above it already selects,
215
+ for example `friends` shown by default in `UserSerializer`:
216
+
217
+ ```ruby
218
+ class UserSerializer < Serega
219
+ attribute :friends, serializer: -> { UserSerializer }
220
+ end
221
+
222
+ UserSerializer.to_h(user)
223
+ # => Serega::DepthLimitError: Relations nest in a cycle
224
+ ```
225
+
226
+ ```text
227
+ UserSerializer -> friends
228
+ (hint: hide a relation of the cycle or set config.depth_limit)
229
+ ```
230
+
231
+ The limit is checked when the serialization plan is built, before any
232
+ serialization. Call `UserSerializer.new(with: params[:with])` early to
233
+ validate a request before running other code.