the_help 1.1.4 → 1.2.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: 12d2ca84288d7edcf861be2115e275ea1ebbbf4e5eeb0bd1ad109334e2310b78
4
- data.tar.gz: cbbc8bf078f9452d80488f9993dddb615a10c1fc73ffecfc81d9adf2e606309d
3
+ metadata.gz: 9839566fa2430a430f15129d61d210d5140a9799eb85de929ea3d04768821e90
4
+ data.tar.gz: fc9e5f57c9e615edd1a2858893ca8529dd2c471dcc7a0e9f57bc16b96f428cdb
5
5
  SHA512:
6
- metadata.gz: '00586882e22049dfc9ff1aeeed59037d0d8703346495db16a62eda8327171a9c3729d6c733dc27fb29c745ed6304338c530097de929f583331b18ec6fd99857e'
7
- data.tar.gz: f1c932128245cf6e7c66a343ca4f815a60308d03a017d8d2f89c6f035c48f3212c5bbc577a67b9638ad1b1c14ec629b26f189b5b46cee17320c23c5e2c061c18
6
+ metadata.gz: 6a202d3b4725139bec17b7644279486ba9c71abd6ccc6f904f749e473c012460c0264809796523d518c9172e16e36e325e55aa7a1cc03b26b4a7eefe86fed3c8
7
+ data.tar.gz: df6f0dcbc797265af71b36725e554143f027eeed97c2963318fb56e73cf91cb1323eaa1d59ef6f5be4941a147b501017d28d2fbf396e00a8084ea6c702634259
@@ -0,0 +1 @@
1
+ 2.5.0
@@ -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
@@ -5,5 +5,6 @@ module TheHelp
5
5
  class AbstractClassError < StandardError; end
6
6
  class ServiceNotImplementedError < StandardError; end
7
7
  class NotAuthorizedError < RuntimeError; end
8
+ class NoResultError < StandardError; end
8
9
  end
9
10
  end
@@ -4,28 +4,28 @@ module TheHelp
4
4
  # Adds a callback DSL to including classes
5
5
  #
6
6
  # @example
7
- # class Foo
8
- # attr_accessor :collaborator
7
+ # class Foo
8
+ # attr_accessor :collaborator
9
9
  #
10
- # def do_something
11
- # collaborator.do_some_other_thing(when_done: callback(:it_was_done))
12
- # end
10
+ # def do_something
11
+ # collaborator.do_some_other_thing(when_done: callback(:it_was_done))
12
+ # end
13
13
  #
14
- # callback(:it_was_done) do |some_arg:|
15
- # puts "Yay! #{some_arg}"
14
+ # callback(:it_was_done) do |some_arg:|
15
+ # puts "Yay! #{some_arg}"
16
+ # end
16
17
  # end
17
- # end
18
18
  #
19
- # class Bar
20
- # def do_some_other_thing(when_done:)
21
- # when_done.call('done by Bar')
22
- # end
23
- # end
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
- # f = Foo.new
26
- # f.collaborator = Bar.new
27
- # f.do_something
28
- # # STDOUT: "Yay! done by Bar"
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
@@ -10,54 +10,92 @@ module TheHelp
10
10
  # application.
11
11
  #
12
12
  # @example
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
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
- # 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))
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
- # callback(:message_sent) do
33
- # # do something really important, I'm sure
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
- # class Authorize < TheHelp::Service
38
- # input :permission
39
- # input :allowed
60
+ # CreateNewUserAccount.(context: current_user, user: new_user_object)
61
+ #
62
+ # @example Calling services with a block
40
63
  #
41
- # authorization_policy allow_all: true
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
- # main do
44
- # if user_has_permission?
45
- # allowed.call
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
- # class SendWelcomeMessage < TheHelp::Service
51
- # input :user
52
- # input :success, default: ->() { }
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
- # main do
55
- # # whatever
56
- # success.call
84
+ # main do
85
+ # self.result = :the_service_result
86
+ # end
57
87
  # end
58
- # end
59
88
  #
60
- # CreateNewUserAccount.(context: current_user, user: new_user_object)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TheHelp
4
- VERSION = '1.1.4'
4
+ VERSION = '1.2.0'
5
5
  end
@@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency 'rake', '~> 10.0'
26
26
  spec.add_development_dependency 'rspec', '~> 3.0'
27
27
  spec.add_development_dependency 'rubocop', '~> 0.50'
28
+ spec.add_development_dependency 'yard'
28
29
  end
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.1.4
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