barley 0.6.1 → 0.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 80e724f1d510a0774072dab90c06262278a06fd49adff8a77314852c2aafbe7f
4
- data.tar.gz: a13898b638153e90c95c25d0558c54e63b2514c6095be81c5755fb5d92dbb544
3
+ metadata.gz: f6246aafe191aa99a01d5bcda56530dda4be81aa4b83a4626a138074b857524b
4
+ data.tar.gz: 483efb36394fcb402461336e82ba348d7960d59911885e0239f5e28a9a255716
5
5
  SHA512:
6
- metadata.gz: da037e1c4749228e9fa48db4dcc00a8b614ebac1acc5ba0646c61fce0b77a377e530581b7536bc08b3111747ed6303ce60d3a275ac4c8cef2231a1ef726d873b
7
- data.tar.gz: 49b297c3db282da63110f28d9599157ac91f50c7fec42916e4a387c20ef0010ebfd3e4b4c53b03baa61985cdf17cdd7b79412eb388551d7c5a58d2c5f00b36b3
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
@@ -3,4 +3,7 @@
3
3
  module Barley
4
4
  class Error < StandardError
5
5
  end
6
+
7
+ class InvalidAttributeError < Error
8
+ end
6
9
  end
@@ -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
@@ -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? ? value : type[value]
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
- el_serializer.new(element, cache: cache, context: @context).serializable_hash
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 = elements.instance_exec(&scope)
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
- elements.map { |element| el_serializer.new(element, cache: cache, context: @context).serializable_hash }.reject(&:blank?)
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
- def initialize(object, cache: false, root: false, context: nil)
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
@@ -1,3 +1,3 @@
1
1
  module Barley
2
- VERSION = "0.6.1"
2
+ VERSION = "0.7"
3
3
  end
data/lib/barley.rb CHANGED
@@ -15,6 +15,7 @@ module Barley
15
15
 
16
16
  autoload :Cache, "barley/cache"
17
17
  autoload :Error, "barley/error"
18
+ autoload :InvalidAttributeError, "barley/error"
18
19
  autoload :Serializable, "barley/serializable"
19
20
  autoload :Serializer, "barley/serializer"
20
21
  end
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.6.1
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-06-14 00:00:00.000000000 Z
11
+ date: 2024-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails