barley 0.6 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +18 -23
- data/lib/barley/error.rb +3 -0
- data/lib/barley/serializer.rb +14 -2
- data/lib/barley/version.rb +1 -1
- data/lib/barley.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 802ad211955dc1b4f0579ff2f88ac1e88a56eb2425317ae46b1cda590cfc1f26
|
4
|
+
data.tar.gz: 5f5e309f03e3422f9b2bd7e7b7319994c5c63d3367043e6f6c19328784e73c01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3189c87d7fe0d3cdb96a6c503a56f0469a3f97cd965feeafb0337e351cdd8199990b63ac1d119381ef3058914ed07b41f81fc9b0d1789059ae3d944171731226
|
7
|
+
data.tar.gz: 000bdb7632ea4ab4c1d015fdae029220dc35cbae03ddd1999cf016f21e1c08f46699a7ad610fcc816abbb21e014408c2b838e37a3ef036cb3806b373981f15ab
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
Barley is a fast and efficient ActiveModel serializer.
|
8
8
|
|
9
|
-
Cerealize your ActiveModel objects into flat hashes with a dead simple, yet versatile DSL, and caching and type-checking baked in. Our daily bread is to make your API faster.
|
9
|
+
Cerealize your ActiveModel objects into flat hashes with a dead simple, yet versatile DSL, and caching and type-checking baked in. Our daily bread is to make your API faster.
|
10
10
|
|
11
11
|
You don't believe us? Check out the [benchmarks](#benchmarks). 😎
|
12
12
|
|
@@ -28,30 +28,30 @@ Then define your attributes and associations in a serializer class.
|
|
28
28
|
```ruby
|
29
29
|
# /app/serializers/user_serializer.rb
|
30
30
|
class UserSerializer < Barley::Serializer
|
31
|
-
|
31
|
+
|
32
32
|
attributes id: Types::Strict::Integer, :name
|
33
|
-
|
33
|
+
|
34
34
|
attribute :email
|
35
35
|
attribute :value, type: Types::Coercible::Integer
|
36
36
|
|
37
37
|
many :posts
|
38
|
-
|
38
|
+
|
39
39
|
many :posts, key_name: :featured, scope: :featured
|
40
|
-
|
40
|
+
|
41
41
|
many :posts, key_name: :popular, scope: -> { where("views > 10_000").limit(3) }
|
42
|
-
|
42
|
+
|
43
43
|
one :group, serializer: CustomGroupSerializer
|
44
|
-
|
44
|
+
|
45
45
|
many :related_users, key: :friends, cache: true
|
46
|
-
|
46
|
+
|
47
47
|
one :profile, cache: { expires_in: 1.day } do
|
48
48
|
attributes :avatar, :social_url
|
49
|
-
|
49
|
+
|
50
50
|
attribute :badges do
|
51
51
|
object.badges.map(&:display_name)
|
52
52
|
end
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
end
|
56
56
|
```
|
57
57
|
|
@@ -89,7 +89,7 @@ You can also define the serializer class with the `serializer` macro.
|
|
89
89
|
# /app/models/user.rb
|
90
90
|
class User < ApplicationRecord
|
91
91
|
include Barley::Serializable
|
92
|
-
|
92
|
+
|
93
93
|
serializer UserSerializer
|
94
94
|
end
|
95
95
|
```
|
@@ -143,12 +143,12 @@ You can define a custom serializer for the association with the `serializer` opt
|
|
143
143
|
|
144
144
|
You can of course define serializers with inner classes for simple needs.
|
145
145
|
|
146
|
-
```ruby
|
146
|
+
```ruby
|
147
147
|
class UserSerializer < Barley::Serializer
|
148
148
|
attributes :id, :name, :email, :created_at, :updated_at
|
149
149
|
|
150
150
|
one :group, serializer: LocalGroupSerializer
|
151
|
-
|
151
|
+
|
152
152
|
class LocalGroupSerializer < Barley::Serializer
|
153
153
|
attributes :id, :name
|
154
154
|
end
|
@@ -208,7 +208,7 @@ Feel like using a block to define your associations? You can do that too.
|
|
208
208
|
```ruby
|
209
209
|
many :posts do
|
210
210
|
attributes :id, :title, :body
|
211
|
-
|
211
|
+
|
212
212
|
one :author do
|
213
213
|
attributes :name, :email
|
214
214
|
end
|
@@ -240,7 +240,7 @@ class PostSerializer < Barley::Serializer
|
|
240
240
|
attribute :is_owner do
|
241
241
|
object.user == context.current_user
|
242
242
|
end
|
243
|
-
|
243
|
+
|
244
244
|
many :comments do
|
245
245
|
many :likes do
|
246
246
|
attribute :is_owner do
|
@@ -264,7 +264,7 @@ serializer = PostSerializer.new(Post.last, context: my_context)
|
|
264
264
|
You have two generators available. One to generate the serializer class:
|
265
265
|
|
266
266
|
```shell
|
267
|
-
rails generate barley:serializer User
|
267
|
+
rails generate barley:serializer User
|
268
268
|
# or
|
269
269
|
rails generate barley:serializer User --name=CustomUserSerializer
|
270
270
|
```
|
@@ -318,7 +318,7 @@ end
|
|
318
318
|
```
|
319
319
|
|
320
320
|
## Type checking
|
321
|
-
Barley can check the type of the object you are serializing with the [dry-types](https://dry-rb.org/gems/dry-types/main/) gem.
|
321
|
+
Barley can check the type of the object you are serializing with the [dry-types](https://dry-rb.org/gems/dry-types/main/) gem.
|
322
322
|
|
323
323
|
It will raise an error if the object is not of the expected type, or coerce it to the correct type and perform constraints checks.
|
324
324
|
|
@@ -345,7 +345,7 @@ You will soon be able to replace all occurrences of `Serializer` with `Cerealize
|
|
345
345
|
# /app/models/user.rb
|
346
346
|
class User < ApplicationRecord
|
347
347
|
include Barley::Cerealizable
|
348
|
-
|
348
|
+
|
349
349
|
cerealizer UserCerealizer
|
350
350
|
end
|
351
351
|
|
@@ -464,8 +464,3 @@ Make sure you adhere to [our code of conduct](CODE_OF_CONDUCT.md). We aim to kee
|
|
464
464
|
## Security
|
465
465
|
|
466
466
|
Please refer to our [security guidelines](SECURITY.md)
|
467
|
-
|
468
|
-
## Credits
|
469
|
-
Barley is brought to you by the developer team from [StockPro](https://www.stock-pro.fr/).
|
470
|
-
|
471
|
-
[![Barley is brought to you by StockPro](https://i.imgur.com/5a0veEG.png)](https://www.stock-pro.fr/)
|
data/lib/barley/error.rb
CHANGED
data/lib/barley/serializer.rb
CHANGED
@@ -73,11 +73,18 @@ module Barley
|
|
73
73
|
# @param key_name [Symbol] the key name in the hash
|
74
74
|
# @param type [Dry::Types] the type to use, or coerce the value to
|
75
75
|
# @param block [Proc] a block to use to compute the value
|
76
|
+
# @raise [Barley::InvalidAttributeError] if the value does not match the type - when a type is provided
|
76
77
|
def attribute(key, key_name: nil, type: nil, &block)
|
77
78
|
key_name ||= key
|
78
79
|
define_method(key_name) do
|
79
80
|
value = block ? instance_eval(&block) : object.send(key)
|
80
|
-
type.nil?
|
81
|
+
if type.nil?
|
82
|
+
value
|
83
|
+
else
|
84
|
+
raise Barley::InvalidAttributeError, "Invalid value type found for attribute #{key_name}::#{type.name}: #{value}::#{value.class}" unless type.valid?(value)
|
85
|
+
|
86
|
+
type[value]
|
87
|
+
end
|
81
88
|
end
|
82
89
|
|
83
90
|
self.defined_attributes = (defined_attributes || []) << key_name
|
@@ -172,7 +179,7 @@ module Barley
|
|
172
179
|
# @param key_name [Symbol] the key name in the hash
|
173
180
|
# @param serializer [Class] the serializer to use
|
174
181
|
# @param cache [Boolean, Hash<Symbol, ActiveSupport::Duration>] whether to cache the result, or a hash with options for the cache
|
175
|
-
# @param scope [Symbol] the scope to use to fetch the elements
|
182
|
+
# @param scope [Symbol, Proc] the scope to use to fetch the elements
|
176
183
|
# @param block [Proc] a block to use to define the serializer inline
|
177
184
|
def many(key, key_name: nil, serializer: nil, cache: false, scope: nil, &block)
|
178
185
|
key_name ||= key
|
@@ -183,6 +190,11 @@ module Barley
|
|
183
190
|
end
|
184
191
|
define_method(key_name) do
|
185
192
|
elements = object.send(key)
|
193
|
+
if scope.is_a?(Symbol)
|
194
|
+
elements = elements.send(scope)
|
195
|
+
elsif scope.is_a?(Proc)
|
196
|
+
elements = elements.instance_exec(&scope)
|
197
|
+
end
|
186
198
|
return [] if elements.empty?
|
187
199
|
|
188
200
|
el_serializer = serializer || elements.first.serializer.class
|
data/lib/barley/version.rb
CHANGED
data/lib/barley.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: barley
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cedric Delalande
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-06-
|
11
|
+
date: 2024-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|