the_help 2.0.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +97 -2
- data/lib/the_help/errors.rb +1 -0
- data/lib/the_help/service.rb +10 -0
- data/lib/the_help/service_caller.rb +3 -1
- data/lib/the_help/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58c4ca451c960bbd6f54ee05d9a786db32365e8630cd57d1730d21b1e23b4b28
|
4
|
+
data.tar.gz: d6b16972071beed84aebb226bf77be94e8e04bdc38ea8eb452170da903287e05
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31854f6f5c438280a532d0f9674f10409873164fa240b06716939972d72dd28de48244dc9ab0b160d5714d9c351d5f37da2323cbf33780bcf4c62d23aeadbf2b
|
7
|
+
data.tar.gz: 59f1e173753355dbbaec54c1d4411c49184ff769e3a84061c5f81413e634761a9b959b86d8ffe9ff86bc54c3b2b316671bd75f477ee3fe693e7fbf7883ba6671
|
data/Gemfile.lock
CHANGED
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
|
-
|
34
|
-
|
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
|
|
data/lib/the_help/errors.rb
CHANGED
data/lib/the_help/service.rb
CHANGED
@@ -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
|
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: 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-
|
11
|
+
date: 2019-11-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: byebug
|