hal_presenter 1.4.0 → 1.5.0

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
  SHA256:
3
- metadata.gz: e0dc504f92084015f773cf97fb82e35f1e969cce9d994190ea193b9966d7fade
4
- data.tar.gz: e7dc11b0623948a02dd51e5f9fccfd912c49d26c5616ce4d61da2a656e6cc330
3
+ metadata.gz: 306b8de363e5da5cb14c573bf44e7c6cceefd0d0a050dba2a859da94429722aa
4
+ data.tar.gz: 42d74e04e03c0b4705023385e3bd9ddff1d3d70f638536f40143a3ab47c30e56
5
5
  SHA512:
6
- metadata.gz: 13dc22af8b63f4bb7c369ebcbecfcbe81df0cd88e534cd4b95bf57f337a19a359dc8a41f8df80377e8cd24e01e02b66803f209959ed304e4cdfdfd4e8cd2cbab
7
- data.tar.gz: fe8e9d146fd80980fadd22856b38467e2f2f428cd6ad00837a4bc945fe12de00e22920959a2e353ede236bd8681fb3d8ad304b4bf49e0a7cd45b0163ad10a6b7
6
+ metadata.gz: 44aa0f45829620c8d2b26283503b0906df6201d0d9ec7f38b692c02aabb4a3418c953d0ca4385f112650bd88fa7b654a8f11a7731feab332f45e35fd71042515
7
+ data.tar.gz: 82d4afdb641c4299cb9e7c56c18b3ef6b3c7e01234e10f230e0a77d0ae15966c855460774ffd3d2754de763dae00cfd10215842086d6ee7c6b7127dc91e09cbc
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -46,15 +46,13 @@ module HALPresenter
46
46
  href = value(resource, options)
47
47
  return {} unless href
48
48
 
49
- hash = {href: HALPresenter.href(href)}.tap do |h|
50
- h[:type] = type if type
51
- h[:deprecation] = deprecation if deprecation
52
- h[:profile] = profile if profile
53
- h[:title] = title if title
54
- h[:templated] = templated if templated
49
+ {href: HALPresenter.href(href)}.tap do |hash|
50
+ hash[:type] = type if type
51
+ hash[:deprecation] = deprecation if deprecation
52
+ hash[:profile] = profile if profile
53
+ hash[:title] = title if title
54
+ hash[:templated] = templated if templated
55
55
  end
56
-
57
- {rel => hash}
58
56
  end
59
57
  end
60
58
 
@@ -68,7 +66,6 @@ module HALPresenter
68
66
  end
69
67
 
70
68
  kwargs[:context] ||= self
71
- links.delete_if { |link| link.rel == rel }
72
69
  Link.new(rel, value, **kwargs, &block).tap do |link|
73
70
  links << link
74
71
  end
@@ -6,21 +6,24 @@ module HALPresenter
6
6
 
7
7
  module ClassMethods
8
8
  def to_hal(resource, options = {})
9
- raise Serializer::Error, "Resource is nil" if resource.nil?
10
9
  options = options.dup
11
- presenter = options.delete(:presenter)
12
- presenter ||= HALPresenter.lookup_presenter(resource)
13
- raise Serializer::Error, "No presenter for #{resource.class}" unless presenter
14
- presenter.to_hal(resource, options)
10
+ presenter!(resource, options).to_hal(resource, options)
15
11
  end
16
12
 
17
13
  def to_collection(resources, options = {})
18
- raise Serializer::Error, "resources is nil" if resources.nil?
19
14
  options = options.dup
15
+ presenter!(resources, options).to_collection(resources, options)
16
+ end
17
+
18
+ private
19
+
20
+ def presenter!(resources, options = {})
21
+ raise Serializer::Error, "resources is nil" if resources.nil?
20
22
  presenter = options.delete(:presenter)
21
23
  presenter ||= HALPresenter.lookup_presenter(resources)
22
24
  raise Serializer::Error, "No presenter for #{resources.first.class}" unless presenter
23
- presenter.to_collection(resources, options)
25
+
26
+ presenter
24
27
  end
25
28
  end
26
29
 
@@ -37,6 +40,8 @@ module HALPresenter
37
40
  options[:_depth] ||= 0
38
41
  hash = to_hash(resource, options)
39
42
  move_curies_to_root! hash
43
+ return hash if options[:as_hash]
44
+
40
45
  JSON.generate(hash)
41
46
  end
42
47
 
@@ -51,6 +56,8 @@ module HALPresenter
51
56
  options[:_depth] ||= 0
52
57
  hash = to_collection_hash(resources, options)
53
58
  move_curies_to_root! hash
59
+ return hash if options[:as_hash]
60
+
54
61
  JSON.generate(hash)
55
62
  end
56
63
 
@@ -133,9 +140,19 @@ module HALPresenter
133
140
 
134
141
  def _serialize_links(links, curies, resource, policy, options)
135
142
  serialized = links.each_with_object({}) do |link, hash|
143
+ rel = link.rel
136
144
  next unless link.nested_depth_ok? options[:_depth]
137
- next if policy && !policy.link?(link.rel)
138
- hash.merge! link.to_h(resource, options)
145
+ next if policy && !policy.link?(rel)
146
+
147
+ link_hash = link.to_h(resource, options)
148
+ next if link_hash.empty?
149
+
150
+ if hash.key? rel
151
+ hash[rel] = [hash[rel]] unless hash[rel].is_a? Array
152
+ hash[rel] << link_hash
153
+ else
154
+ hash.merge!(rel => link_hash)
155
+ end
139
156
  end
140
157
  curies = _serialize_curies(curies, resource, options)
141
158
  serialized[:curies] = curies if curies.any?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hal_presenter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sammy Henningsson
@@ -30,7 +30,7 @@ cert_chain:
30
30
  ZMhjYR7sRczGJx+GxGU2EaR0bjRsPVlC4ywtFxoOfRG3WaJcpWGEoAoMJX6Z0bRv
31
31
  M40=
32
32
  -----END CERTIFICATE-----
33
- date: 2019-12-18 00:00:00.000000000 Z
33
+ date: 2020-01-18 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: rake
metadata.gz.sig CHANGED
@@ -1,2 +1 @@
1
- K�s����� ����Iv]����?/\��j
2
- ĬEig����F�Z��}(�uVUڠ�z�uz�6p(-Jz��n��5d�͚���<�T�������- /�E�)#�Xy���x8�4*+)&W�&Q��T(.H��B�#�V�g�� ��M�`$�2m�;�~
1
+ L �oHk�O��_�Jƹ�Ȟ���1U�X��j9Q��lrı6z��eX&���UF���lR��}Am5��@����`!0n�9O_���/}l"Y��>+��a;���v�� ˂���f��S���7�<��r���p��4�n�3�"쵺�a�K��]��uG&���n���5��:�Q#l�"���,>e�HTކHX@%g�F�Jt�> � �N�M٩��*_)���M8P�dlQ�}߉�Pa�"%�v