hal_presenter 1.5.0 → 1.5.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
  SHA256:
3
- metadata.gz: 306b8de363e5da5cb14c573bf44e7c6cceefd0d0a050dba2a859da94429722aa
4
- data.tar.gz: 42d74e04e03c0b4705023385e3bd9ddff1d3d70f638536f40143a3ab47c30e56
3
+ metadata.gz: 403e79978e056d4f350ac5ec339bb1db0c3b080ff5a1cc53767c807ecddea490
4
+ data.tar.gz: c1910ed9afb1976f13f988a0732ce785343bd1b12ccc3ca3ea164402a09420c5
5
5
  SHA512:
6
- metadata.gz: 44aa0f45829620c8d2b26283503b0906df6201d0d9ec7f38b692c02aabb4a3418c953d0ca4385f112650bd88fa7b654a8f11a7731feab332f45e35fd71042515
7
- data.tar.gz: 82d4afdb641c4299cb9e7c56c18b3ef6b3c7e01234e10f230e0a77d0ae15966c855460774ffd3d2754de763dae00cfd10215842086d6ee7c6b7127dc91e09cbc
6
+ metadata.gz: 0da13bd2e8c5c6809ddd5e61f0774473ff556ef9b8ab58890d5609d808717ef458b01e85a93af1f964789800496d0e6aab119c8b355a262c32ad2485f691f34a
7
+ data.tar.gz: af582f322a79eb338153e2ddf914b22f33304aea63e93cc53d603f6da0e2391acaeb1ab71708f1c77e7dc5ca9f3703787f054b55a8c5c46a3a3545c4424c768f
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -5,10 +5,10 @@ module HALPresenter
5
5
  module Attributes
6
6
  include SuperInit
7
7
 
8
- def attribute(*args, **kwargs, &block)
8
+ def attribute(name, value = Property::NO_VALUE, **kwargs, &block)
9
9
  kwargs[:context] ||= self
10
- attributes.delete_if { |attr| attr.name == args.first }
11
- Property.new(*args, **kwargs, &block).tap do |attr|
10
+ attributes.delete_if { |attr| attr.name == name }
11
+ Property.new(name, value, **kwargs, &block).tap do |attr|
12
12
  attributes << attr
13
13
  end
14
14
  end
@@ -8,16 +8,16 @@ module HALPresenter
8
8
  class Embed < HALPresenter::Property
9
9
  attr_reader :presenter_class
10
10
 
11
- def initialize(name, value = nil, **kwargs, &block)
11
+ def initialize(name, value = NO_VALUE, **kwargs, &block)
12
12
  @presenter_class = kwargs.delete(:presenter_class)
13
13
  super(name, value, **kwargs, &block)
14
14
  end
15
15
  end
16
16
 
17
- def embed(*args, **kwargs, &block)
17
+ def embed(name, value = Property::NO_VALUE, **kwargs, &block)
18
18
  kwargs[:context] ||= self
19
- embedded.delete_if { |embed| embed.name == args.first }
20
- Embed.new(*args, **kwargs, &block).tap do |embed|
19
+ embedded.delete_if { |embed| embed.name == name }
20
+ Embed.new(name, value, **kwargs, &block).tap do |embed|
21
21
  embedded << embed
22
22
  end
23
23
  end
@@ -24,7 +24,7 @@ module HALPresenter
24
24
 
25
25
  alias rel name
26
26
 
27
- def initialize(rel, value = nil, **kwargs, &block)
27
+ def initialize(rel, value = NO_VALUE, **kwargs, &block)
28
28
  @type = kwargs[:type].freeze
29
29
  @deprecation = kwargs[:deprecation].freeze
30
30
  @profile = kwargs[:profile].freeze
@@ -66,6 +66,12 @@ module HALPresenter
66
66
  end
67
67
 
68
68
  kwargs[:context] ||= self
69
+ rel = rel.to_sym
70
+
71
+ if rel == :self || kwargs[:replace_parent]
72
+ links.delete_if { |link| link.rel == rel }
73
+ end
74
+
69
75
  Link.new(rel, value, **kwargs, &block).tap do |link|
70
76
  links << link
71
77
  end
@@ -4,7 +4,7 @@ module HALPresenter
4
4
  module Profile
5
5
  include SuperInit
6
6
 
