barley 0.4.1 → 0.5
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/README.md +19 -0
- data/lib/barley/serializer.rb +24 -4
- data/lib/barley/version.rb +1 -1
- 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: 4808cb10061c5a86be6edbf532a6fb041519e4b7b4a08b770e0d63854a5f61e1
|
4
|
+
data.tar.gz: 5ccb1ed50e9d440a0bb3e9c006280e970b70b5ecc64afb7b8660c0d326f58239
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4562ae18d898be75d1abf2d2872bd5f79333f38e6a3e111548624ba6e4b19f4f7316087e36ff35f9103cac56a6b96e9d2f1fb32d1cfb1596430e1b81a1f68c2a
|
7
|
+
data.tar.gz: 13c86fbcb1d2678165bd128ea0e57ca80e1e4d63e3a3ccee09f9ac85838bab7cd0894d75cc832c7a513ac29fd697573039b8ee5a1621be1152e2a22c6525b5ce
|
data/README.md
CHANGED
@@ -200,6 +200,25 @@ Feel like using a block to define your associations? You can do that too.
|
|
200
200
|
end
|
201
201
|
```
|
202
202
|
|
203
|
+
## Context
|
204
|
+
You can pass a context to the serializer with the `with_context` method.
|
205
|
+
|
206
|
+
```ruby
|
207
|
+
serializer = PostSerializer.new(Post.last).with_context(current_user: current_user)
|
208
|
+
```
|
209
|
+
|
210
|
+
This context will be available in the serializer with the `context` method.
|
211
|
+
|
212
|
+
```ruby
|
213
|
+
class PostSerializer < Barley::Serializer
|
214
|
+
attributes :id, :title, :body
|
215
|
+
|
216
|
+
attribute :is_owner do
|
217
|
+
object.user == context.current_user
|
218
|
+
end
|
219
|
+
end
|
220
|
+
```
|
221
|
+
|
203
222
|
## Generators
|
204
223
|
You have two generators available. One to generate the serializer class:
|
205
224
|
|
data/lib/barley/serializer.rb
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
module Barley
|
4
4
|
class Serializer
|
5
5
|
attr_accessor :object
|
6
|
+
attr_accessor :context
|
6
7
|
|
7
8
|
class << self
|
8
9
|
attr_accessor :defined_attributes
|
@@ -224,6 +225,25 @@ module Barley
|
|
224
225
|
Barley::Cache.delete(key)
|
225
226
|
end
|
226
227
|
|
228
|
+
# Sets the context object for the serializer
|
229
|
+
#
|
230
|
+
# The context object is a Struct built from the given arguments.
|
231
|
+
# It can be used to pass additional data to the serializer. The context object is accessible in the serializer with the `context` attribute.
|
232
|
+
# @example
|
233
|
+
# serializer.with_context(current_user: current_user, locale: I18n.locale)
|
234
|
+
# # => #<Barley::Serializer:0x00007f8f3b8b3e08 @object=#<Product id: 1, name: "Product 1">, @context=#<struct current_user=1, locale=:en>>
|
235
|
+
# # In the serializer:
|
236
|
+
# attribute :name do
|
237
|
+
# "#{object.name[context.locale]}" # Will use the locale from the context
|
238
|
+
# end
|
239
|
+
# @param args [Hash] the context object attributes
|
240
|
+
# @return [Barley::Serializer] the serializer
|
241
|
+
def with_context(**args)
|
242
|
+
@context = Struct.new(*args.keys).new(*args.values)
|
243
|
+
|
244
|
+
self
|
245
|
+
end
|
246
|
+
|
227
247
|
private
|
228
248
|
|
229
249
|
# @api private
|
@@ -247,15 +267,15 @@ module Barley
|
|
247
267
|
# Serializes the object
|
248
268
|
#
|
249
269
|
# @api private
|
270
|
+
# @raise [Barley::Error] if no attribute or relation is defined in the serializer
|
250
271
|
#
|
251
272
|
# @return [Hash] the serializable hash
|
252
273
|
def _serializable_hash
|
253
|
-
|
274
|
+
raise Barley::Error, "No attribute or relation defined in #{self.class}" if defined_attributes.blank?
|
254
275
|
|
255
|
-
defined_attributes.
|
256
|
-
|
276
|
+
hash = defined_attributes.each_with_object({}) do |key, result|
|
277
|
+
result[key] = send(key)
|
257
278
|
end
|
258
|
-
|
259
279
|
@root ? {root_key => hash} : hash
|
260
280
|
end
|
261
281
|
|
data/lib/barley/version.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: 0.
|
4
|
+
version: '0.5'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cedric Delalande
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-04-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|