userializer 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 +27 -0
- data/lib/userializer.rb +1 -0
- data/lib/userializer/composite_serializer.rb +78 -0
- data/lib/userializer/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f3ef32ea4a17b463844f6c5f59453e206253c59c21735ea9aa6e97c353db2c3d
|
4
|
+
data.tar.gz: 77c4cdcba82ef5a5470da04db24e2984ae7754d8ba25593c0097470f3c0fdf65
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e48196e2d4264d655eb4da209e7635c56f4849eac2e977522379f358ccf7969cd6189bf3fe5675a721ef3ac9f2e89392099541189ef97f503a1ed1c0ea94f4f
|
7
|
+
data.tar.gz: d8a897a3e951c27488e700fb1f80385501367088219ab7b24505f1a7420b478f5171734f4641d319c9b8d9bf2a119407a8f2012c1bf4d5e6926709b33c99c7db
|
data/README.md
CHANGED
@@ -167,6 +167,33 @@ different situations:
|
|
167
167
|
}
|
168
168
|
```
|
169
169
|
|
170
|
+
### CompositeSerializer
|
171
|
+
|
172
|
+
Imagine you have a compound of different data that you want to return to the same payload.
|
173
|
+
For example, you have an **array** of a `Foo` class and a `Bar` value to return.
|
174
|
+
You can use a `CompositeSerializer` to serialize both.
|
175
|
+
|
176
|
+
```ruby
|
177
|
+
array_foo = [Foo.new, Foo.new]
|
178
|
+
bar = Bar.new
|
179
|
+
|
180
|
+
CompositeSerializer.new(
|
181
|
+
{ key_foo: array_foo, key_bar: bar },
|
182
|
+
each_serializer: { key_foo: FooCustomSerializer },
|
183
|
+
serializer: { key_bar: BarSerializer },
|
184
|
+
root: { key_foo: :foo_root, key_bar: :bar_root }
|
185
|
+
).to_json
|
186
|
+
```
|
187
|
+
|
188
|
+
this will render:
|
189
|
+
|
190
|
+
```json
|
191
|
+
{
|
192
|
+
"foo_root": [{... foo1 attributes ...}, {... foo2 attributes ...}],
|
193
|
+
"bar_root": {... bar attributes ...}
|
194
|
+
}
|
195
|
+
```
|
196
|
+
|
170
197
|
## Development
|
171
198
|
|
172
199
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/userializer.rb
CHANGED
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'oj'
|
2
|
+
|
3
|
+
module USerializer
|
4
|
+
class CompositeObject
|
5
|
+
def initialize(obj, opts = {})
|
6
|
+
@obj = obj
|
7
|
+
@opts = opts
|
8
|
+
@root_key = opts[:root].to_sym
|
9
|
+
serializer = opts[:serializer]
|
10
|
+
@serializer = if serializer.is_a?(Proc)
|
11
|
+
serializer
|
12
|
+
elsif serializer
|
13
|
+
proc { serializer }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def merge_root(res, opts)
|
18
|
+
serializer(@obj, opts).merge_root(res, @root_key, true, opts)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def serializer(obj, opts)
|
24
|
+
return @serializer.call(obj, opts).new(obj, @opts) if @serializer
|
25
|
+
return obj.serialize if obj.respond_to?(:serialize)
|
26
|
+
|
27
|
+
USerializer.infered_serializer_class(obj.class).new(obj, @opts)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class CompositeSerializer
|
32
|
+
def initialize(objs, opts = {})
|
33
|
+
@opts = opts
|
34
|
+
@objs = compose_objs(objs)
|
35
|
+
end
|
36
|
+
|
37
|
+
def merge_root(res, opts)
|
38
|
+
@objs.each do |obj|
|
39
|
+
obj.merge_root(res, opts)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def to_hash
|
44
|
+
res = {}
|
45
|
+
|
46
|
+
merge_root(res, @opts)
|
47
|
+
res
|
48
|
+
end
|
49
|
+
|
50
|
+
def serialize(*_args)
|
51
|
+
to_hash
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_json(*_args)
|
55
|
+
Oj.dump(to_hash, mode: :compat)
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def compose_objs(objs)
|
61
|
+
objs.map do |(key, obj)|
|
62
|
+
opts = options_for(key)
|
63
|
+
|
64
|
+
if obj.is_a? Enumerable
|
65
|
+
ArraySerializer.new(obj, opts)
|
66
|
+
else
|
67
|
+
CompositeObject.new(obj, opts)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def options_for(key)
|
73
|
+
@opts.reduce({}) do |acc, (opt_key, values)|
|
74
|
+
acc.merge(opt_key.to_sym => values[key.to_sym])
|
75
|
+
end.compact.merge(root: key) { |_, x, y| x || y }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
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.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexis Montagne
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-05-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -100,6 +100,7 @@ files:
|
|
100
100
|
- lib/userializer/array_serializer.rb
|
101
101
|
- lib/userializer/attribute.rb
|
102
102
|
- lib/userializer/base_serializer.rb
|
103
|
+
- lib/userializer/composite_serializer.rb
|
103
104
|
- lib/userializer/has_many.rb
|
104
105
|
- lib/userializer/has_one.rb
|
105
106
|
- lib/userializer/version.rb
|
@@ -122,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
123
|
- !ruby/object:Gem::Version
|
123
124
|
version: '0'
|
124
125
|
requirements: []
|
125
|
-
rubygems_version: 3.
|
126
|
+
rubygems_version: 3.1.4
|
126
127
|
signing_key:
|
127
128
|
specification_version: 4
|
128
129
|
summary: Write a short summary, because RubyGems requires one.
|