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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1fdb9a14bc7ef54528db8017406dd28708f6543b
4
- data.tar.gz: 3daea7984ca3962708d013f493c5f913a6308551
3
+ metadata.gz: 2897e8bc0d974babfc03d1461b90ae41c9e082a7
4
+ data.tar.gz: d2205bd5adc36fb326f2e90571e5f7b5ea02fd2b
5
5
  SHA512:
6
- metadata.gz: e572bb50f4e82435f9760cb19cfcece75f68810ae2d517c83a6e8876751918d822cae1d90f9e5b0371ca9ab12c89ea9f2f6b3b687a59112763b02a2459a0cebb
7
- data.tar.gz: d891bc22668258cc50a0e0863338245ed9d24c4e9e73dd4948a9752e31b972731043e22c9b4f5eeba9282d586f1d4061c4eaf1fb84fd9a53227b0a479be2c2c2
6
+ metadata.gz: 78f1ff66a8dee4282b2286cb7fdb96d56d48a498eb43f00d1606b8362d7d5597d58ce6eddf82fd8f2d513865fc1a969c01588cced3855c962c764ec3e97c76e9
7
+ data.tar.gz: 22b1031407abe3fc434add985e476c1d7281f56884e22162325694b052f8e7d48184e879fac68654da845c1c405f212d8c4bfd5e7b3ac591587e74dddf2f21c7
checksums.yaml.gz.sig CHANGED
Binary file
@@ -7,6 +7,8 @@ module HALDecorator
7
7
  @_attributes << Property.new(*args, &block)
8
8
  end
9
9
 
10
+ protected
11
+
10
12
  def attributes
11
13
  @_attributes ||= []
12
14
  @_attributes.dup
@@ -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
@@ -10,6 +10,8 @@ module HALDecorator
10
10
  @_curies << Property.new(rel, value, &block)
11
11
  end
12
12
 
13
+ protected
14
+
13
15
  def curies
14
16
  @_curies ||= []
15
17
  @_curies.dup
@@ -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.send(setter_method, hash[attribute.name.to_s])
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.send(embed.name)
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.send(setter_method, embedded_resource)
61
+ resource.public_send(setter_method, embedded_resource)
61
62
  end
62
63
  end
63
64
 
@@ -16,6 +16,8 @@ module HALDecorator
16
16
  @_embedded << Embed.new(*args, &block)
17
17
  end
18
18
 
19
+ protected
20
+
19
21
  def embedded
20
22
  @_embedded ||= []
21
23
  @_embedded.dup
@@ -19,6 +19,8 @@ module HALDecorator
19
19
  @_links << Link.new(rel, value, http_method: method || methods, &block)
20
20
  end
21
21
 
22
+ protected
23
+
22
24
  def links
23
25
  @_links ||= []
24
26
  @_links.dup
@@ -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.send method, *a, &b }
33
- return send(method, *args, &block)
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 to_hash(object, options, embed: true)
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 do |resource|
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, embed: false)
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, embed: false)
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
- ���Ts����A�� ��V�����pH*.�\n��)]c������6԰̾H�� z��5yהcEp@�HN*أ�n�;_x��?A����?y(:"�֦��c�c��ŗ��4�C��>FJ �*ɝkm)F�.ӌw(B�ӯ v���PK
1
+ ����wBI8�������"t^�uc�=,��� ������Z�F�#�$L��*���%�e��k�†�X������ƥŪ�� 3���N�. V7�\��� F��ӏ�����P��jv#.n1H��l2@p��Y��$)4�I3��G��Qd��>�|��*���$8h}&5oOZ��K������ *�ɐ�a?�*��=�P[�:[�o
2
+ � ��܊���̜�Ш������MVj#�TA�
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hal_decorator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sammy Henningsson
metadata.gz.sig CHANGED
Binary file