the_help 2.0.0 → 3.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f11ddd3d1fc5d239938188bda0cba4c3e57d86433fc39f5214dc9ae6aee875af
4
- data.tar.gz: e1a80305b54998dfb4c84e6ec8c1b0c49ccaedc0b21c969becfaca90a671ac60
3
+ metadata.gz: 58c4ca451c960bbd6f54ee05d9a786db32365e8630cd57d1730d21b1e23b4b28
4
+ data.tar.gz: d6b16972071beed84aebb226bf77be94e8e04bdc38ea8eb452170da903287e05
5
5
  SHA512:
6
- metadata.gz: 0af2dbe4d5979ed8d525794f9797c0011a4535ebe7127afd3bddd2a5c11911c0b919c86b8527916a49ee0e29decf662a28c9317cdea935cd8366e1bf56d781e7
7
- data.tar.gz: '019aaca0b3c14e5d96ac5e0f661f5fb9cb8c04ddcbff254ae27f9c380c19a44019037ae80026dbb24604c0a2d09e8b50331f9e94b325515083ab1c67cb083594'
6
+ metadata.gz: 31854f6f5c438280a532d0f9674f10409873164fa240b06716939972d72dd28de48244dc9ab0b160d5714d9c351d5f37da2323cbf33780bcf4c62d23aeadbf2b
7
+ data.tar.gz: 59f1e173753355dbbaec54c1d4411c49184ff769e3a84061c5f81413e634761a9b959b86d8ffe9ff86bc54c3b2b316671bd75f477ee3fe693e7fbf7883ba6671
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- the_help (2.0.0)
4
+ the_help (3.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -28,10 +28,101 @@ them.
28
28
  Make it easier to call a service by including
29
29
  [`TheHelp::ServiceCaller`](lib/the_help/service_caller.rb).
30
30
 
31
+ ### Service Results
32
+
33
+ Every service call will return an instance of `TheHelp::Service::Result`. Your service
34
+ implementation MUST set a result using either the `#success` or the `#error` methods, for example:
35
+
36
+ ```ruby
37
+ class MyService < TheHelp::Service
38
+ authorization_policy allow_all: true
39
+
40
+ input :foo
41
+
42
+ main do
43
+ if foo
44
+ result.success 'bar'
45
+ else
46
+ result.error 'sorry, that did not work'
47
+ end
48
+ end
49
+ end
50
+
51
+ result = MyService.call(context: {}, logger: logger, foo: false)
52
+ result.success?
53
+ #=> false
54
+ result.error?
55
+ #=> true
56
+ result.value
57
+ #=> 'sorry, that did not work'
58
+ result.value!
59
+ # raises the exception TheHelp::Service::ResultError with the message 'sorry, that did not work'
60
+
61
+ result = MyService.call(context: {}, logger: logger, foo: true)
62
+ result.success?
63
+ #=> true
64
+ result.error?
65
+ #=> false
66
+ result.value
67
+ #=> 'bar'
68
+ result.value!
69
+ #=> 'bar'
70
+
71
+ MyService.call(context: {}, logger: logger, foo: true) { |result|
72
+ break 'oops' if result.error?
73
+
74
+ result.value + ' baz'
75
+ }
76
+ #=> 'bar baz'
77
+ ```
78
+
79
+ When using the ServiceCaller interface, unless a block is provided, the `#call_service` will call
80
+ the `TheHelp::Service::Result#value!` method internally, and will either return the succesful
81
+ result value or raise an exception as appropriate.
82
+
83
+ ```ruby
84
+ call_service(MyService, foo: true)
85
+ #=> 'bar'
86
+
87
+ call_service(MyService, foo: false)
88
+ # raises the exception TheHelp::Service::ResultError with the message 'sorry, that did not work'
89
+
90
+ call_service(MyService, foo: true) { |result|
91
+ break 'oops' if result.error?
92
+
93
+ result.value + ' baz'
94
+ }
95
+ #=> 'bar baz'
96
+ ```
97
+
98
+ Finally, you can change the type of the exception that is raised when
99
+ `TheHelp::Service::Result#value!` is called on an error result by providing the exception itself
100
+ as the result value:
101
+
102
+ ```ruby
103
+ class MyService < TheHelp::Service
104
+ authorization_policy allow_all: true
105
+
106
+ input :foo
107
+
108
+ main do
109
+ if foo
110
+ result.success 'bar'
111
+ else
112
+ result.error ArgumentError.new('foo must be true')
113
+ end
114
+ end
115
+ end
116
+
117
+ call_service(MyService, foo: false)
118
+ # raises the exception ArgumentError with the message 'foo must be true'
119
+ ```
120
+
31
121
  ### Running Callbacks
32
122
 
33
- This library encourages you to pass in callbacks to a service rather than
34
- relying on a return value from the service call. For example:
123
+ In some cases a simple success or error result is not sufficient to describe the various results
124
+ about which a service may need to be able to inform its callers. In these cases, a callback style
125
+ of programming can be useful:
35
126
 
36
127
  ```ruby
37
128
  class Foo < TheHelp::Service
@@ -89,8 +180,10 @@ class GetSomeWidgets < TheHelp::Service
89
180
  invalid_customer.call
90
181
  no_widgets_found.call
91
182
  do_some_important_cleanup_for_invalid_customers
183
+ result.error 'invalid customer'
92
184
  else
93
185
  #...
186
+ result.success some_widgets
94
187
  end
95
188
  end
96
189
 
@@ -122,8 +215,10 @@ class GetSomeWidgets < TheHelp::Service
122
215
  run_callback(invalid_customer)
123
216
  run_callback(no_widgets_found)
124
217
  do_some_important_cleanup_for_invalid_customers
218
+ result.error 'invalid customer'
125
219
  else
126
220
  #...
221
+ result.success some_widgets
127
222
  end
128
223
  end
129
224
 
@@ -6,5 +6,6 @@ module TheHelp
6
6
  class ServiceNotImplementedError < StandardError; end
7
7
  class NotAuthorizedError < RuntimeError; end
8
8
  class NoResultError < StandardError; end
9
+ class ResultError < StandardError; end
9
10
  end
10
11
  end
@@ -200,6 +200,16 @@ module TheHelp
200
200
  freeze
201
201
  end
202
202
 
203
+ def value!
204
+ raise TheHelp::NoResultError if pending?
205
+
206
+ raise value if error? && value.is_a?(Exception)
207
+
208
+ raise TheHelp::ResultError.new(value) if error?
209
+
210
+ value
211
+ end
212
+
203
213
  private
204
214
 
205
215
  attr_writer :status, :value
@@ -39,7 +39,9 @@ module TheHelp
39
39
  }.merge(args)
40
40
  service_logger.debug("#{self.class.name}/#{__id__} called service " \
41
41
  "#{service.name}")
42
- service.call(**service_args, &block)
42
+ return service.call(**service_args, &block) if block_given?
43
+
44
+ service.call(**service_args).value!
43
45
  end
44
46
  end
45
47
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TheHelp
4
- VERSION = '2.0.0'
4
+ VERSION = '3.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: 2.0.0
4
+ version: 3.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-10-28 00:00:00.000000000 Z
11
+ date: 2019-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug