the_help 1.6.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.tool-versions +1 -1
- data/Gemfile.lock +22 -24
- data/lib/the_help/service.rb +64 -45
- data/lib/the_help/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f11ddd3d1fc5d239938188bda0cba4c3e57d86433fc39f5214dc9ae6aee875af
|
4
|
+
data.tar.gz: e1a80305b54998dfb4c84e6ec8c1b0c49ccaedc0b21c969becfaca90a671ac60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0af2dbe4d5979ed8d525794f9797c0011a4535ebe7127afd3bddd2a5c11911c0b919c86b8527916a49ee0e29decf662a28c9317cdea935cd8366e1bf56d781e7
|
7
|
+
data.tar.gz: '019aaca0b3c14e5d96ac5e0f661f5fb9cb8c04ddcbff254ae27f9c380c19a44019037ae80026dbb24604c0a2d09e8b50331f9e94b325515083ab1c67cb083594'
|
data/.tool-versions
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby 2.
|
1
|
+
ruby 2.6.3
|
data/Gemfile.lock
CHANGED
@@ -1,45 +1,43 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
the_help (
|
4
|
+
the_help (2.0.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
9
|
ast (2.4.0)
|
10
|
-
byebug (
|
10
|
+
byebug (11.0.1)
|
11
11
|
diff-lcs (1.3)
|
12
|
-
jaro_winkler (1.5.
|
13
|
-
parallel (1.
|
14
|
-
parser (2.6.
|
12
|
+
jaro_winkler (1.5.3)
|
13
|
+
parallel (1.18.0)
|
14
|
+
parser (2.6.5.0)
|
15
15
|
ast (~> 2.4.0)
|
16
|
-
powerpack (0.1.2)
|
17
16
|
rainbow (3.0.0)
|
18
17
|
rake (10.5.0)
|
19
|
-
rspec (3.
|
20
|
-
rspec-core (~> 3.
|
21
|
-
rspec-expectations (~> 3.
|
22
|
-
rspec-mocks (~> 3.
|
23
|
-
rspec-core (3.
|
24
|
-
rspec-support (~> 3.
|
25
|
-
rspec-expectations (3.
|
18
|
+
rspec (3.9.0)
|
19
|
+
rspec-core (~> 3.9.0)
|
20
|
+
rspec-expectations (~> 3.9.0)
|
21
|
+
rspec-mocks (~> 3.9.0)
|
22
|
+
rspec-core (3.9.0)
|
23
|
+
rspec-support (~> 3.9.0)
|
24
|
+
rspec-expectations (3.9.0)
|
26
25
|
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
-
rspec-support (~> 3.
|
28
|
-
rspec-mocks (3.
|
26
|
+
rspec-support (~> 3.9.0)
|
27
|
+
rspec-mocks (3.9.0)
|
29
28
|
diff-lcs (>= 1.2.0, < 2.0)
|
30
|
-
rspec-support (~> 3.
|
31
|
-
rspec-support (3.
|
32
|
-
rubocop (0.
|
29
|
+
rspec-support (~> 3.9.0)
|
30
|
+
rspec-support (3.9.0)
|
31
|
+
rubocop (0.75.1)
|
33
32
|
jaro_winkler (~> 1.5.1)
|
34
33
|
parallel (~> 1.10)
|
35
|
-
parser (>= 2.
|
36
|
-
powerpack (~> 0.1)
|
34
|
+
parser (>= 2.6)
|
37
35
|
rainbow (>= 2.2.2, < 4.0)
|
38
36
|
ruby-progressbar (~> 1.7)
|
39
|
-
unicode-display_width (
|
40
|
-
ruby-progressbar (1.10.
|
41
|
-
unicode-display_width (1.
|
42
|
-
yard (0.9.
|
37
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
38
|
+
ruby-progressbar (1.10.1)
|
39
|
+
unicode-display_width (1.6.0)
|
40
|
+
yard (0.9.20)
|
43
41
|
|
44
42
|
PLATFORMS
|
45
43
|
ruby
|
data/lib/the_help/service.rb
CHANGED
@@ -15,18 +15,17 @@ module TheHelp
|
|
15
15
|
# input :send_welcome_message, default: true
|
16
16
|
#
|
17
17
|
# authorization_policy do
|
18
|
-
#
|
19
|
-
# call_service(Authorize, permission: :admin_users,
|
20
|
-
# allowed: ->() { authorized = true })
|
21
|
-
# authorized
|
18
|
+
# call_service(Authorize, permission: :admin_users).success?
|
22
19
|
# end
|
23
20
|
#
|
24
21
|
# main do
|
25
22
|
# # do something to create the user account
|
26
23
|
# if send_welcome_message
|
27
|
-
# call_service(SendWelcomeMessage, user: user
|
28
|
-
#
|
24
|
+
# call_service(SendWelcomeMessage, user: user) do |result|
|
25
|
+
# callback(:message_sent) if result.success?
|
26
|
+
# end
|
29
27
|
# end
|
28
|
+
# result.success
|
30
29
|
# end
|
31
30
|
#
|
32
31
|
# callback(:message_sent) do |message|
|
@@ -36,25 +35,25 @@ module TheHelp
|
|
36
35
|
#
|
37
36
|
# class Authorize < TheHelp::Service
|
38
37
|
# input :permission
|
39
|
-
# input :allowed
|
40
38
|
#
|
41
39
|
# authorization_policy allow_all: true
|
42
40
|
#
|
43
41
|
# main do
|
44
42
|
# if user_has_permission?
|
45
|
-
#
|
43
|
+
# result.success
|
44
|
+
# else
|
45
|
+
# result.error 'Permission Denied'
|
46
46
|
# end
|
47
47
|
# end
|
48
48
|
# end
|
49
49
|
#
|
50
50
|
# class SendWelcomeMessage < TheHelp::Service
|
51
51
|
# input :user
|
52
|
-
# input :success, default: ->(message) { }
|
53
52
|
#
|
54
53
|
# main do
|
55
54
|
# message = 'Hello, world!'
|
56
55
|
# # do something with message...
|
57
|
-
#
|
56
|
+
# result.success message
|
58
57
|
# end
|
59
58
|
# end
|
60
59
|
#
|
@@ -62,38 +61,20 @@ module TheHelp
|
|
62
61
|
#
|
63
62
|
# @example Calling services with a block
|
64
63
|
#
|
65
|
-
# #
|
66
|
-
# # receive one will result in an exception being raised
|
67
|
-
#
|
68
|
-
# class DoesNotTakeBlock < TheHelp::Service
|
69
|
-
# authorization_policy allow_all: true
|
70
|
-
#
|
71
|
-
# main do
|
72
|
-
# # whatever
|
73
|
-
# end
|
74
|
-
# end
|
75
|
-
#
|
76
|
-
# DoesNotTakeBlock.call { |result| true } # raises TheHelp::NoResultError
|
77
|
-
#
|
78
|
-
# # However, if the service *is* designed to receive a block (by explicitly
|
79
|
-
# # assigning to the internal `#result` attribute in the main routine), the
|
80
|
-
# # result will be yielded to the block if a block is present.
|
64
|
+
# # The service result will be yielded to the block if a block is present.
|
81
65
|
#
|
82
66
|
# class CanTakeABlock < TheHelp::Service
|
83
67
|
# authorization_policy allow_all: true
|
84
68
|
#
|
85
69
|
# main do
|
86
|
-
#
|
70
|
+
# result.success :the_service_result
|
87
71
|
# end
|
88
72
|
# end
|
89
73
|
#
|
90
74
|
# service_result = nil
|
91
75
|
#
|
92
|
-
# CanTakeABlock.call
|
93
|
-
# service_result
|
94
|
-
# #=> nil # but obviously the result is just discarded
|
76
|
+
# CanTakeABlock.call { |result| service_result = result.value }
|
95
77
|
#
|
96
|
-
# CanTakeABlock.call { |result| service_result = result }
|
97
78
|
# service_result
|
98
79
|
# #=> :the_service_result
|
99
80
|
#
|
@@ -126,9 +107,7 @@ module TheHelp
|
|
126
107
|
#
|
127
108
|
# Any arguments are passed to #initialize
|
128
109
|
def call(*args, &block)
|
129
|
-
|
130
|
-
return result unless result.is_a?(self)
|
131
|
-
self
|
110
|
+
new(*args).call(&block)
|
132
111
|
end
|
133
112
|
|
134
113
|
# :nodoc:
|
@@ -185,8 +164,51 @@ module TheHelp
|
|
185
164
|
end
|
186
165
|
end
|
187
166
|
|
167
|
+
# Holds the result of running a service as well as the execution status
|
168
|
+
#
|
169
|
+
# An instance of this class will be returned from any service call and will have a status of
|
170
|
+
# either :success or :error along with a value that is set by the service.
|
171
|
+
class Result
|
172
|
+
attr_reader :status, :value
|
173
|
+
|
174
|
+
def initialize
|
175
|
+
self.status = :pending
|
176
|
+
self.value = nil
|
177
|
+
end
|
178
|
+
|
179
|
+
def pending?
|
180
|
+
status == :pending
|
181
|
+
end
|
182
|
+
|
183
|
+
def success?
|
184
|
+
status == :success
|
185
|
+
end
|
186
|
+
|
187
|
+
def error?
|
188
|
+
status == :error
|
189
|
+
end
|
190
|
+
|
191
|
+
def success(value = nil)
|
192
|
+
self.value = value
|
193
|
+
self.status = :success
|
194
|
+
freeze
|
195
|
+
end
|
196
|
+
|
197
|
+
def error(value)
|
198
|
+
self.value = value
|
199
|
+
self.status = :error
|
200
|
+
freeze
|
201
|
+
end
|
202
|
+
|
203
|
+
private
|
204
|
+
|
205
|
+
attr_writer :status, :value
|
206
|
+
end
|
207
|
+
|
188
208
|
def initialize(context:, logger: Logger.new($stdout),
|
189
209
|
not_authorized: CB_NOT_AUTHORIZED, **inputs)
|
210
|
+
@result = Result.new
|
211
|
+
|
190
212
|
self.context = context
|
191
213
|
self.logger = logger
|
192
214
|
self.not_authorized = not_authorized
|
@@ -194,26 +216,28 @@ module TheHelp
|
|
194
216
|
self.stop_caller = false
|
195
217
|
end
|
196
218
|
|
219
|
+
# Executes the service and returns the result
|
220
|
+
#
|
221
|
+
# @return [TheHelp::Service::Result]
|
197
222
|
def call
|
198
223
|
validate_service_definition
|
199
224
|
catch(:stop) do
|
200
225
|
authorize
|
201
226
|
log_service_call
|
202
227
|
main
|
228
|
+
check_result!
|
203
229
|
self.block_result = yield result if block_given?
|
204
230
|
end
|
205
231
|
throw :stop if stop_caller
|
206
232
|
return block_result if block_given?
|
207
|
-
return result
|
208
|
-
self
|
233
|
+
return result
|
209
234
|
end
|
210
235
|
|
211
236
|
private
|
212
237
|
|
213
238
|
attr_accessor :context, :logger, :not_authorized, :block_result,
|
214
239
|
:stop_caller
|
215
|
-
|
216
|
-
attr_reader :inputs
|
240
|
+
attr_reader :inputs, :result
|
217
241
|
|
218
242
|
alias service_context context
|
219
243
|
alias service_logger logger
|
@@ -257,13 +281,8 @@ module TheHelp
|
|
257
281
|
throw :stop
|
258
282
|
end
|
259
283
|
|
260
|
-
def
|
261
|
-
raise TheHelp::NoResultError
|
262
|
-
@result
|
263
|
-
end
|
264
|
-
|
265
|
-
def result_set?
|
266
|
-
defined?(@result)
|
284
|
+
def check_result!
|
285
|
+
raise TheHelp::NoResultError if result.pending?
|
267
286
|
end
|
268
287
|
|
269
288
|
def run_callback(callback, *args)
|
data/lib/the_help/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: the_help
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Wilger
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: byebug
|
@@ -127,8 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
127
127
|
- !ruby/object:Gem::Version
|
128
128
|
version: '0'
|
129
129
|
requirements: []
|
130
|
-
|
131
|
-
rubygems_version: 2.7.6
|
130
|
+
rubygems_version: 3.0.3
|
132
131
|
signing_key:
|
133
132
|
specification_version: 4
|
134
133
|
summary: A service layer framework
|