barley 0.4.0 → 0.5

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: faa7d627d11906ae112f0a8fd640f0a2d90acacc114c63297a26e227d5120c3d
4
- data.tar.gz: c36d22bee59469e7e98f481086a5a32a75ac96d9bab984330a4d945c97efa5f6
3
+ metadata.gz: 4808cb10061c5a86be6edbf532a6fb041519e4b7b4a08b770e0d63854a5f61e1
4
+ data.tar.gz: 5ccb1ed50e9d440a0bb3e9c006280e970b70b5ecc64afb7b8660c0d326f58239
5
5
  SHA512:
6
- metadata.gz: 3adb8da65be384daf38feaf056541f6b3e2c2f7f400917ed2da43a9f382e3bbc28afdad2437fe7489bdcd1ccd3980a587697525f0b493e1ec7b5374acfb61bb8
7
- data.tar.gz: a359ed1a3c15f8efdd62ec86112163edc8d423f9d0d8c8790e781c651ac208dfa722a42bb4c93f822b5bbfc2a072626bbf1c9ad088c24354828745476247fc58
6
+ metadata.gz: 4562ae18d898be75d1abf2d2872bd5f79333f38e6a3e111548624ba6e4b19f4f7316087e36ff35f9103cac56a6b96e9d2f1fb32d1cfb1596430e1b81a1f68c2a
7
+ data.tar.gz: 13c86fbcb1d2678165bd128ea0e57ca80e1e4d63e3a3ccee09f9ac85838bab7cd0894d75cc832c7a513ac29fd697573039b8ee5a1621be1152e2a22c6525b5ce
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
1
  ![Barley loqo](https://i.imgur.com/am0emi4.png)
2
2
 
3
- Barley is a dead simple, fast, and efficient ActiveModel JSON serializer.
3
+ ![Test suite badge](https://github.com/MoskitoHero/barley/actions/workflows/ruby.yml/badge.svg)
4
+ [![Gem Version](https://badge.fury.io/rb/barley.svg)](https://badge.fury.io/rb/barley)
5
+ ![Static Badge](https://img.shields.io/badge/Cereal%20-%20100%25%20-%20darklime)
4
6
 
5
- Cerealize your ActiveModel objects into flat hashes with a dead simple, yet versatile DSL, and caching baked in. Our daily bread is to make your API faster.
7
+ Barley is a dead simple, fast, and efficient ActiveModel serializer.
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.
6
10
 
7
11
  You don't believe us? Check out the [benchmarks](#benchmarks). 😎
8
12
 
@@ -24,24 +28,30 @@ Then define your attributes and associations in a serializer class.
24
28
  ```ruby
25
29
  # /app/serializers/user_serializer.rb
26
30
  class UserSerializer < Barley::Serializer
27
- attributes id: Types::Strict::Integer, :name # multiple attributes, optional type checking with dry-types
28
- attribute :email # single attribute
29
- attribute :value, type: Types::Coercible::Integer # optional type checking with dry-types
30
-
31
- many :posts # relations
32
- one :group, serializer: CustomGroupSerializer # custom serializer
33
- many :related_users, key: :friends, cache: true # custom key, and caching
34
- one :profile, cache: { expires_in: 1.day } do # cache definition, and block (on associations) for nested, on-the-fly serializer
31
+
32
+ attributes id: Types::Strict::Integer, :name
33
+
34
+ attribute :email
35
+ attribute :value, type: Types::Coercible::Integer
36
+
37
+ many :posts
38
+
39
+ one :group, serializer: CustomGroupSerializer
40
+
41
+ many :related_users, key: :friends, cache: true
42
+
43
+ one :profile, cache: { expires_in: 1.day } do
35
44
  attributes :avatar, :social_url
45
+
36
46
  attribute :badges do
37
- object.badges.map(&:display_name) # use object in a block to return custom code
47
+ object.badges.map(&:display_name)
38
48
  end
39
49
  end
40
50
 
41
51
  end
42
52
  ```
43
53
 
44
- The just use the `as_json` method on your model.
54
+ Then just use the `as_json` method on your model.
45
55
 
46
56
  ```ruby
47
57
  user = User.find(1)
@@ -190,6 +200,25 @@ Feel like using a block to define your associations? You can do that too.
190
200
  end
191
201
  ```
192
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
+
193
222
  ## Generators
194
223
  You have two generators available. One to generate the serializer class:
195
224
 
@@ -386,6 +415,15 @@ ams : 1299674 allocated - 28.20x more
386
415
  ## License
387
416
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
388
417
 
418
+ ## Contributing
419
+ You can contribute in several ways: reporting bugs, suggesting features, or contributing code. See [our contributing guidelines](CONTRIBUTING.md)
420
+
421
+ Make sure you adhere to [our code of conduct](CODE_OF_CONDUCT.md). We aim to keep this project open and inclusive.
422
+
423
+ ## Security
424
+
425
+ Please refer to our [security guidelines](SECURITY.md)
426
+
389
427
  ## Credits
390
428
  Barley is brought to you by the developer team from [StockPro](https://www.stock-pro.fr/).
391
429
 
data/Rakefile CHANGED
@@ -1,3 +1,12 @@
1
1
  require "bundler/setup"
2
2
 
3
3
  require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ t.libs << "test"
9
+ end
10
+ desc "Run tests"
11
+
12
+ task default: :test
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Barley
4
+ class Error < StandardError
5
+ end
6
+ end
@@ -30,28 +30,45 @@ module Barley
30
30
  # @param klass [Class] the serializer class
31
31
  # @param cache [Boolean, Hash<Symbol, ActiveSupport::Duration>] whether to cache the result, or a hash with options for the cache
32
32
  def serializer(klass, cache: false)
33
- define_method(:serializer) do
34
- klass.new(self, cache: cache)
33
+ # We need to silence the warnings because we are defining a method with the same name as the parameter
34
+ # This avoids :
35
+ # - warning: method redefined; discarding old serializer
36
+ # - warning: previous definition of serializer was here
37
+ Kernel.silence_warnings do
38
+ define_method(:serializer) do
39
+ klass.new(self, cache: cache)
40
+ end
35
41
  end
36
42
  end
37
43
  end
38
44
 
39
45
  included do
40
- serializer "#{self}Serializer".constantize
46
+ begin
47
+ serializer "#{self}Serializer".constantize
48
+ rescue NameError
49
+ raise Barley::Error, "Could not find serializer for #{self}. Please define a #{self}Serializer class."
50
+ end
41
51
 
42
52
  # Serializes the model
43
53
  #
44
54
  # @note this method does not provide default rails options like `only` or `except`.
45
55
  # This is because the Barley serializer should be the only place where the attributes are defined.
46
56
  #
47
- # @param serializer [Class] the serializer to use
48
- # @param cache [Boolean, Hash<Symbol, ActiveSupport::Duration>] whether to cache the result, or a hash with options for the cache
49
- # @param root [Boolean] whether to include the root key in the hash
57
+ # @option options [Class] :serializer the serializer to use
58
+ # @option options [Boolean, Hash<Symbol, ActiveSupport::Duration>] :cache whether to cache the result, or a hash with options for the cache
59
+ # @option options [Boolean] :root whether to include the root key
50
60
  #
51
61
  # @return [Hash] the serialized attributes
52
- def as_json(serializer: nil, cache: false, root: false)
53
- serializer ||= self.serializer.class
54
- serializer.new(self, cache: cache, root: root).serializable_hash
62
+ def as_json(options = nil)
63
+ options ||= {}
64
+ serializer = options[:serializer] || self.serializer.class
65
+ cache = options[:cache] || false
66
+ root = options[:root] || false
67
+ begin
68
+ serializer.new(self, cache: cache, root: root).serializable_hash
69
+ rescue NameError
70
+ raise Barley::Error, "Could not find serializer for #{self}. Please define a #{serializer} class."
71
+ end
55
72
  end
56
73
  end
57
74
  end
@@ -3,8 +3,11 @@
3
3
  module Barley
4
4
  class Serializer
5
5
  attr_accessor :object
6
+ attr_accessor :context
6
7
 
7
8
  class << self
9
+ attr_accessor :defined_attributes
10
+
8
11
  # Defines attributes for the serializer
9
12
  #
10
13
  # Accepts either a list of symbols or a hash of symbols and Dry::Types, or a mix of both
@@ -72,16 +75,12 @@ module Barley
72
75
  # @param block [Proc] a block to use to compute the value
73
76
  def attribute(key, key_name: nil, type: nil, &block)
74
77
  key_name ||= key
75
- if block
76
- define_method(key_name) do
77
- type.nil? ? instance_eval(&block) : type[instance_eval(&block)]
78
- end
79
- else
80
- define_method(key_name) do
81
- type.nil? ? object.send(key) : type[object.send(key)]
82
- end
78
+ define_method(key_name) do
79
+ value = block ? instance_eval(&block) : object.send(key)
80
+ type.nil? ? value : type[value]
83
81
  end
84
- set_class_iv(:@defined_attributes, key_name)
82
+
83
+ self.defined_attributes = (defined_attributes || []) << key_name
85
84
  end
86
85
 
87
86
  # Defines a single association for the serializer
@@ -131,7 +130,7 @@ module Barley
131
130
  el_serializer = serializer || element.serializer.class
132
131
  el_serializer.new(element, cache: cache).serializable_hash
133
132
  end
134
- set_class_iv(:@defined_attributes, key_name)
133
+ self.defined_attributes = (defined_attributes || []) << key_name
135
134
  end
136
135
 
137
136
  # Defines a collection association for the serializer
@@ -181,17 +180,7 @@ module Barley
181
180
  el_serializer = serializer || elements.first.serializer.class
182
181
  elements.map { |element| el_serializer.new(element, cache: cache).serializable_hash }.reject(&:blank?)
183
182
  end
184
- set_class_iv(:@defined_attributes, key_name)
185
- end
186
-
187
- # Either sets or appends a key to an instance variable
188
- #
189
- # @api private
190
- #
191
- # @param iv [Symbol] the instance variable to set
192
- # @param key [Symbol] the key to add to the instance variable
193
- def set_class_iv(iv, key)
194
- instance_variable_defined?(iv) ? instance_variable_get(iv) << key : instance_variable_set(iv, [key])
183
+ self.defined_attributes = (defined_attributes || []) << key_name
195
184
  end
196
185
  end
197
186
 
@@ -236,6 +225,25 @@ module Barley
236
225
  Barley::Cache.delete(key)
237
226
  end
238
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
+
239
247
  private
240
248
 
241
249
  # @api private
@@ -253,21 +261,21 @@ module Barley
253
261
  #
254
262
  # @return [Array<Symbol>] the defined attributes
255
263
  def defined_attributes
256
- self.class.instance_variable_get(:@defined_attributes)
264
+ self.class.defined_attributes
257
265
  end
258
266
 
259
267
  # Serializes the object
260
268
  #
261
269
  # @api private
270
+ # @raise [Barley::Error] if no attribute or relation is defined in the serializer
262
271
  #
263
272
  # @return [Hash] the serializable hash
264
273
  def _serializable_hash
265
- hash = {}
274
+ raise Barley::Error, "No attribute or relation defined in #{self.class}" if defined_attributes.blank?
266
275
 
267
- defined_attributes.each do |key|
268
- hash[key] = send(key)
276
+ hash = defined_attributes.each_with_object({}) do |key, result|
277
+ result[key] = send(key)
269
278
  end
270
-
271
279
  @root ? {root_key => hash} : hash
272
280
  end
273
281
 
@@ -1,3 +1,3 @@
1
1
  module Barley
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5"
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.4.0
4
+ version: '0.5'
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: 2023-10-19 00:00:00.000000000 Z
11
+ date: 2024-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,8 +38,9 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 1.7.1
41
- description: Cerealize your ActiveModel objects into flat JSON objects with a dead
42
- simple DSL. Our daily bread is to make your API faster.
41
+ description: Cerealize your ActiveModel objects into flat hashes with a dead simple,
42
+ yet versatile DSL, and caching and type-checking baked in. Our daily bread is to
43
+ make your API faster.
43
44
  email:
44
45
  - weengs@moskitohero.com
45
46
  executables: []
@@ -52,6 +53,7 @@ files:
52
53
  - lib/barley.rb
53
54
  - lib/barley/cache.rb
54
55
  - lib/barley/configuration.rb
56
+ - lib/barley/error.rb
55
57
  - lib/barley/railtie.rb
56
58
  - lib/barley/serializable.rb
57
59
  - lib/barley/serializer.rb
@@ -74,7 +76,8 @@ metadata:
74
76
  homepage_uri: https://github.com/moskitohero/barley
75
77
  source_code_uri: https://github.com/moskitohero/barley
76
78
  changelog_uri: https://github.com/moskitohero/barley/CHANGELOG.md
77
- post_install_message:
79
+ documentation_uri: https://rubydoc.info/github/MoskitoHero/barley/main
80
+ post_install_message:
78
81
  rdoc_options: []
79
82
  require_paths:
80
83
  - lib
@@ -89,8 +92,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
92
  - !ruby/object:Gem::Version
90
93
  version: '0'
91
94
  requirements: []
92
- rubygems_version: 3.2.33
93
- signing_key:
95
+ rubygems_version: 3.3.26
96
+ signing_key:
94
97
  specification_version: 4
95
- summary: Barley is a dead simple, fast, and efficient ActiveModel JSON serializer.
98
+ summary: Barley is a dead simple, fast, and efficient ActiveModel serializer.
96
99
  test_files: []