barley 0.6 → 0.6.1
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 -18
- data/lib/barley/serializer.rb +6 -1
- data/lib/barley/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 80e724f1d510a0774072dab90c06262278a06fd49adff8a77314852c2aafbe7f
|
4
|
+
data.tar.gz: a13898b638153e90c95c25d0558c54e63b2514c6095be81c5755fb5d92dbb544
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da037e1c4749228e9fa48db4dcc00a8b614ebac1acc5ba0646c61fce0b77a377e530581b7536bc08b3111747ed6303ce60d3a275ac4c8cef2231a1ef726d873b
|
7
|
+
data.tar.gz: 49b297c3db282da63110f28d9599157ac91f50c7fec42916e4a387c20ef0010ebfd3e4b4c53b03baa61985cdf17cdd7b79412eb388551d7c5a58d2c5f00b36b3
|
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
|
|
data/lib/barley/serializer.rb
CHANGED
@@ -172,7 +172,7 @@ module Barley
|
|
172
172
|
# @param key_name [Symbol] the key name in the hash
|
173
173
|
# @param serializer [Class] the serializer to use
|
174
174
|
# @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
|
175
|
+
# @param scope [Symbol, Proc] the scope to use to fetch the elements
|
176
176
|
# @param block [Proc] a block to use to define the serializer inline
|
177
177
|
def many(key, key_name: nil, serializer: nil, cache: false, scope: nil, &block)
|
178
178
|
key_name ||= key
|
@@ -183,6 +183,11 @@ module Barley
|
|
183
183
|
end
|
184
184
|
define_method(key_name) do
|
185
185
|
elements = object.send(key)
|
186
|
+
if scope.is_a?(Symbol)
|
187
|
+
elements = elements.send(scope)
|
188
|
+
elsif scope.is_a?(Proc)
|
189
|
+
elements = elements.instance_exec(&scope)
|
190
|
+
end
|
186
191
|
return [] if elements.empty?
|
187
192
|
|
188
193
|
el_serializer = serializer || elements.first.serializer.class
|
data/lib/barley/version.rb
CHANGED