hal_presenter 1.3.0 → 1.4.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: 10e6de4d852aa101cc4318eb88d152996adeb2e288359a0c9a53f6efcc5e9cc6
4
- data.tar.gz: 89080e06a6c5a756bc00d9620663fb8e6eb97906059285666d13f007a4e6b4fa
3
+ metadata.gz: e0dc504f92084015f773cf97fb82e35f1e969cce9d994190ea193b9966d7fade
4
+ data.tar.gz: e7dc11b0623948a02dd51e5f9fccfd912c49d26c5616ce4d61da2a656e6cc330
5
5
  SHA512:
6
- metadata.gz: ab43657fea638457294930e648d5a33b2a6055ff02e511ba239d55b0b38bb8bad9ea6a3816b2c01674efa38c816876681af075dc5fc6ccb67ad1ebe44a891ae6
7
- data.tar.gz: 84f44aa7ef856dbec2e23c56a1aceef04b35b253ebc1dd574c7379aa574f72b57d6b8e969e4a53eff8b7be5da54ecc23ca8d0411d87dbdb2f52a694bb7dcd8a7
6
+ metadata.gz: 13dc22af8b63f4bb7c369ebcbecfcbe81df0cd88e534cd4b95bf57f337a19a359dc8a41f8df80377e8cd24e01e02b66803f209959ed304e4cdfdfd4e8cd2cbab
7
+ data.tar.gz: fe8e9d146fd80980fadd22856b38467e2f2f428cd6ad00837a4bc945fe12de00e22920959a2e353ede236bd8681fb3d8ad304b4bf49e0a7cd45b0163ad10a6b7
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -7,7 +7,7 @@ module HALPresenter
7
7
 
8
8
  def initialize(block, context)
9
9
  @context = context
10
- define_singleton_method(:evaluate_block, &block)
10
+ define_singleton_method(:call, &block)
11
11
  end
12
12
 
13
13
  def update_context(context)
@@ -16,17 +16,21 @@ module HALPresenter
16
16
 
17
17
  def evaluate(resource, options)
18
18
  @resource = resource
19
- @options = options || {}
20
- evaluate_block
19
+ @options = (options || {}).dup
20
+ call
21
21
  ensure
22
- @resource = nil
23
- @options = nil
22
+ clear_state
24
23
  end
25
24
 
26
25
  private
27
26
 
28
27
  attr_reader :context
29
28
 
29
+ def clear_state
30
+ @resource = nil
31
+ @options = nil
32
+ end
33
+
30
34
  def method_missing(method, *args, &block)
31
35
  return super unless context.respond_to?(method)
32
36
 
@@ -1,19 +1,19 @@
1
1
  module HALPresenter
2
2
  module Policy
3
3
  module DSL
4
-
5
4
  class Rules
5
+ DEFAULT_PROC = Proc.new { false }
6
6
 
7
7
  def attributes
8
- @attributes ||= Hash.new(Proc.new { false })
8
+ @attributes ||= Hash.new(DEFAULT_PROC)
9
9
  end
10
10
 
11
11
  def links
12
- @links ||= Hash.new(Proc.new { false })
12
+ @links ||= Hash.new(DEFAULT_PROC)
13
13
  end
14
14
 
15
15
  def embedded
16
- @embedded ||= Hash.new(Proc.new { false })
16
+ @embedded ||= Hash.new(DEFAULT_PROC)
17
17
  end
18
18
 
19
19
  private :attributes, :links, :embedded
@@ -53,34 +53,31 @@ module HALPresenter
53
53
  def strip_curie(rel)
54
54
  rel.to_s.split(':', 2)[1]&.to_sym
55
55
  end
56
-
57
56
  end
58
57
 
59
58
  module ClassMethods
60
-
61
59
  def allow_by_default(*types)
62
60
  rules.defaults(*types, value: true)
63
61
  end
64
62
 
65
- def attribute(*names)
66
- b = block_given? ? Proc.new : Proc.new { true }
67
- names.each { |name| rules.add_attribute(name, b) }
63
+ def attribute(*names, &block)
64
+ block ||= Proc.new { true }
65
+ names.each { |name| rules.add_attribute(name, block) }
68
66
  end
69
67
 
70
- def link(*rels)
71
- b = block_given? ? Proc.new : Proc.new { true }
72
- rels.each { |rel| rules.add_link(rel, b) }
68
+ def link(*rels, &block)
69
+ block ||= Proc.new { true }
70
+ rels.each { |rel| rules.add_link(rel, block) }
73
71
  end
