barley 0.4.0 → 0.4.1
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 +4 -4
- data/README.md +31 -12
- data/Rakefile +9 -0
- data/lib/barley/error.rb +6 -0
- data/lib/barley/serializable.rb +26 -9
- data/lib/barley/serializer.rb +10 -22
- data/lib/barley/version.rb +1 -1
- metadata +12 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d882b89bf1bf77259eda96813c4732ef1b286142ecb29fcb945837b743e089c9
|
4
|
+
data.tar.gz: 87dc3b213a614957a668c08865b87eb44f805cf76b79a60c710025a7d18257d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7eb159a6e40374364504f5c828d7e1f82e7a581bd89fe374182916b0dbc6863caaea779acf409e58c8fe60ff3614acdfa944bb3f74dbd808957925db146dded
|
7
|
+
data.tar.gz: d84b0157c80e83cfa28dbafda0fd300f2774c4362b5e1e47f9fc0381fb0cd8713dce2f1b27f4cfb9896af2c11d354d92b8df0dcfaee6a94bb69e6aca738e45b2
|
data/README.md
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|

|
2
2
|
|
3
|
-
|
3
|
+

|
4
|
+
[](https://badge.fury.io/rb/barley)
|
5
|
+

|
4
6
|
|
5
|
-
|
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
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
many :
|
34
|
-
|
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)
|
47
|
+
object.badges.map(&:display_name)
|
38
48
|
end
|
39
49
|
end
|
40
50
|
|
41
51
|
end
|
42
52
|
```
|
43
53
|
|
44
|
-
|
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
data/lib/barley/error.rb
ADDED
data/lib/barley/serializable.rb
CHANGED
@@ -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
|
-
|
34
|
-
|
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
|
-
|
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
|
-
# @
|
48
|
-
# @
|
49
|
-
# @
|
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(
|
53
|
-
|
54
|
-
serializer
|
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
|
data/lib/barley/serializer.rb
CHANGED
@@ -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
|
-
|
76
|
-
|
77
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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.
|
244
|
+
self.class.defined_attributes
|
257
245
|
end
|
258
246
|
|
259
247
|
# Serializes the object
|
data/lib/barley/version.rb
CHANGED
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.
|
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-
|
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
|
42
|
-
|
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
|
-
|
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.
|
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
|
98
|
+
summary: Barley is a dead simple, fast, and efficient ActiveModel serializer.
|
96
99
|
test_files: []
|