barley 0.6.1 → 0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -6
- data/lib/barley/error.rb +3 -0
- data/lib/barley/serializable.rb +1 -1
- data/lib/barley/serializer.rb +34 -6
- 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: f6246aafe191aa99a01d5bcda56530dda4be81aa4b83a4626a138074b857524b
|
4
|
+
data.tar.gz: 483efb36394fcb402461336e82ba348d7960d59911885e0239f5e28a9a255716
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b820717e738c2a0e4e5f19f01c16729d844ceaa5098d3a427ced289ef670da1827635f75ea7c55781e00c92e26d47437dccdfe298ee26658bb24688850415c2
|
7
|
+
data.tar.gz: 8b080b1d166123c465a21a3728cee601c677d15766728c46c24fe3f957fd47974317e49024d9da84db21c028ef29fad9b6ab8baa1365541f00b9fc0c451464da
|
data/README.md
CHANGED
@@ -40,6 +40,8 @@ class UserSerializer < Barley::Serializer
|
|
40
40
|
|
41
41
|
many :posts, key_name: :popular, scope: -> { where("views > 10_000").limit(3) }
|
42
42
|
|
43
|
+
many :posts, key_name: :in_current_language, scope: -> (context) { where(language: context.language) }
|
44
|
+
|
43
45
|
one :group, serializer: CustomGroupSerializer
|
44
46
|
|
45
47
|
many :related_users, key: :friends, cache: true
|
@@ -59,7 +61,7 @@ Then just use the `as_json` method on your model.
|
|
59
61
|
|
60
62
|
```ruby
|
61
63
|
user = User.find(1)
|
62
|
-
user.as_json
|
64
|
+
user.as_json(only: [:id, :name, posts: [:id, :title]])
|
63
65
|
```
|
64
66
|
|
65
67
|
## Installation
|
@@ -189,6 +191,12 @@ You can pass a scope to the association with the `scope` option. It can either b
|
|
189
191
|
many :posts, scope: -> { where(published: true).limit(4) }
|
190
192
|
```
|
191
193
|
|
194
|
+
You can also pass a context to the lambda. See the [context section](#context) for more details.
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
many :posts, scope: -> (context) { where(language: context.language) }
|
198
|
+
```
|
199
|
+
|
192
200
|
##### Key name
|
193
201
|
You can also pass a key name for the association with the `key_name` option.
|
194
202
|
|
@@ -251,6 +259,12 @@ class PostSerializer < Barley::Serializer
|
|
251
259
|
end
|
252
260
|
```
|
253
261
|
|
262
|
+
The context is also available in the scope of the lambda passed to the `scope` option of the `many` macro. See the [scope section](#scope) for more details.
|
263
|
+
|
264
|
+
```ruby
|
265
|
+
many :posts, scope: -> (context) { where(language: context.language) }
|
266
|
+
```
|
267
|
+
|
254
268
|
### Using a custom context object
|
255
269
|
Barley generates a Struct from the context hash you pass to the with_context method. But you can also pass a custom context object directly in the initializer instead.
|
256
270
|
|
@@ -464,8 +478,3 @@ Make sure you adhere to [our code of conduct](CODE_OF_CONDUCT.md). We aim to kee
|
|
464
478
|
## Security
|
465
479
|
|
466
480
|
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/serializable.rb
CHANGED
@@ -65,7 +65,7 @@ module Barley
|
|
65
65
|
cache = options[:cache] || false
|
66
66
|
root = options[:root] || false
|
67
67
|
begin
|
68
|
-
serializer.new(self, cache: cache, root: root).serializable_hash
|
68
|
+
serializer.new(self, cache: cache, root: root, only: options[:only], except: options[:except]).serializable_hash
|
69
69
|
rescue NameError
|
70
70
|
raise Barley::Error, "Could not find serializer for #{self}. Please define a #{serializer} class."
|
71
71
|
end
|
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
|
@@ -128,7 +135,9 @@ module Barley
|
|
128
135
|
return {} if element.nil?
|
129
136
|
|
130
137
|
el_serializer = serializer || element.serializer.class
|
131
|
-
|
138
|
+
only = @only.find { |k| k.is_a?(Hash) && k.key?(key) }.slice(key).values.first if @only.present?
|
139
|
+
except = @except.find { |k| k.is_a?(Hash) && k.key?(key) }.slice(key).values.first if @except.present?
|
140
|
+
el_serializer.new(element, cache: cache, context: @context, only: only, except: except).serializable_hash
|
132
141
|
end
|
133
142
|
self.defined_attributes = (defined_attributes || []) << key_name
|
134
143
|
end
|
@@ -186,12 +195,18 @@ module Barley
|
|
186
195
|
if scope.is_a?(Symbol)
|
187
196
|
elements = elements.send(scope)
|
188
197
|
elsif scope.is_a?(Proc)
|
189
|
-
elements =
|
198
|
+
elements = if scope.arity == 1
|
199
|
+
elements.instance_exec(@context, &scope)
|
200
|
+
else
|
201
|
+
elements.instance_exec(&scope)
|
202
|
+
end
|
190
203
|
end
|
191
204
|
return [] if elements.empty?
|
192
205
|
|
193
206
|
el_serializer = serializer || elements.first.serializer.class
|
194
|
-
|
207
|
+
only = @only.find { |k| k.is_a?(Hash) && k.key?(key) }.slice(key).values.first if @only.present?
|
208
|
+
except = @except.find { |k| k.is_a?(Hash) && k.key?(key) }.slice(key).values.first if @except.present?
|
209
|
+
elements.map { |element| el_serializer.new(element, cache: cache, context: @context, only: only, except: except).serializable_hash }.reject(&:blank?)
|
195
210
|
end
|
196
211
|
self.defined_attributes = (defined_attributes || []) << key_name
|
197
212
|
end
|
@@ -207,10 +222,14 @@ module Barley
|
|
207
222
|
# @param cache [Boolean, Hash<Symbol, ActiveSupport::Duration>] a boolean to cache the result, or a hash with options for the cache
|
208
223
|
# @param root [Boolean] whether to include the root key in the hash
|
209
224
|
# @param context [Object] an optional context object to pass additional data to the serializer
|
210
|
-
|
225
|
+
# @param only [Array<Symbol>] an array of attributes to include
|
226
|
+
# @param except [Array<Symbol>] an array of attributes to exclude
|
227
|
+
def initialize(object, cache: false, root: false, context: nil, only: nil, except: nil)
|
211
228
|
@object = object
|
212
229
|
@context = context
|
213
230
|
@root = root
|
231
|
+
@only = only
|
232
|
+
@except = except
|
214
233
|
@cache, @expires_in = if cache.is_a?(Hash)
|
215
234
|
[true, cache[:expires_in]]
|
216
235
|
else
|
@@ -222,13 +241,22 @@ module Barley
|
|
222
241
|
#
|
223
242
|
# @return [Hash] the serializable hash
|
224
243
|
def serializable_hash
|
225
|
-
if @cache
|
244
|
+
hash = if @cache
|
226
245
|
Barley::Cache.fetch(cache_base_key, expires_in: @expires_in) do
|
227
246
|
_serializable_hash
|
228
247
|
end
|
229
248
|
else
|
230
249
|
_serializable_hash
|
231
250
|
end
|
251
|
+
if @only.present?
|
252
|
+
only = @only.map { |k| k.is_a?(Hash) ? k.keys.first : k }
|
253
|
+
hash.slice!(*only)
|
254
|
+
end
|
255
|
+
if @except.present?
|
256
|
+
except = @except.reject { |k| k.is_a?(Hash) }
|
257
|
+
hash.except!(*except)
|
258
|
+
end
|
259
|
+
hash
|
232
260
|
end
|
233
261
|
|
234
262
|
# Clears the cache for the object
|
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: 0.
|
4
|
+
version: '0.7'
|
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-
|
11
|
+
date: 2024-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|