dry_serialization 0.4.4.1 → 0.4.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dry_serialization/concerns/deserializable.rb +63 -61
- data/lib/dry_serialization/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: 6722956e8906a82b85c3cf64a9ce275882d217c28f674003d410fa36195cfd13
|
4
|
+
data.tar.gz: b6881b357536253f4dd37ff059de7e4dcb01ec3cd6e77cc79d62a0c4b17ac60c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5a6afddeca50b3fd6baacf423375178354f33482edb81acd2c18693269fd1f07d0ffbd01bf03c4ff15a559e45d06080550efdeb842215e372d9f7212666aab6
|
7
|
+
data.tar.gz: 4fa61e7c736019154a8d70191459756168c6d8154cb6254517e9715f192bfe93a9851a67e775fdf7b404af47f8122a95babd95e87ccb89b59e3790a23f09e7b0
|
@@ -1,69 +1,71 @@
|
|
1
1
|
module DrySerialization
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def build_nested_relationships(attrs, base_params)
|
15
|
-
relations = relationships(base_params)
|
16
|
-
attrs.merge!(transform_object_values(relations))
|
17
|
-
end
|
18
|
-
|
19
|
-
def transform_object_values(relations)
|
20
|
-
relations.to_h.each_with_object({}) do |(key, data), acc|
|
21
|
-
key = "#{key}_attributes" unless key.end_with?('_attributes')
|
22
|
-
acc[key] ||= transform_relationships_to_get_attributes(data)
|
23
|
-
transform_nested_relationships_to_get_attributes(acc[key], data)
|
2
|
+
module Concerns
|
3
|
+
module Deserializable
|
4
|
+
|
5
|
+
def deserialized_payload(base_params, object = nil)
|
6
|
+
attrs = attributes(base_params)
|
7
|
+
id = dig_id(base_params, object)
|
8
|
+
attrs.merge!(id: id) if id
|
9
|
+
|
10
|
+
build_nested_relationships(attrs, base_params)
|
24
11
|
end
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
nested_relationships.each do |(relation, rel_data)|
|
32
|
-
hash = { "#{relation}_attributes": transform_relationships_to_get_attributes(rel_data) }
|
33
|
-
parent_relation_hash.merge!(hash)
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def build_nested_relationships(attrs, base_params)
|
16
|
+
relations = relationships(base_params)
|
17
|
+
attrs.merge!(transform_object_values(relations))
|
34
18
|
end
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
object[:data].map do |e|
|
43
|
-
attrs = transform_relationships_to_get_attributes({ data: e })
|
44
|
-
attrs.merge!(transform_object_values(e[:relationships]))
|
45
|
-
end
|
46
|
-
else
|
47
|
-
return
|
19
|
+
|
20
|
+
def transform_object_values(relations)
|
21
|
+
relations.to_h.each_with_object({}) do |(key, data), acc|
|
22
|
+
key = "#{key}_attributes" unless key.end_with?('_attributes')
|
23
|
+
acc[key] ||= transform_relationships_to_get_attributes(data)
|
24
|
+
transform_nested_relationships_to_get_attributes(acc[key], data)
|
25
|
+
end
|
48
26
|
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def dig_id(base_params, object)
|
52
|
-
return unless object
|
53
27
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
28
|
+
def transform_nested_relationships_to_get_attributes(parent_relation_hash, data)
|
29
|
+
nested_relationships = data[:data].respond_to?(:key?) ? relationships(data) : nil
|
30
|
+
return unless nested_relationships
|
31
|
+
|
32
|
+
nested_relationships.each do |(relation, rel_data)|
|
33
|
+
hash = { "#{relation}_attributes": transform_relationships_to_get_attributes(rel_data) }
|
34
|
+
parent_relation_hash.merge!(hash)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def transform_relationships_to_get_attributes(object)
|
39
|
+
case object[:data]
|
40
|
+
when Hash
|
41
|
+
object[:attributes] || object.dig(:data, :attributes)
|
42
|
+
when Array
|
43
|
+
object[:data].map do |e|
|
44
|
+
attrs = transform_relationships_to_get_attributes({ data: e })
|
45
|
+
attrs.merge!(transform_object_values(e[:relationships]))
|
46
|
+
end
|
47
|
+
else
|
48
|
+
return
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def dig_id(base_params, object)
|
53
|
+
return unless object
|
54
|
+
|
55
|
+
base_params.dig(:data, :id)
|
56
|
+
send("#{object}_id")
|
57
|
+
rescue
|
58
|
+
nil
|
59
|
+
end
|
60
|
+
|
61
|
+
def attributes(base_params)
|
62
|
+
base_params.dig(:data, :attributes)
|
63
|
+
end
|
64
|
+
|
65
|
+
def relationships(base_params)
|
66
|
+
base_params.dig(:data, :relationships)
|
67
|
+
end
|
68
|
+
|
66
69
|
end
|
67
|
-
|
68
70
|
end
|
69
71
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry_serialization
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.4.
|
4
|
+
version: 0.4.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Heft
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-10-
|
11
|
+
date: 2020-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|