barley 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -7
- data/lib/barley/serializable.rb +2 -2
- data/lib/barley/serializer.rb +19 -11
- data/lib/barley/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: be82895a01fc1f0be3170a9a105b7fdf20b7e98d31a77c74c5d9f67068814dec
|
4
|
+
data.tar.gz: 276dcc80814079aed898b1e4d1d9e5b6bf3bc4b8b6b86f2b5cff701961d2b877
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f3482dc0562669991a64403c9c5cee510d61cc5ec79affa3715eb203ac1a48f1f15c676778d380eccbfe6efc57281eae6c179ab4378a62d35c31b0fd65529ea
|
7
|
+
data.tar.gz: b9d2a98edf8704acf8b8466042b924b771384e1756fd06f9d23939f9bd0fb4368a4934303057edd0a53d0c8f487dc93c10649e39fe17b3031189f383a668ec54
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Barley is a dead simple, fast, and efficient ActiveModel JSON serializer.
|
4
4
|
|
5
|
-
Cerealize your ActiveModel objects into flat
|
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.
|
6
6
|
|
7
7
|
You don't believe us? Check out the [benchmarks](#benchmarks). 😎
|
8
8
|
|
@@ -21,10 +21,19 @@ Then define your attributes and associations in a serializer class.
|
|
21
21
|
```ruby
|
22
22
|
# /app/serializers/user_serializer.rb
|
23
23
|
class UserSerializer < Barley::Serializer
|
24
|
-
attributes :id, :name,
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
attributes :id, :name,
|
25
|
+
attribute :email # single attribute
|
26
|
+
|
27
|
+
many :posts # relations
|
28
|
+
one :group, serializer: CustomGroupSerializer # custom serializer
|
29
|
+
many :related_users, key: :friends, cache: true # custom key, and caching
|
30
|
+
one :profile, cache: { expires_in: 1.day } do # cache definition, and block (on associations) for nested, on-the-fly serializer
|
31
|
+
attributes :avatar, :social_url
|
32
|
+
attribute :badges do
|
33
|
+
object.badges.map(&:display_name) # use object in a block to return custom code
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
28
37
|
end
|
29
38
|
```
|
30
39
|
|
@@ -264,14 +273,14 @@ Ah ah ah. This is so funny.
|
|
264
273
|
*Note: we are thinking about adding a `Surrealizer` class for the most advanced users. Stay tuned.*
|
265
274
|
|
266
275
|
## JSON:API
|
267
|
-
|
276
|
+
Barley does not serialize to the JSON:API standard. We prefer to keep it simple and fast.
|
268
277
|
|
269
278
|
## Benchmarks
|
270
279
|
This gem is blazing fast and efficient. It is 2 to 3 times faster than [ActiveModel::Serializer](https://github.com/rails-api/active_model_serializers) and twice as fast as [FastJsonapi](https://github.com/Netflix/fast_jsonapi). Memory object allocation is also much lower.
|
271
280
|
|
272
281
|
With caching enabled, it is just mind-blowing. We think. *Disclaimer: we do not serialize to the JSON:API standard, so that might be the reason why we are so fast.*
|
273
282
|
|
274
|
-
This is the result we get with the benchmark script used in the AMS repo on an Apple Silicon M1Pro processor.
|
283
|
+
This is the result we get with the benchmark script used in the AMS repo on an Apple Silicon M1Pro processor. [Check it out for yourself here](https://github.com/MoskitoHero/active_model_serializers/tree/benchmarks).
|
275
284
|
|
276
285
|
```shell
|
277
286
|
bundle exec ruby benchmark.rb
|
@@ -351,3 +360,8 @@ ams : 1299674 allocated - 28.20x more
|
|
351
360
|
|
352
361
|
## License
|
353
362
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
363
|
+
|
364
|
+
## Credits
|
365
|
+
Barley is brought to you by the developer team from [StockPro](https://www.stock-pro.fr/).
|
366
|
+
|
367
|
+
[![Barley is brought to you by StockPro](img/stockpro.png)](https://www.stock-pro.fr/)
|
data/lib/barley/serializable.rb
CHANGED
@@ -33,9 +33,9 @@ module Barley
|
|
33
33
|
included do
|
34
34
|
serializer "#{self}Serializer".constantize
|
35
35
|
|
36
|
-
def as_json(serializer: nil, cache: false)
|
36
|
+
def as_json(serializer: nil, cache: false, root: false)
|
37
37
|
serializer ||= self.serializer.class
|
38
|
-
serializer.new(self, cache: cache).
|
38
|
+
serializer.new(self, cache: cache, root: root).serializable_hash
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
data/lib/barley/serializer.rb
CHANGED
@@ -40,7 +40,7 @@ module Barley
|
|
40
40
|
return {} if element.nil?
|
41
41
|
|
42
42
|
el_serializer = serializer || element.serializer.class
|
43
|
-
el_serializer.new(element, cache: cache).
|
43
|
+
el_serializer.new(element, cache: cache).serializable_hash
|
44
44
|
end
|
45
45
|
set_class_iv(:@defined_attributes, key_name)
|
46
46
|
end
|
@@ -57,7 +57,7 @@ module Barley
|
|
57
57
|
return [] if elements.empty?
|
58
58
|
|
59
59
|
el_serializer = serializer || elements.first.serializer.class
|
60
|
-
elements.map { |element| el_serializer.new(element, cache: cache).
|
60
|
+
elements.map { |element| el_serializer.new(element, cache: cache).serializable_hash }.reject(&:blank?)
|
61
61
|
end
|
62
62
|
set_class_iv(:@defined_attributes, key_name)
|
63
63
|
end
|
@@ -71,8 +71,9 @@ module Barley
|
|
71
71
|
# Barley::Serializer.new(object, cache: true)
|
72
72
|
# @example with cache and expires_in
|
73
73
|
# Barley::Serializer.new(object, cache: {expires_in: 1.hour})
|
74
|
-
def initialize(object, cache: false)
|
74
|
+
def initialize(object, cache: false, root: false)
|
75
75
|
@object = object
|
76
|
+
@root = root
|
76
77
|
@cache, @expires_in = if cache.is_a?(Hash)
|
77
78
|
[true, cache[:expires_in]]
|
78
79
|
else
|
@@ -80,14 +81,13 @@ module Barley
|
|
80
81
|
end
|
81
82
|
end
|
82
83
|
|
83
|
-
def
|
84
|
+
def serializable_hash
|
84
85
|
if @cache
|
85
|
-
|
86
|
-
|
87
|
-
_as_json
|
86
|
+
Barley::Cache.fetch(cache_base_key, expires_in: @expires_in) do
|
87
|
+
_serializable_hash
|
88
88
|
end
|
89
89
|
else
|
90
|
-
|
90
|
+
_serializable_hash
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
@@ -98,21 +98,29 @@ module Barley
|
|
98
98
|
private
|
99
99
|
|
100
100
|
def cache_base_key
|
101
|
-
|
101
|
+
if object.updated_at.present?
|
102
|
+
"#{object.class.name&.underscore}/#{object.id}/#{object.updated_at&.to_i}/barley_cache/"
|
103
|
+
else
|
104
|
+
"#{object.class.name&.underscore}/#{object.id}/barley_cache/"
|
105
|
+
end
|
102
106
|
end
|
103
107
|
|
104
108
|
def defined_attributes
|
105
109
|
self.class.instance_variable_get(:@defined_attributes)
|
106
110
|
end
|
107
111
|
|
108
|
-
def
|
112
|
+
def _serializable_hash
|
109
113
|
hash = {}
|
110
114
|
|
111
115
|
defined_attributes.each do |key|
|
112
116
|
hash[key] = send(key)
|
113
117
|
end
|
114
118
|
|
115
|
-
hash
|
119
|
+
@root ? {root_key => hash} : hash
|
120
|
+
end
|
121
|
+
|
122
|
+
def root_key
|
123
|
+
object.class.name.underscore.to_sym
|
116
124
|
end
|
117
125
|
end
|
118
126
|
end
|
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
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cedric Delalande
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-10-
|
11
|
+
date: 2023-10-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|