barley 0.1.0 → 0.2.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 +40 -19
- data/lib/barley/serializer.rb +60 -45
- 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: bee5cfe197a0a824c4edd25ab6ad77628c14732bafc2ff09ceb64e08e24d6aaf
|
4
|
+
data.tar.gz: 5d060056d2de5fb5b8c64ca78b84a5faeb39fb4e7714e8de07647a26bfeeb76b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67d5c179e808312c73d5058e634c4ee2a7237d702b931580fd6a2c5e73085c51839a0e1debb8591f172cc696a8cbb9b694ccd7c740880d5fd384995a28011115
|
7
|
+
data.tar.gz: 5db28559be764eff3ef9990925896c6e1ce11005011335ab993f4fd346313cf0874bd22e7abdfc815616b95b0820c51648ecc22ca4243993b1d5dc9a4f200988
|
data/README.md
CHANGED
@@ -92,6 +92,12 @@ You can also define a custom attribute with a block. You will have a `object` va
|
|
92
92
|
end
|
93
93
|
```
|
94
94
|
|
95
|
+
You can also set a custom key name for the attribute with the `key_name` option.
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
attribute :updated_at, key: :last_change
|
99
|
+
```
|
100
|
+
|
95
101
|
### Associations
|
96
102
|
|
97
103
|
#### One-to-one
|
@@ -101,14 +107,6 @@ You can define a one-to-one association with the `one` macro.
|
|
101
107
|
one :group
|
102
108
|
```
|
103
109
|
|
104
|
-
You can also define a custom association with a block. You will have a `object` variable available in the block. It is the object you are serializing.
|
105
|
-
|
106
|
-
```ruby
|
107
|
-
one :group do
|
108
|
-
object.group.name
|
109
|
-
end
|
110
|
-
```
|
111
|
-
|
112
110
|
##### Custom serializer and caching
|
113
111
|
You can define a custom serializer for the association with the `serializer` option, and / or caching options with the `cache` option.
|
114
112
|
|
@@ -128,9 +126,15 @@ class UserSerializer < Barley::Serializer
|
|
128
126
|
attributes :id, :name
|
129
127
|
end
|
130
128
|
end
|
129
|
+
```
|
130
|
+
|
131
|
+
##### Key name
|
132
|
+
|
133
|
+
You can also pass a key name for the association with the `key_name` option.
|
131
134
|
|
135
|
+
```ruby
|
136
|
+
one :group, key_name: :team
|
132
137
|
```
|
133
|
-
end
|
134
138
|
|
135
139
|
#### One-to-many
|
136
140
|
You can define a one-to-many association with the `many` macro.
|
@@ -139,20 +143,38 @@ You can define a one-to-many association with the `many` macro.
|
|
139
143
|
many :posts
|
140
144
|
```
|
141
145
|
|
142
|
-
|
146
|
+
##### Custom serializer and caching
|
147
|
+
|
148
|
+
You can define a custom serializer for the association with the `serializer` option, and / or caching options with the `cache` option.
|
143
149
|
|
144
150
|
```ruby
|
145
|
-
many :posts
|
146
|
-
object.posts.map(&:title)
|
147
|
-
end
|
151
|
+
many :posts, serializer: CustomPostSerializer, cache: { expires_in: 1.hour }
|
148
152
|
```
|
149
153
|
|
150
|
-
#####
|
154
|
+
##### Key name
|
155
|
+
You can also pass a key name for the association with the `key_name` option.
|
151
156
|
|
152
|
-
|
157
|
+
```ruby
|
158
|
+
many :posts, key_name: :articles
|
159
|
+
```
|
160
|
+
|
161
|
+
### Associations with blocks
|
162
|
+
Feel like using a block to define your associations? You can do that too.
|
153
163
|
|
154
164
|
```ruby
|
155
|
-
|
165
|
+
one :group do
|
166
|
+
attributes :id, :name
|
167
|
+
end
|
168
|
+
```
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
many :posts do
|
172
|
+
attributes :id, :title, :body
|
173
|
+
|
174
|
+
one :author do
|
175
|
+
attributes :name, :email
|
176
|
+
end
|
177
|
+
end
|
156
178
|
```
|
157
179
|
|
158
180
|
## Generators
|
@@ -209,9 +231,6 @@ Barley uses the MemoryStore by default. You can change the cache store with the
|
|
209
231
|
# /config/initializers/barley.rb
|
210
232
|
Barley.configure do |config|
|
211
233
|
config.cache_store = ActiveSupport::Cache::RedisCacheStore.new
|
212
|
-
# config.cache_store = ActiveSupport::Cache::MemoryStore.new
|
213
|
-
# config.cache_store = ActiveSupport::Cache::FileStore.new
|
214
|
-
# config.cache_store = Rails.cache
|
215
234
|
end
|
216
235
|
```
|
217
236
|
|
@@ -242,6 +261,8 @@ rails generate barley:cerealizer User
|
|
242
261
|
|
243
262
|
Ah ah ah. This is so funny.
|
244
263
|
|
264
|
+
*Note: we are thinking about adding a `Surrealizer` class for the most advanced users. Stay tuned.*
|
265
|
+
|
245
266
|
## JSON:API
|
246
267
|
No. Not yet. Maybe never. We don't know. We don't care. We don't use it. We don't like it. We don't want to. We don't have time. We don't have money. We don't have a life. We don't have a girlfriend. We don't have a boyfriend. We don't have a dog. We don't have a cat. We are generating this readme with Copilot.
|
247
268
|
|
data/lib/barley/serializer.rb
CHANGED
@@ -4,65 +4,80 @@ module Barley
|
|
4
4
|
class Serializer
|
5
5
|
attr_accessor :object
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
else
|
16
|
-
[cache, nil]
|
7
|
+
class << self
|
8
|
+
def attributes(*keys)
|
9
|
+
keys.each do |key|
|
10
|
+
define_method(key) do
|
11
|
+
object.send(key)
|
12
|
+
end
|
13
|
+
set_class_iv(:@defined_attributes, key)
|
14
|
+
end
|
17
15
|
end
|
18
|
-
end
|
19
16
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
17
|
+
def attribute(key, key_name: nil, &block)
|
18
|
+
key_name ||= key
|
19
|
+
if block
|
20
|
+
define_method(key_name) do
|
21
|
+
instance_eval(&block)
|
22
|
+
end
|
23
|
+
else
|
24
|
+
define_method(key_name) do
|
25
|
+
object.send(key)
|
26
|
+
end
|
24
27
|
end
|
25
|
-
set_class_iv(:@defined_attributes,
|
28
|
+
set_class_iv(:@defined_attributes, key_name)
|
26
29
|
end
|
27
|
-
end
|
28
30
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
def one(key, key_name: nil, serializer: nil, cache: false, &block)
|
32
|
+
key_name ||= key
|
33
|
+
if block
|
34
|
+
serializer = Class.new(Barley::Serializer) do
|
35
|
+
instance_eval(&block)
|
36
|
+
end
|
33
37
|
end
|
34
|
-
|
35
|
-
|
36
|
-
|
38
|
+
define_method(key_name) do
|
39
|
+
element = object.send(key)
|
40
|
+
return {} if element.nil?
|
41
|
+
|
42
|
+
el_serializer = serializer || element.serializer.class
|
43
|
+
el_serializer.new(element, cache: cache).as_json
|
37
44
|
end
|
45
|
+
set_class_iv(:@defined_attributes, key_name)
|
38
46
|
end
|
39
|
-
set_class_iv(:@defined_attributes, key)
|
40
|
-
end
|
41
47
|
|
42
|
-
|
43
|
-
|
44
|
-
|
48
|
+
def many(key, key_name: nil, serializer: nil, cache: false, &block)
|
49
|
+
key_name ||= key
|
50
|
+
if block
|
51
|
+
serializer = Class.new(Barley::Serializer) do
|
52
|
+
instance_eval(&block)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
define_method(key_name) do
|
56
|
+
elements = object.send(key)
|
57
|
+
return [] if elements.empty?
|
45
58
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
59
|
+
el_serializer = serializer || elements.first.serializer.class
|
60
|
+
elements.map { |element| el_serializer.new(element, cache: cache).as_json }.reject(&:blank?)
|
61
|
+
end
|
62
|
+
set_class_iv(:@defined_attributes, key_name)
|
63
|
+
end
|
50
64
|
|
51
|
-
|
52
|
-
|
65
|
+
def set_class_iv(iv, key)
|
66
|
+
instance_variable_defined?(iv) ? instance_variable_get(iv) << key : instance_variable_set(iv, [key])
|
53
67
|
end
|
54
|
-
set_class_iv(:@defined_attributes, key)
|
55
68
|
end
|
56
69
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
70
|
+
# @example with cache
|
71
|
+
# Barley::Serializer.new(object, cache: true)
|
72
|
+
# @example with cache and expires_in
|
73
|
+
# Barley::Serializer.new(object, cache: {expires_in: 1.hour})
|
74
|
+
def initialize(object, cache: false)
|
75
|
+
@object = object
|
76
|
+
@cache, @expires_in = if cache.is_a?(Hash)
|
77
|
+
[true, cache[:expires_in]]
|
78
|
+
else
|
79
|
+
[cache, nil]
|
64
80
|
end
|
65
|
-
set_class_iv(:@defined_attributes, key)
|
66
81
|
end
|
67
82
|
|
68
83
|
def as_json
|
@@ -83,7 +98,7 @@ module Barley
|
|
83
98
|
private
|
84
99
|
|
85
100
|
def cache_base_key
|
86
|
-
"#{
|
101
|
+
"#{object.class.name&.underscore}/#{object.id}/#{object.updated_at.to_i}/as_json/"
|
87
102
|
end
|
88
103
|
|
89
104
|
def defined_attributes
|
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.2.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-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|