7
- def profile(value = nil, **kwargs, &block)
7
+ def profile(value = Property::NO_VALUE, **kwargs, &block)
8
8
  if value.nil? && !block_given?
9
9
  raise 'profile must be called with non nil value or be given a block'
10
10
  end
@@ -2,9 +2,11 @@ require 'hal_presenter/lazy_evaluator'
2
2
 
3
3
  module HALPresenter
4
4
  class Property
5
+ NO_VALUE = Object.new.freeze
6
+
5
7
  attr_reader :name, :embed_depth
6
8
 
7
- def initialize(name, value = nil, **kwargs, &block)
9
+ def initialize(name, value = NO_VALUE, **kwargs, &block)
8
10
  @name = name.to_sym
9
11
  @value = value.freeze
10
12
  @embed_depth = kwargs[:embed_depth].freeze
@@ -15,10 +17,16 @@ module HALPresenter
15
17
  def value(resource = nil, options = {})
16
18
  if @lazy
17
19
  @lazy.evaluate(resource, options)
18
- elsif @value
20
+ elsif @value != NO_VALUE
19
21
  @value
20
22
  elsif resource&.respond_to? name_without_curie
21
23
  resource.public_send(name_without_curie)
24
+ else
25
+ raise ArgumentError, <<~ERR
26
+ Cannot serialize #{name.inspect}.
27
+ No value given and resource does not respond to #{name_without_curie}. Resource:
28
+ #{resource.inspect}"
29
+ ERR
22
30
  end
23
31
  end
24
32
 
@@ -5,19 +5,19 @@ require 'hal_presenter/curie_collection'
5
5
  module HALPresenter
6
6
 
7
7
  module ClassMethods
8
- def to_hal(resource, options = {})
8
+ def to_hal(resource, **options)
9
9
  options = options.dup
10
10
  presenter!(resource, options).to_hal(resource, options)
11
11
  end
12
12
 
13
- def to_collection(resources, options = {})
13
+ def to_collection(resources, **options)
14
14
  options = options.dup
15
15
  presenter!(resources, options).to_collection(resources, options)
16
16
  end
17
17
 
18
18
  private
19
19
 
20
- def presenter!(resources, options = {})
20
+ def presenter!(resources, **options)
21
21
  raise Serializer::Error, "resources is nil" if resources.nil?
22
22
  presenter = options.delete(:presenter)
23
23
  presenter ||= HALPresenter.lookup_presenter(resources)
@@ -35,7 +35,7 @@ module HALPresenter
35
35
  base.extend(ClassMethods)
36
36
  end
37
37
 
38
- def to_hal(resource = nil, options = {})
38
+ def to_hal(resource = nil, **options)
39
39
  options = options.dup
40
40
  options[:_depth] ||= 0
41
41
  hash = to_hash(resource, options)
@@ -45,7 +45,7 @@ module HALPresenter
45
45
  JSON.generate(hash)
46
46
  end
47
47
 
48
- def to_collection(resources = [], options = {})
48
+ def to_collection(resources = [], **options)
49
49
  unless can_serialize_collection?
50
50
  raise Error,
51
51
  "Trying to serialize a collection using #{self} which has no collection info. " \
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.5.0
4
+ version: 1.5.1
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: 2020-01-18 00:00:00.000000000 Z
33
+ date: 2020-03-01 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: rake
metadata.gz.sig CHANGED
@@ -1 +1,2 @@
1
- L �oHk�O��_�Jƹ�Ȟ���1U�X��j9Q��lrı6z��eX&���UF���lR��}Am5��@����`!0n9O_���/}l"Y��>+��a;���v�� ˂���f��S���7�<��r���p��4n�3�"쵺�a�K��]��u�G&���n���5��:�Q#l�"���,>eHTކHX@%gFJt�> � �N�M٩��*_)���M8PdlQ�}߉�Pa�"%�v
1
+ -�$����aH����%�d?%�ӏ�Q���_��u�"��s�8��b�1�c�+:=�[v�8x��Ӏ�[#W�Q�^Od_MUS�`���j{x�˙��Jܺj>�XqP�`��ۄ�Nur�'_�x �/�=��A��օ�D�Ô�y>嫳�"Aܨ���@���W�������0���
2
+ �AӲ���<(�d�����.��~��Q@��ؘC9���T�TI)�s�ҩ���F�\