barley 0.6.2 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 802ad211955dc1b4f0579ff2f88ac1e88a56eb2425317ae46b1cda590cfc1f26
4
- data.tar.gz: 5f5e309f03e3422f9b2bd7e7b7319994c5c63d3367043e6f6c19328784e73c01
3
+ metadata.gz: f6246aafe191aa99a01d5bcda56530dda4be81aa4b83a4626a138074b857524b
4
+ data.tar.gz: 483efb36394fcb402461336e82ba348d7960d59911885e0239f5e28a9a255716
5
5
  SHA512:
6
- metadata.gz: 3189c87d7fe0d3cdb96a6c503a56f0469a3f97cd965feeafb0337e351cdd8199990b63ac1d119381ef3058914ed07b41f81fc9b0d1789059ae3d944171731226
7
- data.tar.gz: 000bdb7632ea4ab4c1d015fdae029220dc35cbae03ddd1999cf016f21e1c08f46699a7ad610fcc816abbb21e014408c2b838e37a3ef036cb3806b373981f15ab
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
 
@@ -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
@@ -135,7 +135,9 @@ module Barley
135
135
  return {} if element.nil?
136
136
 
137
137
  el_serializer = serializer || element.serializer.class
138
- 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
139
141
  end
140
142
  self.defined_attributes = (defined_attributes || []) << key_name
141
143
  end
@@ -193,12 +195,18 @@ module Barley
193
195
  if scope.is_a?(Symbol)
194
196
  elements = elements.send(scope)
195
197
  elsif scope.is_a?(Proc)
196
- 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
197
203
  end
198
204
  return [] if elements.empty?
199
205
 
200
206
  el_serializer = serializer || elements.first.serializer.class
201
- 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?)
202
210
  end
203
211
  self.defined_attributes = (defined_attributes || []) << key_name
204
212
  end
@@ -214,10 +222,14 @@ module Barley
214
222
  # @param cache [Boolean, Hash<Symbol, ActiveSupport::Duration>] a boolean to cache the result, or a hash with options for the cache
215
223
  # @param root [Boolean] whether to include the root key in the hash
216
224
  # @param context [Object] an optional context object to pass additional data to the serializer
217
- 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)
218
228
  @object = object
219
229
  @context = context
220
230
  @root = root
231
+ @only = only
232
+ @except = except
221
233
  @cache, @expires_in = if cache.is_a?(Hash)
222
234
  [true, cache[:expires_in]]
223
235
  else
@@ -229,13 +241,22 @@ module Barley
229
241
  #
230
242
  # @return [Hash] the serializable hash
231
243
  def serializable_hash
232
- if @cache
244
+ hash = if @cache
233
245
  Barley::Cache.fetch(cache_base_key, expires_in: @expires_in) do
234
246
  _serializable_hash
235
247
  end
236
248
  else
237
249
  _serializable_hash
238
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
239
260
  end
240
261
 
241
262
  # Clears the cache for the object
@@ -1,3 +1,3 @@
1
1
  module Barley
2
- VERSION = "0.6.2"
2
+ VERSION = "0.7"
3
3
  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.2
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-17 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