barley 0.4.0 → 0.4.1

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: d882b89bf1bf77259eda96813c4732ef1b286142ecb29fcb945837b743e089c9
4
+ data.tar.gz: 87dc3b213a614957a668c08865b87eb44f805cf76b79a60c710025a7d18257d8
5
5
  SHA512:
6
- metadata.gz: 3adb8da65be384daf38feaf056541f6b3e2c2f7f400917ed2da43a9f382e3bbc28afdad2437fe7489bdcd1ccd3980a587697525f0b493e1ec7b5374acfb61bb8
7
- data.tar.gz: a359ed1a3c15f8efdd62ec86112163edc8d423f9d0d8c8790e781c651ac208dfa722a42bb4c93f822b5bbfc2a072626bbf1c9ad088c24354828745476247fc58
6
+ metadata.gz: a7eb159a6e40374364504f5c828d7e1f82e7a581bd89fe374182916b0dbc6863caaea779acf409e58c8fe60ff3614acdfa944bb3f74dbd808957925db146dded
7
+ data.tar.gz: d84b0157c80e83cfa28dbafda0fd300f2774c4362b5e1e47f9fc0381fb0cd8713dce2f1b27f4cfb9896af2c11d354d92b8df0dcfaee6a94bb69e6aca738e45b2
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)
@@ -386,6 +396,15 @@ ams : 1299674 allocated - 28.20x more
386
396
  ## License
387
397
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
388
398
 
399
+ ## Contributing
400
+ You can contribute in several ways: reporting bugs, suggesting features, or contributing code. See [our contributing guidelines](CONTRIBUTING.md)
401
+
402
+ Make sure you adhere to [our code of conduct](CODE_OF_CONDUCT.md). We aim to keep this project open and inclusive.
403
+
404
+ ## Security
405
+
406
+ Please refer to our [security guidelines](SECURITY.md)
407
+
389
408
  ## Credits
390
409
  Barley is brought to you by the developer team from [StockPro](https://www.stock-pro.fr/).
391
410
 
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
@@ -5,6 +5,8 @@ module Barley
5
5
  attr_accessor :object
6
6
 
7
7
  class << self
8
+ attr_accessor :defined_attributes
9
+
8
10
  # Defines attributes for the serializer
9
11
  #
10
12
  # Accepts either a list of symbols or a hash of symbols and Dry::Types, or a mix of both
@@ -72,16 +74,12 @@ module Barley
72
74
  # @param block [Proc] a block to use to compute the value
73
75
  def attribute(key, key_name: nil, type: nil, &block)
74
76
  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
77
+ define_method(key_name) do
78
+ value = block ? instance_eval(&block) : object.send(key)
79
+ type.nil? ? value : type[value]
83
80
  end
84
- set_class_iv(:@defined_attributes, key_name)
81
+
82
+ self.defined_attributes = (defined_attributes || []) << key_name
85
83
  end
86
84
 
87
85
  # Defines a single association for the serializer
@@ -131,7 +129,7 @@ module Barley
131
129
  el_serializer = serializer || element.serializer.class
132
130
  el_serializer.new(element, cache: cache).serializable_hash
133
131
  end
134
- set_class_iv(:@defined_attributes, key_name)
132
+ self.defined_attributes = (defined_attributes || []) << key_name
135
133
  end
136
134
 
137
135
  # Defines a collection association for the serializer
@@ -181,17 +179,7 @@ module Barley
181
179
  el_serializer = serializer || elements.first.serializer.class
182
180
  elements.map { |element| el_serializer.new(element, cache: cache).serializable_hash }.reject(&:blank?)
183
181
  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])
182
+ self.defined_attributes = (defined_attributes || []) << key_name
195
183
  end
196
184
  end
197
185
 
@@ -253,7 +241,7 @@ module Barley
253
241
  #
254
242
  # @return [Array<Symbol>] the defined attributes
255
243
  def defined_attributes
256
- self.class.instance_variable_get(:@defined_attributes)
244
+ self.class.defined_attributes
257
245
  end
258
246
 
259
247
  # Serializes the object
@@ -1,3 +1,3 @@
1
1
  module Barley
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
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.4.1
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: 2023-10-27 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: []