ams_hal 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ams_hal/adapter.rb +47 -38
- data/lib/ams_hal/embed.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ae5d537e7726b7aa36721d90fe6e39156b9bf82
|
4
|
+
data.tar.gz: 64a517fb6e9d71e1ef16270a2f8660cb7e113fcb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17a14cc150b84c8a9aa63a7c5be4cdefa286daee74ea6ff66223f160d0ed9ca751cd0d19b8a7785947a8abc27e1326ba7e4914bad698861cbbadd5a1631cd05a
|
7
|
+
data.tar.gz: cf7033fb8efdc620b498023d16eb1fe1005029404363b0cfad3e9c4d8ae4dd5ca7d588e79a258e692461b877fddaa4905ac028fb46da9732197529635bee606c
|
data/lib/ams_hal/adapter.rb
CHANGED
@@ -1,38 +1,57 @@
|
|
1
1
|
module AmsHal
|
2
2
|
class Adapter < ActiveModelSerializers::Adapter::Base
|
3
|
-
def serializable_hash(options =
|
3
|
+
def serializable_hash(options = {})
|
4
4
|
options = serialization_options(options)
|
5
5
|
options[:fields] ||= instance_options[:fields]
|
6
|
-
|
7
|
-
serializer
|
8
|
-
array << serialize_resource(_serializer, instance_options, options)
|
9
|
-
end
|
6
|
+
hash = if serializer.respond_to?(:each)
|
7
|
+
serialize_collection(serializer, instance_options, options)
|
10
8
|
else
|
11
9
|
serialize_resource(serializer, instance_options, options)
|
12
10
|
end
|
13
11
|
|
14
|
-
self.class.transform_key_casing!(
|
12
|
+
self.class.transform_key_casing!(hash, instance_options)
|
15
13
|
end
|
16
14
|
|
17
15
|
protected
|
18
16
|
|
19
17
|
def serialize_resource(serializer, adapter_options, options)
|
18
|
+
skip_embedded = options.delete(:skip_embedded) || false
|
19
|
+
|
20
20
|
options[:include_directive] = {} # Don't include associations as attributes
|
21
|
-
|
21
|
+
hash = serializer.serializable_hash(adapter_options, options, self)
|
22
22
|
|
23
23
|
if links = serialize_links(serializer)
|
24
|
-
|
24
|
+
hash[:_links] = links if links.any?
|
25
25
|
end
|
26
26
|
|
27
|
+
return hash if skip_embedded
|
28
|
+
|
27
29
|
if embedded = serialize_embedded(serializer)
|
28
|
-
|
30
|
+
hash[:_embedded] = embedded if embedded.any?
|
31
|
+
end
|
32
|
+
|
33
|
+
if associations = serialize_associations(serializer, adapter_options)
|
34
|
+
hash[:_embedded] = associations if associations.any?
|
35
|
+
end
|
36
|
+
|
37
|
+
hash
|
38
|
+
end
|
39
|
+
|
40
|
+
def serialize_collection(serializer, adapter_options, options)
|
41
|
+
options[:include_directive] = {} # Don't include associations as attributes
|
42
|
+
hash = {}
|
43
|
+
|
44
|
+
if links = serialize_links(serializer)
|
45
|
+
hash[:_links] = links if links.any?
|
29
46
|
end
|
30
47
|
|
31
|
-
|
32
|
-
|
48
|
+
embedded = serializer.map do |_serializer|
|
49
|
+
options[:skip_embedded] = true
|
50
|
+
serialize_resource(_serializer, instance_options, options)
|
33
51
|
end
|
52
|
+
hash[:_embedded] = embedded if embedded.any?
|
34
53
|
|
35
|
-
|
54
|
+
hash
|
36
55
|
end
|
37
56
|
|
38
57
|
def serialize_links(serializer)
|
@@ -85,34 +104,24 @@ module AmsHal
|
|
85
104
|
end
|
86
105
|
end
|
87
106
|
|
88
|
-
def serialize_associations(serializer)
|
107
|
+
def serialize_associations(serializer, adapter_options)
|
89
108
|
serializer.associations.each_with_object({}) do |association, embedded|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
serialize_embedded_resource(
|
106
|
-
resrc,
|
107
|
-
serializer: association.serializer.send(:options)[:serializer]
|
108
|
-
)
|
109
|
+
embedded[association.name] =
|
110
|
+
if association.serializer.nil? || association.serializer.object.nil?
|
111
|
+
# active_model_serializers <= 0.10.4
|
112
|
+
object = serializer.object
|
113
|
+
resource = object.public_send(association.name)
|
114
|
+
serialized = [resource].flatten.map do |resrc|
|
115
|
+
serialize_embedded_resource(resrc, serializer: association.serializer&.class)
|
116
|
+
end
|
117
|
+
serialized.size == 1 ? serialized.first : serialized
|
118
|
+
elsif association.serializer.respond_to? :each
|
119
|
+
association.serializer.map do |_serializer|
|
120
|
+
serialize_resource(_serializer, instance_options, {skip_embedded: true})
|
121
|
+
end
|
122
|
+
else
|
123
|
+
serialize_resource(association.serializer, instance_options, {skip_embedded: true})
|
109
124
|
end
|
110
|
-
else
|
111
|
-
embedded[association.name] = serialize_embedded_resource(
|
112
|
-
resource,
|
113
|
-
serializer: association.serializer&.class
|
114
|
-
)
|
115
|
-
end
|
116
125
|
end
|
117
126
|
end
|
118
127
|
|
data/lib/ams_hal/embed.rb
CHANGED
@@ -4,9 +4,10 @@ module AmsHal
|
|
4
4
|
class Embed
|
5
5
|
include ActiveModelSerializers::SerializationContext::UrlHelpers
|
6
6
|
|
7
|
-
attr_reader :name, :options
|
7
|
+
attr_reader :serializer, :name, :options
|
8
8
|
|
9
9
|
def initialize(serializer, name, options = {}, &block)
|
10
|
+
@serializer = serializer
|
10
11
|
@name = name
|
11
12
|
@options = options
|
12
13
|
@block = block
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ams_hal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sammy Henningsson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_model_serializers
|