the_help 1.1.4 → 1.2.0
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
- data/.ruby-version +1 -0
- data/Gemfile.lock +2 -0
- data/lib/the_help/errors.rb +1 -0
- data/lib/the_help/provides_callbacks.rb +17 -17
- data/lib/the_help/service.rb +88 -36
- data/lib/the_help/version.rb +1 -1
- data/the_help.gemspec +1 -0
- metadata +16 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9839566fa2430a430f15129d61d210d5140a9799eb85de929ea3d04768821e90
|
4
|
+
data.tar.gz: fc9e5f57c9e615edd1a2858893ca8529dd2c471dcc7a0e9f57bc16b96f428cdb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a202d3b4725139bec17b7644279486ba9c71abd6ccc6f904f749e473c012460c0264809796523d518c9172e16e36e325e55aa7a1cc03b26b4a7eefe86fed3c8
|
7
|
+
data.tar.gz: df6f0dcbc797265af71b36725e554143f027eeed97c2963318fb56e73cf91cb1323eaa1d59ef6f5be4941a147b501017d28d2fbf396e00a8084ea6c702634259
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.5.0
|
data/Gemfile.lock
CHANGED
@@ -36,6 +36,7 @@ GEM
|
|
36
36
|
unicode-display_width (~> 1.0, >= 1.0.1)
|
37
37
|
ruby-progressbar (1.9.0)
|
38
38
|
unicode-display_width (1.3.0)
|
39
|
+
yard (0.9.12)
|
39
40
|
|
40
41
|
PLATFORMS
|
41
42
|
ruby
|
@@ -45,6 +46,7 @@ DEPENDENCIES
|
|
45
46
|
rspec (~> 3.0)
|
46
47
|
rubocop (~> 0.50)
|
47
48
|
the_help!
|
49
|
+
yard
|
48
50
|
|
49
51
|
BUNDLED WITH
|
50
52
|
1.16.1
|
data/lib/the_help/errors.rb
CHANGED
@@ -4,28 +4,28 @@ module TheHelp
|
|
4
4
|
# Adds a callback DSL to including classes
|
5
5
|
#
|
6
6
|
# @example
|
7
|
-
#
|
8
|
-
#
|
7
|
+
# class Foo
|
8
|
+
# attr_accessor :collaborator
|
9
9
|
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
10
|
+
# def do_something
|
11
|
+
# collaborator.do_some_other_thing(when_done: callback(:it_was_done))
|
12
|
+
# end
|
13
13
|
#
|
14
|
-
#
|
15
|
-
#
|
14
|
+
# callback(:it_was_done) do |some_arg:|
|
15
|
+
# puts "Yay! #{some_arg}"
|
16
|
+
# end
|
16
17
|
# end
|
17
|
-
# end
|
18
18
|
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
19
|
+
# class Bar
|
20
|
+
# def do_some_other_thing(when_done:)
|
21
|
+
# when_done.call('done by Bar')
|
22
|
+
# end
|
23
|
+
# end
|
24
24
|
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
25
|
+
# f = Foo.new
|
26
|
+
# f.collaborator = Bar.new
|
27
|
+
# f.do_something
|
28
|
+
# # STDOUT: "Yay! done by Bar"
|
29
29
|
#
|
30
30
|
# Callbacks can be given to collaborating objects, but the actual methods are
|
31
31
|
# defined as private methods. This allows the object to control which other
|
data/lib/the_help/service.rb
CHANGED
@@ -10,54 +10,92 @@ module TheHelp
|
|
10
10
|
# application.
|
11
11
|
#
|
12
12
|
# @example
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
13
|
+
# class CreateNewUserAccount < TheHelp::Service
|
14
|
+
# input :user
|
15
|
+
# input :send_welcome_message, default: true
|
16
|
+
#
|
17
|
+
# authorization_policy do
|
18
|
+
# authorized = false
|
19
|
+
# call_service(Authorize, permission: :admin_users,
|
20
|
+
# allowed: ->() { authorized = true })
|
21
|
+
# authorized
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# main do
|
25
|
+
# # do something to create the user account
|
26
|
+
# if send_welcome_message
|
27
|
+
# call_service(SendWelcomeMessage, user: user,
|
28
|
+
# success: callback(:message_sent))
|
29
|
+
# end
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# callback(:message_sent) do
|
33
|
+
# # do something really important, I'm sure
|
34
|
+
# end
|
22
35
|
# end
|
23
36
|
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
37
|
+
# class Authorize < TheHelp::Service
|
38
|
+
# input :permission
|
39
|
+
# input :allowed
|
40
|
+
#
|
41
|
+
# authorization_policy allow_all: true
|
42
|
+
#
|
43
|
+
# main do
|
44
|
+
# if user_has_permission?
|
45
|
+
# allowed.call
|
46
|
+
# end
|
29
47
|
# end
|
30
48
|
# end
|
31
49
|
#
|
32
|
-
#
|
33
|
-
#
|
50
|
+
# class SendWelcomeMessage < TheHelp::Service
|
51
|
+
# input :user
|
52
|
+
# input :success, default: ->() { }
|
53
|
+
#
|
54
|
+
# main do
|
55
|
+
# # whatever
|
56
|
+
# success.call
|
57
|
+
# end
|
34
58
|
# end
|
35
|
-
# end
|
36
59
|
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
60
|
+
# CreateNewUserAccount.(context: current_user, user: new_user_object)
|
61
|
+
#
|
62
|
+
# @example Calling services with a block
|
40
63
|
#
|
41
|
-
#
|
64
|
+
# # Calling a service with a block when the service is not designed to
|
65
|
+
# # receive one will result in an exception being raised
|
42
66
|
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
67
|
+
# class DoesNotTakeBlock < TheHelp::Service
|
68
|
+
# authorization_policy allow_all: true
|
69
|
+
#
|
70
|
+
# main do
|
71
|
+
# # whatever
|
46
72
|
# end
|
47
73
|
# end
|
48
|
-
# end
|
49
74
|
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
75
|
+
# DoesNotTakeBlock.call { |result| true } # raises TheHelp::NoResultError
|
76
|
+
#
|
77
|
+
# # However, if the service *is* designed to receive a block (by explicitly
|
78
|
+
# # assigning to the internal `#result` attribute in the main routine), the
|
79
|
+
# # result will be yielded to the block if a block is present.
|
80
|
+
#
|
81
|
+
# class CanTakeABlock < TheHelp::Service
|
82
|
+
# authorization_policy allow_all: true
|
53
83
|
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
84
|
+
# main do
|
85
|
+
# self.result = :the_service_result
|
86
|
+
# end
|
57
87
|
# end
|
58
|
-
# end
|
59
88
|
#
|
60
|
-
#
|
89
|
+
# service_result = nil
|
90
|
+
#
|
91
|
+
# CanTakeABlock.call() # works just fine
|
92
|
+
# service_result
|
93
|
+
# #=> nil # but obviously the result is just discarded
|
94
|
+
#
|
95
|
+
# CanTakeABlock.call { |result| service_result = result }
|
96
|
+
# service_result
|
97
|
+
# #=> :the_service_result
|
98
|
+
#
|
61
99
|
class Service
|
62
100
|
include ProvidesCallbacks
|
63
101
|
include ServiceCaller
|
@@ -87,8 +125,8 @@ module TheHelp
|
|
87
125
|
# Any arguments are passed to #initialize
|
88
126
|
#
|
89
127
|
# @return [Class] Returns the receiver
|
90
|
-
def call(*args)
|
91
|
-
new(*args).call
|
128
|
+
def call(*args, &block)
|
129
|
+
new(*args, &block).call
|
92
130
|
self
|
93
131
|
end
|
94
132
|
|
@@ -152,6 +190,11 @@ module TheHelp
|
|
152
190
|
self.logger = logger
|
153
191
|
self.not_authorized = not_authorized
|
154
192
|
self.inputs = inputs
|
193
|
+
if block_given?
|
194
|
+
self.result_handler = ->(result) {
|
195
|
+
yield result
|
196
|
+
}
|
197
|
+
end
|
155
198
|
end
|
156
199
|
|
157
200
|
def call
|
@@ -160,13 +203,17 @@ module TheHelp
|
|
160
203
|
authorize
|
161
204
|
log_service_call
|
162
205
|
main
|
206
|
+
unless result_handler.nil?
|
207
|
+
result_handler.call(result)
|
208
|
+
end
|
163
209
|
end
|
164
210
|
self
|
165
211
|
end
|
166
212
|
|
167
213
|
private
|
168
214
|
|
169
|
-
attr_accessor :context, :logger, :not_authorized
|
215
|
+
attr_accessor :context, :logger, :not_authorized, :result_handler
|
216
|
+
attr_writer :result
|
170
217
|
attr_reader :inputs
|
171
218
|
|
172
219
|
alias service_context context
|
@@ -209,5 +256,10 @@ module TheHelp
|
|
209
256
|
def stop!
|
210
257
|
throw :stop
|
211
258
|
end
|
259
|
+
|
260
|
+
def result
|
261
|
+
raise TheHelp::NoResultError unless defined?(@result)
|
262
|
+
@result
|
263
|
+
end
|
212
264
|
end
|
213
265
|
end
|
data/lib/the_help/version.rb
CHANGED
data/the_help.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: the_help
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Wilger
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0.50'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: A service layer framework
|
56
70
|
email:
|
57
71
|
- john@johnwilger.com
|
@@ -63,6 +77,7 @@ files:
|
|
63
77
|
- ".gitignore"
|
64
78
|
- ".rspec"
|
65
79
|
- ".rubocop.yml"
|
80
|
+
- ".ruby-version"
|
66
81
|
- ".travis.yml"
|
67
82
|
- CODE_OF_CONDUCT.md
|
68
83
|
- Gemfile
|