74
72
 
75
- def embed(*names)
76
- b = block_given? ? Proc.new : Proc.new { true }
77
- names.each { |name| rules.add_embed(name, b) }
73
+ def embed(*names, &block)
74
+ block ||= Proc.new { true }
75
+ names.each { |name| rules.add_embed(name, block) }
78
76
  end
79
77
 
80
78
  def rules
81
79
  @rules ||= Rules.new
82
80
  end
83
-
84
81
  end
85
82
 
86
83
  def self.included(mod)
@@ -94,24 +91,49 @@ module HALPresenter
94
91
  end
95
92
 
96
93
  def attribute?(name)
97
- run self.class.rules.attribute_rule_for(name)
94
+ __check __rules.attribute_rule_for(name)
98
95
  end
99
96
 
100
97
  def link?(rel)
101
98
  return true if rel == :self
102
- run self.class.rules.link_rule_for(rel)
99
+ __check __rules.link_rule_for(rel)
103
100
  end
104
101
 
105
102
  def embed?(name)
106
- run self.class.rules.embed_rule_for(name)
103
+ __check __rules.embed_rule_for(name)
107
104
  end
108
105
 
109
106
  private
110
107
 
111
108
  attr_reader :current_user, :resource, :options
112
109
 
113
- def run(block)
114
- instance_eval(&block) && true || false
110
+ def delegate_attribute(policy_class, attr, **opts)
111
+ delegate_to(policy_class, :attribute?, args: attr, **opts)
112
+ end
113
+
114
+ def delegate_link(policy_class, rel, **opts)
115
+ delegate_to(policy_class, :link?, args: rel, **opts)
116
+ end
117
+
118
+ def delegate_embed(policy_class, rel, **opts)
119
+ delegate_to(policy_class, :embed?, args: rel, **opts)
120
+ end
121
+
122
+ def delegate_to(policy_class, method, resource: nil, args: nil, **opts)
123
+ resource ||= send(:resource)
124
+ opts = options.merge(opts)
125
+ policy = policy_class.new(current_user, resource, opts)
126
+ args = Array(args)
127
+ args.unshift(method)
128
+ policy.send(*args)
129
+ end
130
+
131
+ def __rules
132
+ self.class.rules
133
+ end
134
+
135
+ def __check(block)
136
+ !!instance_eval(&block)
115
137
  end
116
138
 
117
139
  end
@@ -28,6 +28,11 @@ module HALPresenter
28
28
  self
29
29
  end
30
30
 
31
+ def nested_depth_ok?(level)
32
+ return true unless embed_depth
33
+ level <= embed_depth
34
+ end
35
+
31
36
  private
32
37
 
33
38
  def initialize_copy(source)
@@ -5,23 +5,23 @@ require 'hal_presenter/curie_collection'
5
5
  module HALPresenter
6
6
 
7
7
  module ClassMethods
8
- def to_hal(resource, options = {})
9
- raise Serializer::Error, "Resource is nil" if resource.nil?
10
- 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)
15
- end
16
-
17
- def to_collection(resources, options = {})
18
- raise Serializer::Error, "resources is nil" if resources.nil?
19
- options = options.dup
20
- presenter = options.delete(:presenter)
21
- presenter ||= HALPresenter.lookup_presenter(resources)
22
- raise Serializer::Error, "No presenter for #{resources.first.class}" unless presenter
23
- presenter.to_collection(resources, options)
24
- end
8
+ def to_hal(resource, options = {})
9
+ raise Serializer::Error, "Resource is nil" if resource.nil?
10
+ 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)
15
+ end
16
+
17
+ def to_collection(resources, options = {})
18
+ raise Serializer::Error, "resources is nil" if resources.nil?
19
+ options = options.dup
20
+ presenter = options.delete(:presenter)
21
+ presenter ||= HALPresenter.lookup_presenter(resources)
22
+ raise Serializer::Error, "No presenter for #{resources.first.class}" unless presenter
23
+ presenter.to_collection(resources, options)
24
+ end
25
25
  end
26
26
 
27
27
  module Serializer
@@ -83,9 +83,7 @@ module HALPresenter
83
83
  serialized.merge! _serialize_embedded(embedded, resources, policy, options)
84
84
 
85
85
  # Embedded resources
