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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e2028b3233247d37829ca68a1c6dfd5453f67c9cf4ef4635b46326f87deac4fe
4
- data.tar.gz: 9f0f912ef276566c2eaaf3797646c0b495554643d3d4a0963d50e9da29c50257
3
+ metadata.gz: f11ddd3d1fc5d239938188bda0cba4c3e57d86433fc39f5214dc9ae6aee875af
4
+ data.tar.gz: e1a80305b54998dfb4c84e6ec8c1b0c49ccaedc0b21c969becfaca90a671ac60
5
5
  SHA512:
6
- metadata.gz: e4ef0ec2e74937c413b32f4610a12c03697b29108c34bc0dbc3799c3646cda95356beb5dc038f249010c74757efa28e3e2b6353552f84f8480564b7a77feed42
7
- data.tar.gz: cf939fdae7f09497b44dd0fe5ec93002b0e68426c8cbd736eb9a04bf3f7a80d919e83bf0d83a2d3c798c251f2d5750ac99b75b1fb5ad291e7924c2e73403768e
6
+ metadata.gz: 0af2dbe4d5979ed8d525794f9797c0011a4535ebe7127afd3bddd2a5c11911c0b919c86b8527916a49ee0e29decf662a28c9317cdea935cd8366e1bf56d781e7
7
+ data.tar.gz: '019aaca0b3c14e5d96ac5e0f661f5fb9cb8c04ddcbff254ae27f9c380c19a44019037ae80026dbb24604c0a2d09e8b50331f9e94b325515083ab1c67cb083594'
data/.tool-versions CHANGED
@@ -1 +1 @@
1
- ruby 2.5.3
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 (1.6.2)
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.0.2)
10
+ byebug (11.0.1)
11
11
  diff-lcs (1.3)
12
- jaro_winkler (1.5.2)
13
- parallel (1.13.0)
14
- parser (2.6.0.0)
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.8.0)
20
- rspec-core (~> 3.8.0)
21
- rspec-expectations (~> 3.8.0)
22
- rspec-mocks (~> 3.8.0)
23
- rspec-core (3.8.0)
24
- rspec-support (~> 3.8.0)
25
- rspec-expectations (3.8.2)
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.8.0)
28
- rspec-mocks (3.8.0)
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.8.0)
31
- rspec-support (3.8.0)
32
- rubocop (0.63.1)
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.5, != 2.5.1.1)
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 (~> 1.4.0)
40
- ruby-progressbar (1.10.0)
41
- unicode-display_width (1.4.1)
42
- yard (0.9.18)
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
@@ -15,18 +15,17 @@ module TheHelp
15
15
  # input :send_welcome_message, default: true
16
16
  #
17
17
  # authorization_policy do
18
- # authorized = false
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
- # success: callback(:message_sent))
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
- # allowed.call
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
- # run_callback(success, message)
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
- # # Calling a service with a block when the service is not designed to
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
- # self.result = :the_service_result
70
+ # result.success :the_service_result
87
71
  # end
88
72
  # end
89
73
  #
90
74
  # service_result = nil
91
75
  #
92
- # CanTakeABlock.call() # works just fine
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
- result = new(*args).call(&block)
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 if result_set?
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
- attr_writer :result
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 result
261
- raise TheHelp::NoResultError unless result_set?
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TheHelp
4
- VERSION = '1.6.2'
4
+ VERSION = '2.0.0'
5
5
  end
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: 1.6.2
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-02-23 00:00:00.000000000 Z
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
- rubyforge_project:
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