hal_decorator 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/hal_decorator/attributes.rb +2 -0
- data/lib/hal_decorator/collection.rb +3 -1
- data/lib/hal_decorator/curies.rb +2 -0
- data/lib/hal_decorator/deserializer.rb +4 -3
- data/lib/hal_decorator/embedded.rb +2 -0
- data/lib/hal_decorator/links.rb +2 -0
- data/lib/hal_decorator/property.rb +2 -2
- data/lib/hal_decorator/serializer.rb +13 -15
- data.tar.gz.sig +2 -1
- metadata +1 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2897e8bc0d974babfc03d1461b90ae41c9e082a7
|
4
|
+
data.tar.gz: d2205bd5adc36fb326f2e90571e5f7b5ea02fd2b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78f1ff66a8dee4282b2286cb7fdb96d56d48a498eb43f00d1606b8362d7d5597d58ce6eddf82fd8f2d513865fc1a969c01588cced3855c962c764ec3e97c76e9
|
7
|
+
data.tar.gz: 22b1031407abe3fc434add985e476c1d7281f56884e22162325694b052f8e7d48184e879fac68654da845c1c405f212d8c4bfd5e7b3ac591587e74dddf2f21c7
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
@@ -14,7 +14,7 @@ module HALDecorator
|
|
14
14
|
|
15
15
|
def initialize(name, &block)
|
16
16
|
@name = name
|
17
|
-
instance_exec(&block)
|
17
|
+
instance_exec(&block) if block_given?
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -22,6 +22,8 @@ module HALDecorator
|
|
22
22
|
@_parameters = CollectionParameters.new(of, &block)
|
23
23
|
end
|
24
24
|
|
25
|
+
protected
|
26
|
+
|
25
27
|
def collection_parameters
|
26
28
|
@_parameters ||= nil
|
27
29
|
end
|
data/lib/hal_decorator/curies.rb
CHANGED
@@ -7,6 +7,7 @@ module HALDecorator
|
|
7
7
|
class Error < StandardError; end
|
8
8
|
|
9
9
|
def from_hal(payload, resource = nil)
|
10
|
+
return if payload.nil? || payload.empty?
|
10
11
|
hash = JSON.parse(payload)
|
11
12
|
from_hash(hash, resource)
|
12
13
|
end
|
@@ -37,7 +38,7 @@ module HALDecorator
|
|
37
38
|
attributes.each do |attribute|
|
38
39
|
setter_method = setter_method_name(attribute.name)
|
39
40
|
next unless resource.respond_to? setter_method
|
40
|
-
resource.
|
41
|
+
resource.public_send(setter_method, hash[attribute.name.to_s])
|
41
42
|
end
|
42
43
|
end
|
43
44
|
|
@@ -50,14 +51,14 @@ module HALDecorator
|
|
50
51
|
embedded_hash = hash.dig('_embedded', embed.name.to_s)
|
51
52
|
next unless embedded_hash&.any?
|
52
53
|
|
53
|
-
embedded_resource = resource.
|
54
|
+
embedded_resource = resource.public_send(embed.name)
|
54
55
|
embedded_resource =
|
55
56
|
if embedded_hash.is_a? Array
|
56
57
|
embedded_hash.map { |h| decorator.from_hash(h, embedded_resource) }
|
57
58
|
else
|
58
59
|
decorator.from_hash(embedded_hash, embedded_resource)
|
59
60
|
end
|
60
|
-
resource.
|
61
|
+
resource.public_send(setter_method, embedded_resource)
|
61
62
|
end
|
62
63
|
end
|
63
64
|
|
data/lib/hal_decorator/links.rb
CHANGED
@@ -29,8 +29,8 @@ module HALDecorator
|
|
29
29
|
|
30
30
|
def method_missing(method, *args, &block)
|
31
31
|
if @scope&.respond_to? method
|
32
|
-
define_singleton_method(method) { |*a, &b| @scope.
|
33
|
-
return
|
32
|
+
define_singleton_method(method) { |*a, &b| @scope.public_send method, *a, &b }
|
33
|
+
return public_send(method, *args, &block)
|
34
34
|
end
|
35
35
|
super
|
36
36
|
end
|
@@ -6,20 +6,12 @@ module HALDecorator
|
|
6
6
|
|
7
7
|
class Error < StandardError; end
|
8
8
|
|
9
|
-
def to_hal(resource, options = {})
|
9
|
+
def to_hal(resource = nil, options = {})
|
10
10
|
hash = to_hash(resource, options)
|
11
11
|
JSON.generate(hash)
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
{}.tap do |serialized|
|
16
|
-
serialized.merge! serialize_attributes(object, options)
|
17
|
-
serialized.merge! serialize_links(object, options)
|
18
|
-
serialized.merge! serialize_embedded(object, options) if embed
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def to_collection(resources, options = {})
|
14
|
+
def to_collection(resources = [], options = {})
|
23
15
|
parameters = collection_parameters
|
24
16
|
if parameters.nil?
|
25
17
|
raise Error,
|
@@ -31,15 +23,21 @@ module HALDecorator
|
|
31
23
|
serialized = _serialize_attributes(parameters.attributes, resources, options)
|
32
24
|
serialized.merge! _serialize_links(links, curies, resources, options)
|
33
25
|
|
34
|
-
serialized_resources = resources.map
|
35
|
-
to_hash(resource, embed: false)
|
36
|
-
end
|
26
|
+
serialized_resources = resources.map { |resource| to_hash(resource, options) }
|
37
27
|
serialized[:_embedded] = { parameters.name => serialized_resources }
|
38
28
|
JSON.generate(serialized)
|
39
29
|
end
|
40
30
|
|
41
31
|
protected
|
42
32
|
|
33
|
+
def to_hash(object, options)
|
34
|
+
{}.tap do |serialized|
|
35
|
+
serialized.merge! serialize_attributes(object, options)
|
36
|
+
serialized.merge! serialize_links(object, options)
|
37
|
+
serialized.merge! serialize_embedded(object, options)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
43
41
|
def serialize_attributes(object, options)
|
44
42
|
_serialize_attributes(attributes, object, options)
|
45
43
|
end
|
@@ -95,11 +93,11 @@ module HALDecorator
|
|
95
93
|
if resource.respond_to? :each
|
96
94
|
decorator ||= HALDecorator.lookup_decorator(resource.first).first
|
97
95
|
resource.map do |resrc|
|
98
|
-
decorator.to_hash(resrc, options
|
96
|
+
decorator.to_hash(resrc, options)
|
99
97
|
end
|
100
98
|
else
|
101
99
|
decorator ||= HALDecorator.lookup_decorator(resource).first
|
102
|
-
decorator.to_hash(resource, options
|
100
|
+
decorator.to_hash(resource, options)
|
103
101
|
end
|
104
102
|
end
|
105
103
|
return {} if serialized.empty?
|
data.tar.gz.sig
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
|
1
|
+
����wB�I8�������"t^�uc�=,���������Z�F�#�$L��*���%�e��k��X������ƥŪ�� �3���N�. V7�\��� F��ӏ�����P��jv#.n1H��l2@p��Y��$)4�I3��G��Q�d��>�|��*���$8h�}&5oO�Z��K������ *�ɐ�a?�*��=�P�[�:[�o
|
2
|
+
���܊���̜�Ш������MVj#�TA�
|
metadata
CHANGED
metadata.gz.sig
CHANGED
Binary file
|