hal_decorator 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/hal_decorator/policy/dsl.rb +59 -0
- data/lib/hal_decorator/policy.rb +22 -0
- data/lib/hal_decorator/serializer.rb +24 -18
- data/lib/hal_decorator.rb +5 -2
- data.tar.gz.sig +1 -1
- metadata +6 -4
- 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: 8343a995eb8b48575002dae6dd95cb7537403209
|
4
|
+
data.tar.gz: c1547111bfdec4083e4fdd4048eba42b32a79e8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d168ffdf32bdb764797f3eb82b5145459976efeff5b8121a7485ea8eeea594b01439f9fc8270948d280e491c48361491f7da8a09e3a4f4e627a885fec59a2dc4
|
7
|
+
data.tar.gz: 7f5217f70e6ef567590d423987c1b07e94ae23cee953f629c0084ba2c02bbab1147139998bff8d538959e120c6bca63eaea9526b731adeb7189011eddb458824
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module HALDecorator
|
2
|
+
module Policy
|
3
|
+
module DSL
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
attr_reader :rules
|
7
|
+
|
8
|
+
def attribute(name, &block)
|
9
|
+
@rules ||= {}
|
10
|
+
@rules[:attributes] ||= {}
|
11
|
+
@rules[:attributes][name] = block
|
12
|
+
end
|
13
|
+
|
14
|
+
def link(rel, &block)
|
15
|
+
@rules ||= {}
|
16
|
+
@rules[:links] ||= {}
|
17
|
+
@rules[:links][rel] = block
|
18
|
+
end
|
19
|
+
|
20
|
+
def embed(name, &block)
|
21
|
+
@rules ||= {}
|
22
|
+
@rules[:embeds] ||= {}
|
23
|
+
@rules[:embeds][name] = block
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.included(mod)
|
28
|
+
mod.extend(ClassMethods)
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize(current_user = nil, resource)
|
32
|
+
@current_user = current_user
|
33
|
+
@resource = resource
|
34
|
+
end
|
35
|
+
|
36
|
+
def attribute?(name)
|
37
|
+
run self.class.rules&.dig(:attributes, name)
|
38
|
+
end
|
39
|
+
|
40
|
+
def link?(rel)
|
41
|
+
run self.class.rules&.dig(:links, rel)
|
42
|
+
end
|
43
|
+
|
44
|
+
def embed?(name)
|
45
|
+
run self.class.rules&.dig(:embeds, name)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
attr_reader :current_user, :resource
|
51
|
+
|
52
|
+
def run(block)
|
53
|
+
return false unless block && block.respond_to?(:call)
|
54
|
+
instance_eval(&block) && true || false
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module HALDecorator
|
2
|
+
module Policy
|
3
|
+
|
4
|
+
def policy(clazz)
|
5
|
+
@_policy = clazz
|
6
|
+
end
|
7
|
+
|
8
|
+
protected
|
9
|
+
|
10
|
+
def policy_class
|
11
|
+
@_policy ||= init_policy
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def init_policy
|
17
|
+
return unless is_a? Class
|
18
|
+
return unless superclass.respond_to?(:policy_class, true)
|
19
|
+
superclass.policy_class
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -36,8 +36,8 @@ module HALDecorator
|
|
36
36
|
end
|
37
37
|
links = parameters.links
|
38
38
|
curies = parameters.curies
|
39
|
-
serialized = _serialize_attributes(parameters.attributes, resources, options)
|
40
|
-
serialized.merge! _serialize_links(links, curies, resources, options)
|
39
|
+
serialized = _serialize_attributes(parameters.attributes, resources, nil, options)
|
40
|
+
serialized.merge! _serialize_links(links, curies, resources, nil, options)
|
41
41
|
|
42
42
|
serialized_resources = resources.map { |resource| to_hash(resource, options) }
|
43
43
|
serialized[:_embedded] = { parameters.name => serialized_resources }
|
@@ -47,29 +47,31 @@ module HALDecorator
|
|
47
47
|
protected
|
48
48
|
|
49
49
|
def to_hash(resource, options)
|
50
|
+
policy = policy_class&.new(options[:current_user], resource)
|
51
|
+
|
50
52
|
{}.tap do |serialized|
|
51
|
-
serialized.merge! serialize_attributes(resource, options)
|
52
|
-
serialized.merge! serialize_links(resource, options)
|
53
|
-
serialized.merge! serialize_embedded(resource, options)
|
53
|
+
serialized.merge! serialize_attributes(resource, policy, options)
|
54
|
+
serialized.merge! serialize_links(resource, policy, options)
|
55
|
+
serialized.merge! serialize_embedded(resource, policy, options)
|
54
56
|
|
55
57
|
run_post_serialize_hook!(resource, options, serialized)
|
56
58
|
end
|
57
59
|
end
|
58
60
|
|
59
|
-
def serialize_attributes(resource, options)
|
60
|
-
_serialize_attributes(attributes, resource, options)
|
61
|
+
def serialize_attributes(resource, policy, options)
|
62
|
+
_serialize_attributes(attributes, resource, policy, options)
|
61
63
|
end
|
62
64
|
|
63
|
-
def serialize_links(resource, options)
|
64
|
-
_serialize_links(links, curies, resource, options)
|
65
|
+
def serialize_links(resource, policy, options)
|
66
|
+
_serialize_links(links, curies, resource, policy, options)
|
65
67
|
end
|
66
68
|
|
67
|
-
def serialize_curies(resource, options)
|
68
|
-
_serialize_curies(curies, resource, options)
|
69
|
+
def serialize_curies(resource, policy, options)
|
70
|
+
_serialize_curies(curies, resource, policy, options)
|
69
71
|
end
|
70
72
|
|
71
|
-
def serialize_embedded(resource, options)
|
72
|
-
_serialize_embedded(embedded, resource, options)
|
73
|
+
def serialize_embedded(resource, policy, options)
|
74
|
+
_serialize_embedded(embedded, resource, policy, options)
|
73
75
|
end
|
74
76
|
|
75
77
|
def run_post_serialize_hook!(resource, options, serialized)
|
@@ -79,27 +81,30 @@ module HALDecorator
|
|
79
81
|
|
80
82
|
private
|
81
83
|
|
82
|
-
def _serialize_attributes(attributes, resource, options)
|
84
|
+
def _serialize_attributes(attributes, resource, policy, options)
|
83
85
|
attributes.each_with_object({}) do |attribute, hash|
|
86
|
+
next if policy && !policy.attribute?(attribute.name)
|
84
87
|
hash[attribute.name] = attribute.value(resource, options)
|
85
88
|
end
|
86
89
|
end
|
87
90
|
|
88
|
-
def _serialize_links(links, curies, resource, options)
|
91
|
+
def _serialize_links(links, curies, resource, policy, options)
|
89
92
|
serialized = links.each_with_object({}) do |link, hash|
|
93
|
+
next if policy && !policy.link?(link.rel)
|
90
94
|
href = link.value(resource, options) or next
|
91
95
|
hash[link.rel] = { href: HALDecorator.href(href) }.tap do |s|
|
92
96
|
s[:method] = link.http_method if link.http_method
|
93
97
|
end
|
94
98
|
end
|
95
|
-
curies = _serialize_curies(curies, resource, options)
|
99
|
+
curies = _serialize_curies(curies, resource, policy, options)
|
96
100
|
serialized[:curies] = curies if curies.any?
|
97
101
|
return {} if serialized.empty?
|
98
102
|
{ _links: serialized }
|
99
103
|
end
|
100
104
|
|
101
|
-
def _serialize_curies(curies, resource, options)
|
105
|
+
def _serialize_curies(curies, resource, policy, options)
|
102
106
|
curies.each_with_object([]) do |curie, array|
|
107
|
+
next if policy && !policy.link?(curie.name)
|
103
108
|
href = curie.value(resource, options) or next
|
104
109
|
array << {
|
105
110
|
name: curie.name,
|
@@ -109,8 +114,9 @@ module HALDecorator
|
|
109
114
|
end
|
110
115
|
end
|
111
116
|
|
112
|
-
def _serialize_embedded(embedded, object, options)
|
117
|
+
def _serialize_embedded(embedded, object, policy, options)
|
113
118
|
serialized = embedded.each_with_object({}) do |embed, hash|
|
119
|
+
next if policy && !policy.embed?(embed.name)
|
114
120
|
resource = embed.value(object, options) or next
|
115
121
|
decorator = embed.decorator_class
|
116
122
|
hash[embed.name] =
|
data/lib/hal_decorator.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
+
require 'hal_decorator/model'
|
2
|
+
require 'hal_decorator/policy'
|
3
|
+
require 'hal_decorator/policy/dsl'
|
1
4
|
require 'hal_decorator/attributes'
|
2
|
-
require 'hal_decorator/embedded'
|
3
5
|
require 'hal_decorator/links'
|
6
|
+
require 'hal_decorator/embedded'
|
4
7
|
require 'hal_decorator/curies'
|
5
|
-
require 'hal_decorator/model'
|
6
8
|
require 'hal_decorator/serializer'
|
7
9
|
require 'hal_decorator/deserializer'
|
8
10
|
require 'hal_decorator/collection'
|
@@ -18,4 +20,5 @@ module HALDecorator
|
|
18
20
|
include HALDecorator::Model
|
19
21
|
include HALDecorator::Serializer
|
20
22
|
include HALDecorator::Deserializer
|
23
|
+
include HALDecorator::Policy
|
21
24
|
end
|
data.tar.gz.sig
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
�Bpo:r�����(1�a���)����iD�#���_ȍ>S�]�<5P�<���23��US�VZ���13oԔ�˾��%��E�g�Q괸H����Dcq�P_�Eֈȵ>�.Y��+��'�S����k)���+���v��Q�i2�ӖQ|��b�fG�Փ.��P��9Wx7� ��Ʉ����=�+���DŇ�[����� twm,������8���-������甋4��e�[Lś�z�νѨ֬(q.c
|
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.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sammy Henningsson
|
@@ -31,7 +31,7 @@ cert_chain:
|
|
31
31
|
CNZdF8Vavp6xMQbPHZwqjaeZz2WRXYS7jyYSvCunjwa3OtvXtfbIEGEWE6IM+t9k
|
32
32
|
H1g6Q+B6qk9O6g==
|
33
33
|
-----END CERTIFICATE-----
|
34
|
-
date: 2017-
|
34
|
+
date: 2017-09-03 00:00:00.000000000 Z
|
35
35
|
dependencies:
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
@@ -114,7 +114,7 @@ dependencies:
|
|
114
114
|
- !ruby/object:Gem::Version
|
115
115
|
version: '9.0'
|
116
116
|
description: |
|
117
|
-
|
117
|
+
A DSL for serializing resources according to
|
118
118
|
HypertextApplicationLanguage.
|
119
119
|
email: sammy.henningsson@gmail.com
|
120
120
|
executables: []
|
@@ -129,6 +129,8 @@ files:
|
|
129
129
|
- lib/hal_decorator/embedded.rb
|
130
130
|
- lib/hal_decorator/links.rb
|
131
131
|
- lib/hal_decorator/model.rb
|
132
|
+
- lib/hal_decorator/policy.rb
|
133
|
+
- lib/hal_decorator/policy/dsl.rb
|
132
134
|
- lib/hal_decorator/property.rb
|
133
135
|
- lib/hal_decorator/serialize_hooks.rb
|
134
136
|
- lib/hal_decorator/serializer.rb
|
@@ -152,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
154
|
version: '0'
|
153
155
|
requirements: []
|
154
156
|
rubyforge_project:
|
155
|
-
rubygems_version: 2.6.
|
157
|
+
rubygems_version: 2.6.13
|
156
158
|
signing_key:
|
157
159
|
specification_version: 4
|
158
160
|
summary: HAL serializer
|
metadata.gz.sig
CHANGED
Binary file
|