86
- embed_options = options.dup
87
- embed_options[:_depth] += 1
88
- serialized_resources = resources.map { |resource| to_hash(resource, embed_options) }
86
+ serialized_resources = resources.map { |resource| to_hash(resource, options.dup) }
89
87
  serialized[:_embedded] ||= {}
90
88
  serialized[:_embedded].merge!(properties.name => serialized_resources)
91
89
  end
@@ -127,7 +125,7 @@ module HALPresenter
127
125
 
128
126
  def _serialize_attributes(attributes, resource, policy, options)
129
127
  attributes.each_with_object({}) do |attribute, hash|
130
- next unless nested_depth_ok?(attribute, options[:_depth])
128
+ next unless attribute.nested_depth_ok? options[:_depth]
131
129
  next if policy && !policy.attribute?(attribute.name)
132
130
  hash[attribute.name] = attribute.value(resource, options)
133
131
  end
@@ -135,7 +133,7 @@ module HALPresenter
135
133
 
136
134
  def _serialize_links(links, curies, resource, policy, options)
137
135
  serialized = links.each_with_object({}) do |link, hash|
138
- next unless nested_depth_ok?(link, options[:_depth])
136
+ next unless link.nested_depth_ok? options[:_depth]
139
137
  next if policy && !policy.link?(link.rel)
140
138
  hash.merge! link.to_h(resource, options)
141
139
  end
@@ -147,7 +145,7 @@ module HALPresenter
147
145
 
148
146
  def _serialize_curies(curies, resource, options)
149
147
  curies.each_with_object([]) do |curie, array|
150
- next unless nested_depth_ok?(curie, options[:_depth])
148
+ next unless curie.nested_depth_ok? options[:_depth]
151
149
  hash = curie.to_h(resource, options)
152
150
  array << hash unless hash.empty?
153
151
  end
@@ -155,7 +153,7 @@ module HALPresenter
155
153
 
156
154
  def _serialize_embedded(embedded, object, policy, options)
157
155
  serialized = embedded.each_with_object({}) do |embed, hash|
158
- next unless nested_depth_ok?(embed, options[:_depth])
156
+ next unless embed.nested_depth_ok? options[:_depth]
159
157
  next if policy && !policy.embed?(embed.name)
160
158
  resource = embed.value(object, options) or next
161
159
  presenter = embed.presenter_class
@@ -192,10 +190,5 @@ module HALPresenter
192
190
  def policy_for(resource, options)
193
191
  policy_class&.new(options[:current_user], resource, options)
194
192
  end
195
-
196
- def nested_depth_ok?(property, level)
197
- return true unless embed_depth = property.embed_depth
198
- level <= embed_depth
199
- end
200
193
  end
201
194
  end
@@ -7,7 +7,7 @@ module HALPresenter
7
7
  return default unless superclass.respond_to?(method, true)
8
8
 
9
9
  if default.respond_to? :each
10
- superclass.send(method).map do |prop|
10
+ Array(superclass.send(method)).map do |prop|
11
11
  prop.clone.change_context(self)
12
12
  end
13
13
  else
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.3.0
4
+ version: 1.4.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-10-29 00:00:00.000000000 Z
33
+ date: 2019-12-18 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: rake
@@ -198,7 +198,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
198
198
  - !ruby/object:Gem::Version
199
199
  version: '0'
200
200
  requirements: []
201
- rubygems_version: 3.0.6
201
+ rubygems_version: 3.0.3
202
202
  signing_key:
203
203
  specification_version: 4
204
204
  summary: JSON HAL serializer
metadata.gz.sig CHANGED
@@ -1,2 +1,2 @@
1
- 8Я�Y�J4��#G����޽/�|�uu�(|*���J����a��^�8|F�-$��y�=�`ݑ^��u]�^�C�.��Wc�t�A1����]5��w
2
- W+���A3���k��AhL�Y�G��b�Թ@����t�؉鳃]Q9����$(}����3���L�ӻhM��c]1���3<XM0n�~�?�v�� ��"���j�,P�̙�ls0c����¦u���v_S��#�iPЭ��XP��
1
+ Ks����� ����Iv]����?/\��j
2
+ ĬEig����F�Z��}(uVUڠ�zuz�6p(-Jz��n��5d�͚���<�T�������- /�E�)#�Xy���x84*+)&W�&Q��T(.H��B�#�Vg�� ��M�`$�2m�;�~