serega 0.51.1 → 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 (44) 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 +2 -2
  30. data/lib/serega/plan/cache.rb +13 -29
  31. data/lib/serega/plan/per_ractor_cache.rb +2 -2
  32. data/lib/serega/plan/point.rb +13 -2
  33. data/lib/serega/plan.rb +1 -1
  34. data/lib/serega/plugins/context_metadata/context_metadata.rb +1 -1
  35. data/lib/serega/plugins/root/root.rb +10 -10
  36. data/lib/serega/plugins.rb +13 -0
  37. data/lib/serega/result_code/relation_point_code.rb +3 -2
  38. data/lib/serega/source_group.rb +1 -1
  39. data/lib/serega/validations/attribute/check_block.rb +4 -7
  40. data/lib/serega/validations/attribute/check_opt_hash_access.rb +2 -3
  41. data/lib/serega/validations/check_batch_loader_params.rb +2 -1
  42. data/lib/serega.rb +30 -26
  43. data/sig/serega.rbs +10 -8
  44. metadata +23 -1
@@ -0,0 +1,50 @@
1
+ # Migrating from the Root and Metadata Plugins
2
+
3
+ `plugin :root`, `plugin :metadata` and `plugin :context_metadata` are
4
+ deprecated. A [response serializer](responses.md) replaces them: a serializer
5
+ with `data` and `meta` attributes.
6
+
7
+ Before, with the deprecated plugins:
8
+
9
+ ```ruby
10
+ class AppSerializer < Serega
11
+ plugin :root, root_one: :user, root_many: :users
12
+ plugin :metadata
13
+ plugin :context_metadata
14
+
15
+ meta_attribute :version, const: "1.2.3"
16
+ end
17
+
18
+ UserSerializer.to_h(users, meta: {request_id: request.uuid})
19
+ # => {users: [...], version: "1.2.3", request_id: "..."}
20
+ ```
21
+
22
+ After, with a response serializer:
23
+
24
+ ```ruby
25
+ module Serializers
26
+ module Responses
27
+ module Users
28
+ class Index < Base
29
+ attribute :users, serializer: Serializers::User, method: :itself, many: true # Ruby's Object#itself
30
+ attribute :version, const: "1.2.3"
31
+ attribute :request_id, value: proc { |_users, ctx| ctx[:request_id] }
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ Serializers::Responses::Users::Index.to_h(users, many: false, context: {request_id: request.uuid})
38
+ # => {users: [...], version: "1.2.3", request_id: "..."}
39
+ ```
40
+
41
+ - `plugin :root`: the `data` attribute, or an attribute named like the root key.
42
+ - `root_one:` and `root_many:`: `Response.one` and `Response.list` of
43
+ [step 3](responses.md#3-add-a-common-response-shape), or one response serializer each.
44
+ - `to_h(user, root: nil)`: serialize with the record serializer itself.
45
+ - `meta_attribute :meta, :paging do |records, ctx| ... end`: a meta
46
+ serializer with a `paging` attribute, see [step 2](responses.md#2-describe-meta).
47
+ - `hide_nil: true`: `unless_value: :nil?`, see
48
+ [Conditional Attributes](conditional-attributes.md).
49
+ - `meta:` of `plugin :context_metadata`: the [context](context.md), read in an
50
+ attribute value.
data/docs/preloads.md ADDED
@@ -0,0 +1,125 @@
1
+ # Preloads
2
+
3
+ The `:preload` attribute option names associations to load before
4
+ serialization. Serega collects the preloads of the requested fields only, and
5
+ loads each once for all serialized objects.
6
+
7
+ ```ruby
8
+ class AppSerializer < Serega
9
+ plugin :activerecord_preloads
10
+ end
11
+
12
+ class UserSerializer < AppSerializer
13
+ attribute :name
14
+ attribute :followers_count, preload: :user_stats, value: proc { |user| user.user_stats.followers_count }
15
+ attribute :albums, serializer: AlbumSerializer, preload: :albums
16
+ end
17
+
18
+ class AlbumSerializer < AppSerializer
19
+ attribute :title
20
+ attribute :downloads_count, preload: :downloads, value: proc { |album| album.downloads.size }
21
+ end
22
+
23
+ UserSerializer.to_h(users)
24
+ # 1 query for user_stats of all users
25
+ # 1 query for albums of all users
26
+ # 1 query for downloads of all albums
27
+
28
+ UserSerializer.to_h(users, only: :name)
29
+ # no preloads
30
+ ```
31
+
32
+ The `:preload` value is passed to the preload handler as is: a Symbol, Array,
33
+ Hash, or any value your handler understands. `preload: false` and
34
+ `preload: nil` disable preloading for the attribute.
35
+
36
+ To load preloads only for fields that clients request explicitly, see
37
+ [Hiding by default](selecting-fields.md#hiding-by-default).
38
+
39
+ ## Table of Contents
40
+
41
+ <!-- toc -->
42
+
43
+ - [ActiveRecord](#activerecord)
44
+ - [Automatic preloads](#automatic-preloads)
45
+ - [Preloading without ActiveRecord](#preloading-without-activerecord)
46
+
47
+ <!-- tocstop -->
48
+
49
+ ## ActiveRecord
50
+
51
+ `plugin :activerecord_preloads` loads the preloads with
52
+ `ActiveRecord::Associations::Preloader`. It preloads ActiveRecord objects only
53
+ and raises `Serega::SeregaError` for other objects. Other objects need a
54
+ [custom preload handler](#preloading-without-activerecord).
55
+
56
+ ## Automatic preloads
57
+
58
+ `config.auto_preload = true` adds preloads automatically:
59
+
60
+ - `delegate: {to: :user_stats}` preloads `:user_stats`;
61
+ - `serializer: AlbumSerializer` or an attribute block preloads the association
62
+ with the attribute's name or `:method`.
63
+
64
+ ```ruby
65
+ class AppSerializer < Serega
66
+ plugin :activerecord_preloads
67
+ config.auto_preload = true
68
+ end
69
+
70
+ class UserSerializer < AppSerializer
71
+ attribute :comments_count, delegate: {to: :user_stats} # preloads :user_stats
72
+ attribute :albums, serializer: AlbumSerializer # preloads :albums
73
+ attribute :tracks, serializer: TrackSerializer, preload: {albums: :tracks}
74
+ end
75
+ ```
76
+
77
+ Use `config.auto_preload = {has_delegate_option: true}` or
78
+ `{has_serializer_option: true}` to enable one of them. A `:preload` option
79
+ replaces the automatic preload. Attributes with `:batch` and methods listed in
80
+ `config.auto_preload_excluded_methods` (`:itself` and `:__getobj__` by default)
81
+ are not preloaded:
82
+
83
+ ```ruby
84
+ # Serializes the same user, preloading only what UserStatisticsSerializer declares
85
+ attribute :statistics, method: :itself, serializer: UserStatisticsSerializer
86
+ ```
87
+
88
+ ## Preloading without ActiveRecord
89
+
90
+ `plugin :activerecord_preloads` registers a preload handler. For another ORM
91
+ or for plain Ruby objects, register your own with `preload_with`:
92
+
93
+ ```ruby
94
+ class AppSerializer < Serega
95
+ preload_with { |objects, preloads| RecordPreloader.call(objects, preloads) }
96
+ # or any callable with (objects, preloads)
97
+ preload_with AppPreloader
98
+ end
99
+ ```
100
+
101
+ The handler receives the objects to serialize and the `:preload` value of one
102
+ attribute. Child serializers inherit it. A `:preload` option without a handler
103
+ raises `Serega::SeregaError` on serialization.
104
+
105
+ The handler runs once per attribute with a preload. When several attributes
106
+ share a `:preload` value, it runs once for each of them over the same objects,
107
+ so it should skip data that is already loaded. The `:activerecord_preloads`
108
+ handler skips loaded associations.
109
+
110
+ The `:preload` value can be any value, so it can also tell the handler where to
111
+ load data from:
112
+
113
+ ```ruby
114
+ class PostSerializer < AppSerializer
115
+ attribute :owner, serializer: UserSerializer, preload: :owner
116
+ attribute :author, serializer: UserSerializer, preload: :author
117
+
118
+ preload_with do |posts, preloads|
119
+ case preloads
120
+ when :owner then OwnersLoader.call(posts) # from the database
121
+ when :author then AuthorsApi.load_into(posts) # from an external API
122
+ end
123
+ end
124
+ end
125
+ ```
@@ -0,0 +1,72 @@
1
+ # Prepare Initial Objects
2
+
3
+ `prepare_initial_objects` replaces the objects given to a serializer before
4
+ serialization. A serializer can accept ids and load the records itself.
5
+
6
+ ```ruby
7
+ class UserSerializer < Serega
8
+ prepare_initial_objects { |user_ids| User.where(id: user_ids) }
9
+
10
+ attribute :first_name
11
+ end
12
+
13
+ UserSerializer.to_h(["17", "42"]) # => [{first_name: "Ann"}, {first_name: "Bob"}]
14
+ ```
15
+
16
+ The handler is a block or any callable. It receives the given objects and,
17
+ optionally, the [context](context.md) as a second parameter or the `:ctx`
18
+ keyword:
19
+
20
+ ```ruby
21
+ prepare_initial_objects { |ids, ctx| User.where(id: ids, account: ctx[:account]) }
22
+ prepare_initial_objects { |ids, ctx:| User.where(id: ids, account: ctx[:account]) }
23
+ prepare_initial_objects UsersLoader
24
+ ```
25
+
26
+ ## Table of Contents
27
+
28
+ <!-- toc -->
29
+
30
+ - [Single objects and lists](#single-objects-and-lists)
31
+ - [Preloads](#preloads)
32
+ - [Where the handler runs](#where-the-handler-runs)
33
+
34
+ <!-- tocstop -->
35
+
36
+ ## Single objects and lists
37
+
38
+ The handler runs before Serega decides whether to serialize a list, so its
39
+ result decides it:
40
+
41
+ ```ruby
42
+ UserSerializer.to_h("17") # => [{first_name: "Ann"}], as User.where returns a list
43
+ ```
44
+
45
+ A given `:many` option is used as is. A handler that returns `nil` serializes
46
+ `nil`.
47
+
48
+ ## Preloads
49
+
50
+ [Preloads](preloads.md) are applied to the prepared objects:
51
+
52
+ ```ruby
53
+ class UserSerializer < Serega
54
+ plugin :activerecord_preloads
55
+ prepare_initial_objects { |user_ids| User.where(id: user_ids) }
56
+
57
+ attribute :albums_count, preload: :albums, value: proc { |user| user.albums.size }
58
+ end
59
+ ```
60
+
61
+ [`plugin :activerecord_preloads`](preloads.md#activerecord)
62
+ preloads ActiveRecord objects only, and raises `Serega::SeregaError` for other
63
+ objects. For read-only records, Structs or Hashes, use
64
+ [batch loading](batch-loading.md).
65
+
66
+ ## Where the handler runs
67
+
68
+ - The handler runs for the objects given to the serializer only, not for
69
+ objects of nested serializers.
70
+ - Subclasses inherit the handler. Declare it on the serializers that need it,
71
+ not on a base serializer.
72
+ - Errors raised by the handler reach the caller unchanged.
data/docs/presenter.md ADDED
@@ -0,0 +1,87 @@
1
+ # Presenter
2
+
3
+ A `presenter` block holds computed attributes as methods, so the serializer
4
+ reads as a list of fields.
5
+
6
+ ```ruby
7
+ class UserSerializer < Serega
8
+ attribute :name
9
+ attribute :role
10
+
11
+ presenter do
12
+ def name
13
+ [first_name, last_name].compact.join(" ")
14
+ end
15
+
16
+ def role
17
+ (id == __ctx__[:current_user_id]) ? :self : :other
18
+ end
19
+ end
20
+ end
21
+
22
+ UserSerializer.to_h(user, context: {current_user_id: 1})
23
+ # => {name: "Felonious Gru", role: :self}
24
+ ```
25
+
26
+ The same serializer with `:value` options:
27
+
28
+ ```ruby
29
+ class UserSerializer < Serega
30
+ attribute :name, value: proc { |user| [user.first_name, user.last_name].compact.join(" ") }
31
+ attribute :role, value: proc { |user, ctx| (user.id == ctx[:current_user_id]) ? :self : :other }
32
+ end
33
+ ```
34
+
35
+ ## Table of Contents
36
+
37
+ <!-- toc -->
38
+
39
+ - [Inside presenter methods](#inside-presenter-methods)
40
+ - [Performance cost](#performance-cost)
41
+ - [Inheritance](#inheritance)
42
+
43
+ <!-- tocstop -->
44
+
45
+ ## Inside presenter methods
46
+
47
+ Presenter methods run on a `SimpleDelegator` that wraps the serialized object:
48
+
49
+ - methods of the object are available directly, like `first_name` above;
50
+ - `__getobj__` returns the object;
51
+ - `__ctx__` returns the [context](context.md).
52
+
53
+ Objects are wrapped only when a serialized attribute needs the wrapper: it
54
+ reads a presenter method, or it has a `:value` callable, a batch loader or a
55
+ callable `:if` or `:unless` condition. Then these callables and batch loaders
56
+ receive the wrapped objects, and preload handlers receive the original
57
+ objects. With `attribute :email` added above, `only: [:email]` wraps nothing.
58
+
59
+ ## Performance cost
60
+
61
+ Presenter methods are slower than `:value` options: each object is wrapped,
62
+ and each attribute is read through the wrapper. Fields selected without
63
+ presenter methods skip the wrapper, thus they serialize as fast as without a
64
+ presenter. 20 objects with 10
65
+ attributes, 2 of them computed, serialize in 11.7 µs with presenter methods and
66
+ in 6.3 µs with `:value` options, about twice as fast. The difference is a few
67
+ microseconds, thus it matters little in a request that takes milliseconds.
68
+
69
+ ## Inheritance
70
+
71
+ `presenter` blocks add up. A child serializer gets its parent's presenter
72
+ methods, and its own methods stay out of the parent.
73
+
74
+ ```ruby
75
+ class AdminSerializer < UserSerializer
76
+ attribute :title
77
+
78
+ presenter do
79
+ def title
80
+ "Sir #{name}"
81
+ end
82
+ end
83
+ end
84
+ ```
85
+
86
+ A child copies its parent's blocks when it is created. A `presenter` block
87
+ added to the parent later does not reach existing children.
data/docs/presets.md ADDED
@@ -0,0 +1,174 @@
1
+ # Presets
2
+
3
+ A preset saves `:only`, `:with` and `:except` under a name, at any depth. The
4
+ `:preset` option selects its fields, so a client sends a short name instead of
5
+ a long list of fields.
6
+
7
+ ```ruby
8
+ class UserSerializer < Serega
9
+ attribute :username
10
+ attribute :first_name
11
+ attribute :email, hide: true
12
+ attribute :friends, serializer: UserSerializer, hide: true
13
+
14
+ preset :card, only: [:username, {friends: :username}]
15
+ preset :profile, preset: :card, with: :email
16
+ end
17
+
18
+ UserSerializer.to_h(gru, preset: :card)
19
+ # => {username: "gru", friends: [{username: "lucy"}]}
20
+
21
+ UserSerializer.to_h(gru, preset: :profile)
22
+ # => {username: "gru", email: "gru@example.com", friends: [{username: "lucy"}]}
23
+ ```
24
+
25
+ - `preset:` in a preset definition names presets defined earlier. Their
26
+ fields merge in when the preset is used.
27
+ - A preset with the name of an existing preset replaces it, also in the
28
+ presets that include it.
29
+ - Subclasses inherit the presets, and can redefine them without changing the
30
+ parent serializer.
31
+ - `preset:` in a serialization takes a name or an Array of names. An
32
+ undefined preset raises `Serega::PresetNotExist`.
33
+ - A preset is checked when it is defined: its name, the presets it includes,
34
+ that presets do not include each other, and the attributes it names. An
35
+ unknown attribute raises `Serega::AttributeNotExist`:
36
+ `Preset :card names not existing attributes: nickname`. The
37
+ nested fields of a relation with a serializer name or a Proc are not
38
+ checked then, so that serializer can be defined later. They are checked
39
+ with the options of the call, like other fields.
40
+
41
+ ## Table of Contents
42
+
43
+ <!-- toc -->
44
+
45
+ - [Presets with other options](#presets-with-other-options)
46
+ - [Preset on a relation](#preset-on-a-relation)
47
+ - [Field selection options of a relation](#field-selection-options-of-a-relation)
48
+ - [When to use a preset](#when-to-use-a-preset)
49
+
50
+ <!-- tocstop -->
51
+
52
+ ## Presets with other options
53
+
54
+ Each option of the call merges with the option of the same name of the
55
+ presets. The result is the same as writing the merged options by hand, so
56
+ the rules of [Combining options](selecting-fields.md#combining-options) apply. `:only` and
57
+ `:with` add fields to a preset, and `:except` removes them:
58
+
59
+ ```ruby
60
+ UserSerializer.to_h(gru, preset: :card, only: {friends: :first_name})
61
+ # => {username: "gru", friends: [{username: "lucy", first_name: "Lucy"}]}
62
+
63
+ UserSerializer.to_h(gru, preset: :card, except: :friends)
64
+ # => {username: "gru"}
65
+
66
+ UserSerializer.to_h(gru, preset: [:card, :profile], with: :first_name)
67
+ ```
68
+
69
+ Options name only attributes: `with: {friends: :card}` names an attribute
70
+ `card` of the friends.
71
+
72
+ ## Preset on a relation
73
+
74
+ `preset:` on a relation names presets of the relation serializer. The
75
+ relation serializes the fields of these presets by default. The relation
76
+ serializer owns the list of fields: each parent names it once, and a change
77
+ to the preset changes every parent.
78
+
79
+ ```ruby
80
+ class PostSerializer < Serega
81
+ attribute :id
82
+ attribute :title
83
+ attribute :body
84
+
85
+ preset :summary, only: [:id, :title]
86
+ end
87
+
88
+ class UserSerializer < Serega
89
+ attribute :username
90
+ attribute :posts, serializer: PostSerializer, many: true, preset: :summary
91
+
92
+ preset :card, only: [:username, :posts]
93
+ end
94
+
95
+ UserSerializer.to_h(gru)
96
+ # => {username: "gru", posts: [{id: 1, title: "Stealing the Moon"}]}
97
+
98
+ PostSerializer.to_h(post)
99
+ # => {id: 1, title: "Stealing the Moon", body: "Shrink ray plans"}
100
+ ```
101
+
102
+ The options of the call for the relation merge with the preset, like
103
+ [Presets with other options](#presets-with-other-options):
104
+
105
+ | Call | Fields of `posts` |
106
+ |---|---|
107
+ | `to_h(gru)` | `id`, `title` |
108
+ | `to_h(gru, only: :posts)` | `id`, `title` |
109
+ | `to_h(gru, preset: :card)` | `id`, `title` |
110
+ | `to_h(gru, with: {posts: :body})` | `id`, `title`, `body` |
111
+ | `to_h(gru, except: {posts: :title})` | `id` |
112
+ | `to_h(gru, only: {posts: :id})` | `id`, `title`: `:only` adds fields to the preset |
113
+ | `to_h(gru, except: :posts)` | `posts` is not serialized |
114
+
115
+ An option that names the relation without nested fields keeps the preset.
116
+ `:except` with nested fields removes only these fields, and the relation
117
+ stays:
118
+
119
+ ```ruby
120
+ class CommentSerializer < Serega
121
+ attribute :text
122
+ attribute :user, serializer: UserSerializer
123
+ end
124
+
125
+ CommentSerializer.to_h(comment, except: {user: :posts})
126
+ # => {text: "Nice", user: {username: "gru"}}
127
+ ```
128
+
129
+ - `preset:` takes a name or an Array of names, like the option of a call.
130
+ - A relation needs `:serializer` or a block. `preset:` without them raises
131
+ `Serega::SeregaError`.
132
+ - When `:serializer` is a class, an undefined preset raises
133
+ `Serega::PresetNotExist` when the attribute is defined. With a serializer
134
+ name, a Proc or a block, it raises when the relation is first serialized.
135
+
136
+ ## Field selection options of a relation
137
+
138
+ `:only`, `:with` and `:except` on a relation select the fields of the
139
+ relation by default, like the same options of a call. They fit a list of
140
+ fields that only this relation needs; a list that several relations or calls
141
+ use fits a preset of the relation serializer.
142
+
143
+ ```ruby
144
+ class UserSerializer < Serega
145
+ attribute :username
146
+ attribute :posts, serializer: PostSerializer, many: true, only: [:id, :title]
147
+ attribute :drafts, serializer: PostSerializer, many: true, preset: :summary, with: :body
148
+ end
149
+
150
+ UserSerializer.to_h(gru)
151
+ # => {username: "gru", posts: [{id: 1, title: "Stealing the Moon"}],
152
+ # drafts: [{id: 2, title: "Arkham", body: "Draft notes"}]}
153
+ ```
154
+
155
+ The relation presets, the options of the relation and the options of the
156
+ call merge, with the rules of [Combining options](selecting-fields.md#combining-options):
157
+ `to_h(gru, with: {posts: :body})` adds `body` to the posts.
158
+
159
+ When `:serializer` is a class, an unknown field raises
160
+ `Serega::AttributeNotExist` when the attribute is defined:
161
+ `Relation :posts names not existing attributes: subtitle`. With a serializer
162
+ name, a Proc or a block, it raises when the relation is first serialized.
163
+
164
+ ## When to use a preset
165
+
166
+ One way to choose between the options:
167
+
168
+ | Need | Option |
169
+ |---|---|
170
+ | A field is serialized only on request | [`hide: true`](attributes.md#hidden-attributes) |
171
+ | A relation serializes the same fields in every response | [`preset:` on the relation](#preset-on-a-relation) |
172
+ | Only one relation needs its list of fields | [Options on the relation](#field-selection-options-of-a-relation) |
173
+ | A client or an endpoint chooses the fields | `preset:`, `:only`, `:with` or `:except` in the call |
174
+ | The fields have other values or logic, not only fewer fields | Another serializer |
data/docs/ractors.md ADDED
@@ -0,0 +1,54 @@
1
+ # Ractors
2
+
3
+ End a serializer definition with `make_shareable!` to serialize in other
4
+ Ractors. Define serializers in the main Ractor, then serialize anywhere:
5
+
6
+ ```ruby
7
+ class UserSerializer < Serega
8
+ attribute :name
9
+ make_shareable!
10
+ end
11
+
12
+ Ractor.new(user) { |user| UserSerializer.to_h(user) }.value
13
+ # => {name: "Ann"}
14
+ ```
15
+
16
+ - Call `make_shareable!` in relation serializers too. Nested serializers
17
+ defined with a block become shareable with their parent.
18
+ - A shareable serializer can not be changed.
19
+ - Each Ractor builds and caches its own serialization plans and
20
+ [presenter](presenter.md) class.
21
+ - On a Ruby without Ractors, for example TruffleRuby, `make_shareable!` only
22
+ locks the serializer.
23
+
24
+ ## What can be shared
25
+
26
+ `make_shareable!` deep-freezes the serializer definitions with
27
+ `Ractor.make_shareable`: attribute options, formatters, batch loaders,
28
+ `preload_with` and `prepare_initial_objects` handlers.
29
+
30
+ - A block given to `attribute`, `batch`, `formatter` or another definition is
31
+ shareable when it is written in the serializer class and refers only to
32
+ shareable values.
33
+ - A callable object that changes its own state, for example a formatter that
34
+ caches values in an instance variable, raises `FrozenError` when it
35
+ serializes.
36
+
37
+ A serializer with a definition that can not be shared serializes in the main
38
+ Ractor only. In other Ractors it raises `Serega::SeregaError` with the reason:
39
+
40
+ ```ruby
41
+ class UserSerializer < Serega
42
+ suffix = +"!"
43
+ attribute :greeting, value: proc { |user| user.name + suffix }
44
+ make_shareable!
45
+ end
46
+
47
+ UserSerializer.to_h(user) # => {greeting: "Ann!"}
48
+
49
+ Ractor.new(user) { |user| UserSerializer.to_h(user) }.value
50
+ # => Serega::SeregaError: Attribute :greeting of UserSerializer can not be
51
+ # shared between Ractors (app/serializers/user_serializer.rb:3): cannot make
52
+ # a shareable Proc because it can refer unshareable object "!" from variable
53
+ # 'suffix'
54
+ ```