barley 0.6.2 → 0.8

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: 802ad211955dc1b4f0579ff2f88ac1e88a56eb2425317ae46b1cda590cfc1f26
4
- data.tar.gz: 5f5e309f03e3422f9b2bd7e7b7319994c5c63d3367043e6f6c19328784e73c01
3
+ metadata.gz: 6da2835a5aa80d5ad8df847a595c104a92d1b0a34146f648368acfc95d98712f
4
+ data.tar.gz: 1c2b3c65b17e99f95d80f2842b2a7e99a73d93cdecd91b62943f1c9a6ff3e463
5
5
  SHA512:
6
- metadata.gz: 3189c87d7fe0d3cdb96a6c503a56f0469a3f97cd965feeafb0337e351cdd8199990b63ac1d119381ef3058914ed07b41f81fc9b0d1789059ae3d944171731226
7
- data.tar.gz: 000bdb7632ea4ab4c1d015fdae029220dc35cbae03ddd1999cf016f21e1c08f46699a7ad610fcc816abbb21e014408c2b838e37a3ef036cb3806b373981f15ab
6
+ metadata.gz: 5feba636b2205ce2b1be958e00d62e04509bec4d441405fcb669b76c455cd2209bddce0fc8ceedfa3a44f7251f22d1449b5315b6abcbf96cc0d85ac6924f903e
7
+ data.tar.gz: '08010326d833ba31944fddc5cf90c82db6a8002c0d8c2ae1f22dc2c137b1169b2f2841e58ec64daff403583ed08dacc4b2ad33dffa75a2ff5b270d69b67c1687'
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ![Barley loqo](https://i.imgur.com/am0emi4.png)
1
+ ![Barley loqo](https://i.imgur.com/cJJRA0i.png)
2
2
 
3
3
  ![Test suite badge](https://github.com/MoskitoHero/barley/actions/workflows/ruby.yml/badge.svg)
4
4
  [![Gem Version](https://badge.fury.io/rb/barley.svg)](https://badge.fury.io/rb/barley)
@@ -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 clear, 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
 
@@ -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
 
@@ -456,6 +470,8 @@ ams : 1299674 allocated - 28.20x more
456
470
  ## License
457
471
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
458
472
 
473
+ The logo is made from an asset from [onlinewebfonts.com](https://www.onlinewebfonts.com/icon), licensed by CC BY 4.0.
474
+
459
475
  ## Contributing
460
476
  You can contribute in several ways: reporting bugs, suggesting features, or contributing code. See [our contributing guidelines](CONTRIBUTING.md)
461
477
 
@@ -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.8'
3
3
  end
metadata CHANGED
@@ -1,43 +1,43 @@
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.8'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cedric Delalande
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-17 00:00:00.000000000 Z
11
+ date: 2025-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
14
+ name: dry-types
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 6.1.0
19
+ version: 1.7.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 6.1.0
26
+ version: 1.7.1
27
27
  - !ruby/object:Gem::Dependency
28
- name: dry-types
28
+ name: rails
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 1.7.1
33
+ version: 7.1.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 1.7.1
40
+ version: 7.1.0
41
41
  description: Cerealize your ActiveModel objects into flat hashes with a dead simple,
42
42
  yet versatile DSL, and caching and type-checking baked in. Our daily bread is to
43
43
  make your API faster.
@@ -77,7 +77,7 @@ metadata:
77
77
  source_code_uri: https://github.com/moskitohero/barley
78
78
  changelog_uri: https://github.com/moskitohero/barley/CHANGELOG.md
79
79
  documentation_uri: https://rubydoc.info/github/MoskitoHero/barley/main
80
- post_install_message:
80
+ post_install_message:
81
81
  rdoc_options: []
82
82
  require_paths:
83
83
  - lib
@@ -85,7 +85,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
85
  requirements:
86
86
  - - ">="
87
87
  - !ruby/object:Gem::Version
88
- version: '0'
88
+ version: '3.2'
89
89
  required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  requirements:
91
91
  - - ">="
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  version: '0'
94
94
  requirements: []
95
95
  rubygems_version: 3.3.27
96
- signing_key:
96
+ signing_key:
97
97
  specification_version: 4
98
98
  summary: Barley is a dead simple, fast, and efficient ActiveModel serializer.
99
99
  test_files: []