userializer 0.1.3 → 0.1.4
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/Gemfile.lock +1 -1
- data/README.md +146 -1
- data/lib/userializer/has_many.rb +4 -1
- data/lib/userializer/has_one.rb +2 -2
- data/lib/userializer/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: ffef1e179dd3b2b19e6a55f24edc24c691a0885aa6bcc0585e5600296d6697f8
|
4
|
+
data.tar.gz: 9f6bfe4b0203808dc37ba579ade7386b0025d6593721fb1a330298e79ee76f75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f3e01d2b9dbd0324c21d8917b4e1f4bf2909ea73b7f6233ccbff2b3b61e35849b398dbed0bb3b82e140ce38934905c62213b492ee21856b2cc4551996035fad
|
7
|
+
data.tar.gz: 587444031cec4029296277202efa952b3785b9d3127908205fe0237d115b7fc0ecbd80b6f56533f282d99ec0b9f98bdca3012974377355dd0a9b0a1da6882e4a
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -20,7 +20,152 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
23
|
+
USerializer's DSL is relatively close to Active Model Serializer's,
|
24
|
+
while having a few additional features including:
|
25
|
+
* Attributes Conditional Declaration
|
26
|
+
* Attributes Inline Definition
|
27
|
+
|
28
|
+
### Attributes Conditional Declaration
|
29
|
+
|
30
|
+
USerializer allows you to dynamically decide wether an attribute should
|
31
|
+
be serialized or not by passing its definition an `if` block as follows:
|
32
|
+
```ruby
|
33
|
+
attributes :conditional_attr, if: proc { |_, opts| ... }
|
34
|
+
```
|
35
|
+
|
36
|
+
Eg: Let's say you want to serialize an `Order` object but want to
|
37
|
+
include its `price` only if it's superior to *10*, your serializer
|
38
|
+
would look like the following:
|
39
|
+
```ruby
|
40
|
+
class Order < ActiveRecord::Base
|
41
|
+
def price
|
42
|
+
10
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class OrderSerializer < USerializer::BaseSerializer
|
47
|
+
attributes :price, if: proc do |obj, _|
|
48
|
+
obj.price > 10
|
49
|
+
end
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
In that case for example, the `price` attribute would be omitted from
|
54
|
+
the final response.
|
55
|
+
|
56
|
+
### Attributes Inline Definition
|
57
|
+
|
58
|
+
Using AMS, the only way to rewrite an attribute prior to serialization
|
59
|
+
is to override it using a method with the same name, leading to
|
60
|
+
something like this:
|
61
|
+
```ruby
|
62
|
+
class MyObject < ActiveRecord::Base
|
63
|
+
def random_attr
|
64
|
+
0
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
class MyObjectSerializer < ActiveModel::Serializer
|
69
|
+
attributes :random_attr
|
70
|
+
|
71
|
+
def random_attr
|
72
|
+
object.random_attr + 1
|
73
|
+
end
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
While this code works perfectly, it pushes the serialized attribute
|
78
|
+
value definition back from its declaration, causing developers to lose
|
79
|
+
focus when listing their serialized attributes because the overriding is
|
80
|
+
done farther.
|
81
|
+
|
82
|
+
With USerializer, all of this is done in an inline way, so that you can
|
83
|
+
override the attribute's value while declaring using a block as
|
84
|
+
follows:
|
85
|
+
```ruby
|
86
|
+
attributes :your_attribute do |object, _|
|
87
|
+
...
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
Our `random_attr` serialization would then looks like this with
|
92
|
+
USerializer:
|
93
|
+
```ruby
|
94
|
+
class MyObjectSerializer < USerializer::BaseSerializer
|
95
|
+
attributes :random_attr do |object, _|
|
96
|
+
object.random_attr + 1
|
97
|
+
end
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
Way nicer, right?
|
102
|
+
|
103
|
+
### Relationships
|
104
|
+
|
105
|
+
Just like AMS, USerializer supports `has_one` and `has_many`
|
106
|
+
relationships
|
107
|
+
|
108
|
+
### Serialized Output
|
109
|
+
|
110
|
+
The following outputs will be based an on our `Order` object in
|
111
|
+
different situations:
|
112
|
+
|
113
|
+
* Order is serialized without any relationships:
|
114
|
+
```json
|
115
|
+
{
|
116
|
+
"order": {
|
117
|
+
"id": 1,
|
118
|
+
"attr_1": "value_1",
|
119
|
+
"attr_2": "value_2",
|
120
|
+
"attr_3": "value_3",
|
121
|
+
}
|
122
|
+
}
|
123
|
+
```
|
124
|
+
|
125
|
+
* Order has a `has_one` relationship with a `Client` model
|
126
|
+
```json
|
127
|
+
{
|
128
|
+
"clients": [
|
129
|
+
{
|
130
|
+
"id": 4,
|
131
|
+
"name": "userializer client",
|
132
|
+
...
|
133
|
+
}
|
134
|
+
],
|
135
|
+
"order": {
|
136
|
+
"id": 1,
|
137
|
+
"attr_1": "value_1",
|
138
|
+
"attr_2": "value_2",
|
139
|
+
"attr_3": "value_3",
|
140
|
+
"client_id": 4
|
141
|
+
}
|
142
|
+
}
|
143
|
+
```
|
144
|
+
|
145
|
+
* Order has a `has_many` relationship with an `Article` model
|
146
|
+
```json
|
147
|
+
{
|
148
|
+
"articles": [
|
149
|
+
{
|
150
|
+
"id": 1,
|
151
|
+
"name": "Article #1",
|
152
|
+
...
|
153
|
+
},
|
154
|
+
{
|
155
|
+
"id": 1,
|
156
|
+
"name": "Article #2",
|
157
|
+
...
|
158
|
+
}
|
159
|
+
],
|
160
|
+
"order": {
|
161
|
+
"id": 1,
|
162
|
+
"attr_1": "value_1",
|
163
|
+
"attr_2": "value_2",
|
164
|
+
"attr_3": "value_3",
|
165
|
+
"article_ids": [1, 2]
|
166
|
+
}
|
167
|
+
}
|
168
|
+
```
|
24
169
|
|
25
170
|
## Development
|
26
171
|
|
data/lib/userializer/has_many.rb
CHANGED
@@ -8,13 +8,16 @@ module USerializer
|
|
8
8
|
@opts = opts
|
9
9
|
@id_key = "#{ActiveSupport::Inflector.singularize(key)}_ids".to_sym
|
10
10
|
|
11
|
+
@embed_key = opts[:embed_key] || :id
|
11
12
|
@conditional_block = opts[:if] || proc { true }
|
12
13
|
end
|
13
14
|
|
14
15
|
def merge_attributes(res, ser, opts)
|
15
16
|
return unless @conditional_block.call(ser.object, opts)
|
16
17
|
|
17
|
-
res[@id_key] = (ser.send(@key) || []).compact.map
|
18
|
+
res[@id_key] = (ser.send(@key) || []).compact.map do |obj|
|
19
|
+
obj.nil? ? nil : obj.send(@embed_key)
|
20
|
+
end.compact
|
18
21
|
end
|
19
22
|
|
20
23
|
def merge_root(res, ser, opts)
|
data/lib/userializer/has_one.rb
CHANGED
@@ -7,7 +7,7 @@ module USerializer
|
|
7
7
|
@root_key = opts[:root]&.to_sym
|
8
8
|
|
9
9
|
@serializer = opts[:serializer]
|
10
|
-
|
10
|
+
@embed_key = opts[:embed_key] || :id
|
11
11
|
@conditional_block = opts[:if] || proc { true }
|
12
12
|
end
|
13
13
|
|
@@ -17,7 +17,7 @@ module USerializer
|
|
17
17
|
return unless @conditional_block.call(ser.object, opts)
|
18
18
|
|
19
19
|
obj = ser.send(@key)
|
20
|
-
res[@id_key] = obj
|
20
|
+
res[@id_key] = obj.nil? ? nil : obj.send(@embed_key)
|
21
21
|
end
|
22
22
|
|
23
23
|
def merge_root(res, ser, opts)
|
data/lib/userializer/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: userializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexis Montagne
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|