u-service 0.13.1 → 0.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -6
- data/lib/micro/service/base.rb +5 -5
- data/lib/micro/service/pipeline.rb +1 -1
- data/lib/micro/service/result.rb +22 -4
- data/lib/micro/service/version.rb +1 -1
- data/lib/micro/service/with_validation.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: a511f12c5bc76025f0156c65d934988065f1c51c0d540b67a2cf2f4dcde48f22
|
4
|
+
data.tar.gz: 12489e03b009e02bef85329051952f50866c06f0d585812e55146569b4d535ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0811b8d7893d0cb810345a582b3bb2c2a1dd2c0a21753e512212b9445e6f42bcab601e55ee1b683f2477ec72fa6ee690b741d23f105ffe63d1758dffa66fed50'
|
7
|
+
data.tar.gz: 950935aee3330a885206a85333894c2d27886bb1373ac7151efc95dbb9cdacc1ebe489967dafb1cca0351473460f0122e3d2cbbcea9ca52ea6836995a85de241
|
data/README.md
CHANGED
@@ -183,9 +183,9 @@ module Steps
|
|
183
183
|
end
|
184
184
|
end
|
185
185
|
|
186
|
-
|
186
|
+
#-------------------------------------------------#
|
187
187
|
# Creating a pipeline using the collection syntax #
|
188
|
-
|
188
|
+
#-------------------------------------------------#
|
189
189
|
|
190
190
|
Add2ToAllNumbers = Micro::Service::Pipeline[
|
191
191
|
Steps::ConvertToNumbers,
|
@@ -197,9 +197,9 @@ result = Add2ToAllNumbers.call(numbers: %w[1 1 2 2 3 4])
|
|
197
197
|
p result.success? # true
|
198
198
|
p result.value # {:numbers => [3, 3, 4, 4, 5, 6]}
|
199
199
|
|
200
|
-
|
200
|
+
#-------------------------------------------------------#
|
201
201
|
# An alternative way to create a pipeline using classes #
|
202
|
-
|
202
|
+
#-------------------------------------------------------#
|
203
203
|
|
204
204
|
class DoubleAllNumbers
|
205
205
|
include Micro::Service::Pipeline
|
@@ -211,9 +211,9 @@ DoubleAllNumbers
|
|
211
211
|
.call(numbers: %w[1 1 b 2 3 4])
|
212
212
|
.on_failure { |message| p message } # "numbers must contain only numeric types"
|
213
213
|
|
214
|
-
|
214
|
+
#-----------------------------------------------------------------#
|
215
215
|
# Another way to create a pipeline using the composition operator #
|
216
|
-
|
216
|
+
#-----------------------------------------------------------------#
|
217
217
|
|
218
218
|
SquareAllNumbers =
|
219
219
|
Steps::ConvertToNumbers >> Steps::Square
|
@@ -221,6 +221,21 @@ SquareAllNumbers =
|
|
221
221
|
SquareAllNumbers
|
222
222
|
.call(numbers: %w[1 1 2 2 3 4])
|
223
223
|
.on_success { |value| p value[:numbers] } # [1, 1, 4, 4, 9, 16]
|
224
|
+
|
225
|
+
#=================================================================#
|
226
|
+
# Attention: #
|
227
|
+
# When happening a failure, the service object responsible for it #
|
228
|
+
# will be accessible in the result #
|
229
|
+
#=================================================================#
|
230
|
+
|
231
|
+
result = SquareAllNumbers.call(numbers: %w[1 1 b 2 3 4])
|
232
|
+
|
233
|
+
result.failure? # true
|
234
|
+
result.service.is_a?(Steps::ConvertToNumbers) # true
|
235
|
+
|
236
|
+
result.on_failure do |_message, service|
|
237
|
+
puts "#{service.class.name} was the service responsible by the failure" } # Steps::ConvertToNumbers was the service responsible by the failure
|
238
|
+
end
|
224
239
|
```
|
225
240
|
|
226
241
|
### What is a strict Service Object?
|
data/lib/micro/service/base.rb
CHANGED
@@ -5,7 +5,7 @@ module Micro
|
|
5
5
|
class Base
|
6
6
|
include Micro::Attributes.without(:strict_initialize)
|
7
7
|
|
8
|
-
UNEXPECTED_RESULT = '#call! must return
|
8
|
+
UNEXPECTED_RESULT = '#call! must return an instance of Micro::Service::Result'.freeze
|
9
9
|
InvalidResultInstance = ArgumentError.new('argument must be an instance of Micro::Service::Result'.freeze)
|
10
10
|
ResultIsAlreadyDefined = ArgumentError.new('result is already defined'.freeze)
|
11
11
|
|
@@ -53,13 +53,13 @@ module Micro
|
|
53
53
|
end
|
54
54
|
|
55
55
|
def Success(arg = :ok)
|
56
|
-
|
57
|
-
__get_result__.__set__(true, value, type)
|
56
|
+
value, type = block_given? ? [yield, arg] : [arg, :ok]
|
57
|
+
__get_result__.__set__(true, value, type, nil)
|
58
58
|
end
|
59
59
|
|
60
60
|
def Failure(arg = :error)
|
61
|
-
|
62
|
-
__get_result__.__set__(false, value, type)
|
61
|
+
value, type = block_given? ? [yield, arg] : [arg, :error]
|
62
|
+
__get_result__.__set__(false, value, type, self)
|
63
63
|
end
|
64
64
|
end
|
65
65
|
end
|
data/lib/micro/service/result.rb
CHANGED
@@ -3,14 +3,22 @@
|
|
3
3
|
module Micro
|
4
4
|
module Service
|
5
5
|
class Result
|
6
|
-
InvalidType = TypeError.new('type must be a
|
6
|
+
InvalidType = TypeError.new('type must be a Symbol'.freeze)
|
7
|
+
InvalidService = TypeError.new('service must be a kind or an instance of Micro::Service::Base'.freeze)
|
8
|
+
|
9
|
+
class InvalidAccessToTheServiceObject < StandardError
|
10
|
+
MSG = 'only a failure result can access its service object'.freeze
|
11
|
+
|
12
|
+
def initialize(message = MSG); super; end
|
13
|
+
end
|
7
14
|
|
8
15
|
attr_reader :value, :type
|
9
16
|
|
10
|
-
def __set__(is_success, value, type)
|
17
|
+
def __set__(is_success, value, type, service)
|
11
18
|
raise InvalidType unless type.is_a?(Symbol)
|
19
|
+
raise InvalidService if !is_success && !is_a_service?(service)
|
12
20
|
|
13
|
-
@success, @value, @type = is_success, value, type
|
21
|
+
@success, @value, @type, @service = is_success, value, type, service
|
14
22
|
|
15
23
|
self
|
16
24
|
end
|
@@ -23,12 +31,18 @@ module Micro
|
|
23
31
|
!success?
|
24
32
|
end
|
25
33
|
|
34
|
+
def service
|
35
|
+
return @service if failure?
|
36
|
+
|
37
|
+
raise InvalidAccessToTheServiceObject
|
38
|
+
end
|
39
|
+
|
26
40
|
def on_success(arg = :ok)
|
27
41
|
self.tap { yield(value) if success_type?(arg) }
|
28
42
|
end
|
29
43
|
|
30
44
|
def on_failure(arg = :error)
|
31
|
-
self.tap{ yield(value) if failure_type?(arg) }
|
45
|
+
self.tap{ yield(value, @service) if failure_type?(arg) }
|
32
46
|
end
|
33
47
|
|
34
48
|
private
|
@@ -40,6 +54,10 @@ module Micro
|
|
40
54
|
def failure_type?(arg)
|
41
55
|
failure? && (arg == :error || arg == type)
|
42
56
|
end
|
57
|
+
|
58
|
+
def is_a_service?(arg)
|
59
|
+
(arg.is_a?(Class) && arg < Service::Base) || arg.is_a?(Service::Base)
|
60
|
+
end
|
43
61
|
end
|
44
62
|
end
|
45
63
|
end
|
@@ -8,7 +8,7 @@ module Micro
|
|
8
8
|
include Micro::Attributes::Features::ActiveModelValidations
|
9
9
|
|
10
10
|
def call
|
11
|
-
return Failure(:validation_error) {
|
11
|
+
return Failure(:validation_error) { self.errors } unless valid?
|
12
12
|
|
13
13
|
__call
|
14
14
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: u-service
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.14.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-08-
|
11
|
+
date: 2019-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: u-attributes
|