u-case 3.0.0.rc3 → 3.0.0.rc4
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/Gemfile +2 -0
- data/README.md +34 -37
- data/lib/micro/case.rb +44 -28
- data/lib/micro/case/error.rb +4 -2
- data/lib/micro/case/result.rb +41 -26
- data/lib/micro/case/safe.rb +2 -2
- data/lib/micro/case/version.rb +1 -1
- data/lib/micro/case/with_activemodel_validation.rb +1 -1
- data/lib/micro/cases/flow.rb +17 -21
- data/lib/micro/cases/safe/flow.rb +2 -2
- 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: b3374584a2cf58a68aa1bed216c91bfd715cc4365d2e7972e5693a8c3acb52eb
|
4
|
+
data.tar.gz: 9bbe75661a2cd53e549cf60692811d47d1aaf5895ee71cf89f3fc7e8f8d72f0b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d43a4e404c554e4e489cd4875af61bf66ab41bec68a84c6022b04b3ae8dd79975f8522e8ecf8c0211ae0f08cbd4fe95ac6d7320a94d0c140ce3c436d26650afb
|
7
|
+
data.tar.gz: '084cabc2b47f424a789f0e0cc9f120bfa50516771e669763207bed6c2884b4f6c3b8731be495eafc5bf1dc1c68b1b82b85a308c4a77cb546d77d99f6a088a827'
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -37,7 +37,8 @@ Version | Documentation
|
|
37
37
|
- [How to define custom result types?](#how-to-define-custom-result-types)
|
38
38
|
- [Is it possible to define a custom result type without a block?](#is-it-possible-to-define-a-custom-result-type-without-a-block)
|
39
39
|
- [How to use the result hooks?](#how-to-use-the-result-hooks)
|
40
|
-
- [Why the
|
40
|
+
- [Why the hook usage without a type exposes the result itself?](#why-the-hook-usage-without-a-type-exposes-the-result-itself)
|
41
|
+
- [Using decomposition to access the result data and type](#using-decomposition-to-access-the-result-data-and-type)
|
41
42
|
- [What happens if a result hook was declared multiple times?](#what-happens-if-a-result-hook-was-declared-multiple-times)
|
42
43
|
- [How to use the `Micro::Case::Result#then` method?](#how-to-use-the-microcaseresultthen-method)
|
43
44
|
- [What does happens when a `Micro::Case::Result#then` receives a block?](#what-does-happens-when-a-microcaseresultthen-receives-a-block)
|
@@ -145,14 +146,6 @@ bad_result = Multiply.call(a: 2, b: '2')
|
|
145
146
|
bad_result.failure? # true
|
146
147
|
bad_result.data # { message: "`a` and `b` attributes must be numeric" }
|
147
148
|
|
148
|
-
#-----------------------------#
|
149
|
-
# Calling a use case instance #
|
150
|
-
#-----------------------------#
|
151
|
-
|
152
|
-
result = Multiply.new(a: 2, b: 3).call
|
153
|
-
|
154
|
-
result.value # { number: 6 }
|
155
|
-
|
156
149
|
# Note:
|
157
150
|
# ----
|
158
151
|
# The result of a Micro::Case.call
|
@@ -360,7 +353,7 @@ Double
|
|
360
353
|
# The use case responsible for the failure will be accessible as the second hook argument
|
361
354
|
```
|
362
355
|
|
363
|
-
#### Why the
|
356
|
+
#### Why the hook usage without a type exposes the result itself?
|
364
357
|
|
365
358
|
Answer: To allow you to define how to handle the program flow using some
|
366
359
|
conditional statement (like an `if`, `case/when`).
|
@@ -377,12 +370,8 @@ class Double < Micro::Case
|
|
377
370
|
end
|
378
371
|
end
|
379
372
|
|
380
|
-
#=================================#
|
381
|
-
# Using the result type and value #
|
382
|
-
#=================================#
|
383
|
-
|
384
373
|
Double
|
385
|
-
.call(-1)
|
374
|
+
.call(number: -1)
|
386
375
|
.on_failure do |result, use_case|
|
387
376
|
case result.type
|
388
377
|
when :invalid then raise TypeError, "number must be a numeric value"
|
@@ -394,19 +383,20 @@ Double
|
|
394
383
|
# The output will be the exception:
|
395
384
|
#
|
396
385
|
# ArgumentError (number `-1` must be greater than 0)
|
386
|
+
```
|
397
387
|
|
398
|
-
|
399
|
-
# Using decomposition to access the result data and type #
|
400
|
-
#=========================================================#
|
388
|
+
> **Note:** The same that was did in the previous examples could be done with `#on_success` hook!
|
401
389
|
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
#
|
406
|
-
|
390
|
+
##### Using decomposition to access the result data and type
|
391
|
+
|
392
|
+
The syntax to decompose an Array can be used in methods, blocks and assigments.
|
393
|
+
If you doesn't know it, check out the [Ruby doc](https://ruby-doc.org/core-2.2.0/doc/syntax/assignment_rdoc.html#label-Array+Decomposition).
|
394
|
+
|
395
|
+
```ruby
|
396
|
+
# The object exposed in the hook is a Micro::Case::Result, and it can be decomposed using this syntax. e.g:
|
407
397
|
|
408
398
|
Double
|
409
|
-
.call(-2)
|
399
|
+
.call(number: -2)
|
410
400
|
.on_failure do |(data, type), use_case|
|
411
401
|
case type
|
412
402
|
when :invalid then raise TypeError, 'number must be a numeric value'
|
@@ -420,6 +410,8 @@ Double
|
|
420
410
|
# ArgumentError (the number `-2` must be greater than 0)
|
421
411
|
```
|
422
412
|
|
413
|
+
> **Note:** The same that was did in the previous examples could be done with `#on_success` hook!
|
414
|
+
|
423
415
|
[⬆️ Back to Top](#table-of-contents-)
|
424
416
|
|
425
417
|
#### What happens if a result hook was declared multiple times?
|
@@ -445,10 +437,11 @@ result[:number] * 4 # 24
|
|
445
437
|
|
446
438
|
accum = 0
|
447
439
|
|
448
|
-
result
|
449
|
-
|
450
|
-
|
451
|
-
|
440
|
+
result
|
441
|
+
.on_success { |result| accum += result[:number] }
|
442
|
+
.on_success { |result| accum += result[:number] }
|
443
|
+
.on_success(:computed) { |result| accum += result[:number] }
|
444
|
+
.on_success(:computed) { |result| accum += result[:number] }
|
452
445
|
|
453
446
|
accum # 24
|
454
447
|
|
@@ -621,9 +614,9 @@ class DoubleAllNumbers < Micro::Case
|
|
621
614
|
Steps::Double
|
622
615
|
end
|
623
616
|
|
624
|
-
DoubleAllNumbers
|
625
|
-
|
626
|
-
|
617
|
+
DoubleAllNumbers.
|
618
|
+
call(numbers: %w[1 1 b 2 3 4]).
|
619
|
+
on_failure { |result| puts result[:message] } # "numbers must contain only numeric types"
|
627
620
|
|
628
621
|
# Note:
|
629
622
|
# ----
|
@@ -705,11 +698,11 @@ DoubleAllNumbersAndSquareAndAdd2 =
|
|
705
698
|
|
706
699
|
SquareAllNumbersAndDouble
|
707
700
|
.call(numbers: %w[1 1 2 2 3 4])
|
708
|
-
.on_success { |
|
701
|
+
.on_success { |result| p result[:numbers] } # [6, 6, 12, 12, 22, 36]
|
709
702
|
|
710
703
|
DoubleAllNumbersAndSquareAndAdd2
|
711
704
|
.call(numbers: %w[1 1 2 2 3 4])
|
712
|
-
.on_success { |
|
705
|
+
.on_success { |result| p result[:numbers] } # [6, 6, 18, 18, 38, 66]
|
713
706
|
```
|
714
707
|
|
715
708
|
Note: You can blend any of the [available syntaxes/approaches](#how-to-create-a-flow-which-has-reusable-steps-to-define-a-complex-use-case) to create use case flows - [examples](https://github.com/serradura/u-case/blob/714c6b658fc6aa02617e6833ddee09eddc760f2a/test/micro/cases/flow/blend_test.rb#L5-L35).
|
@@ -956,8 +949,8 @@ result.type == :exception # true
|
|
956
949
|
result.data # { exception: #<ZeroDivisionError...> }
|
957
950
|
result[:exception].is_a?(ZeroDivisionError) # true
|
958
951
|
|
959
|
-
result.on_failure(:exception) do |
|
960
|
-
AppLogger.error(exception.message) # E, [2019-08-21T00:05:44.195506 #9532] ERROR -- : divided by 0
|
952
|
+
result.on_failure(:exception) do |result|
|
953
|
+
AppLogger.error(result[:exception].message) # E, [2019-08-21T00:05:44.195506 #9532] ERROR -- : divided by 0
|
961
954
|
end
|
962
955
|
|
963
956
|
# Note:
|
@@ -965,8 +958,8 @@ end
|
|
965
958
|
# If you need to handle a specific error,
|
966
959
|
# I recommend the usage of a case statement. e,g:
|
967
960
|
|
968
|
-
result.on_failure(:exception) do |
|
969
|
-
case exception
|
961
|
+
result.on_failure(:exception) do |data, use_case|
|
962
|
+
case exception = data[:exception]
|
970
963
|
when ZeroDivisionError then AppLogger.error(exception.message)
|
971
964
|
else AppLogger.debug("#{use_case.class.name} was the use case responsible for the exception")
|
972
965
|
end
|
@@ -1163,6 +1156,8 @@ class Todo::List::AddItem < Micro::Case
|
|
1163
1156
|
end
|
1164
1157
|
```
|
1165
1158
|
|
1159
|
+
[⬆️ Back to Top](#table-of-contents-)
|
1160
|
+
|
1166
1161
|
## `Micro::Case.config`
|
1167
1162
|
|
1168
1163
|
The idea of this feature is to allow the configuration of some `u-case` features/modules.
|
@@ -1180,6 +1175,8 @@ Micro::Case.config do |config|
|
|
1180
1175
|
end
|
1181
1176
|
```
|
1182
1177
|
|
1178
|
+
[⬆️ Back to Top](#table-of-contents-)
|
1179
|
+
|
1183
1180
|
## Benchmarks
|
1184
1181
|
|
1185
1182
|
### `Micro::Case` (v2.6.0)
|
data/lib/micro/case.rb
CHANGED
@@ -18,26 +18,30 @@ module Micro
|
|
18
18
|
|
19
19
|
include Micro::Attributes.without(:strict_initialize)
|
20
20
|
|
21
|
-
def self.config
|
22
|
-
yield(Config.instance)
|
23
|
-
end
|
24
|
-
|
25
21
|
def self.call(options = {})
|
26
|
-
new(options).
|
22
|
+
new(options).__call__
|
27
23
|
end
|
28
24
|
|
29
25
|
def self.to_proc
|
30
26
|
Proc.new { |arg| call(arg) }
|
31
27
|
end
|
32
28
|
|
33
|
-
def self.call!
|
34
|
-
self
|
35
|
-
end
|
36
|
-
|
37
29
|
def self.flow(*args)
|
38
30
|
@__flow_use_cases = args
|
39
31
|
end
|
40
32
|
|
33
|
+
class << self
|
34
|
+
alias __call__ call
|
35
|
+
|
36
|
+
def config
|
37
|
+
yield(Config.instance)
|
38
|
+
end
|
39
|
+
|
40
|
+
def call!
|
41
|
+
self
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
41
45
|
def self.inherited(subclass)
|
42
46
|
subclass.attributes(self.attributes_data({}))
|
43
47
|
subclass.extend ::Micro::Attributes.const_get('Macros::ForSubclasses'.freeze)
|
@@ -60,32 +64,32 @@ module Micro
|
|
60
64
|
input =
|
61
65
|
arg.is_a?(Hash) ? result.__set_transitions_accessible_attributes__(arg) : arg
|
62
66
|
|
63
|
-
__new__(result, input).
|
67
|
+
__new__(result, input).__call__
|
64
68
|
end
|
65
69
|
|
66
|
-
def self.
|
70
|
+
def self.__flow_builder__
|
67
71
|
Cases::Flow
|
68
72
|
end
|
69
73
|
|
70
|
-
def self.
|
74
|
+
def self.__flow_get__
|
71
75
|
return @__flow if defined?(@__flow)
|
72
76
|
end
|
73
77
|
|
74
78
|
private_class_method def self.__flow_set(args)
|
75
|
-
return if
|
79
|
+
return if __flow_get__
|
76
80
|
|
77
|
-
def self.use_cases;
|
81
|
+
def self.use_cases; __flow_get__.use_cases; end
|
78
82
|
|
79
83
|
self.class_eval('def use_cases; self.class.use_cases; end')
|
80
84
|
|
81
|
-
@__flow =
|
85
|
+
@__flow = __flow_builder__.build(args)
|
82
86
|
end
|
83
87
|
|
84
88
|
FLOW_STEP = 'Flow_Step'.freeze
|
85
89
|
|
86
90
|
private_constant :FLOW_STEP
|
87
91
|
|
88
|
-
def self.
|
92
|
+
def self.__call__!
|
89
93
|
return const_get(FLOW_STEP) if const_defined?(FLOW_STEP, false)
|
90
94
|
|
91
95
|
class_eval("class #{FLOW_STEP} < #{self.name}; private def __call; __call_use_case; end; end")
|
@@ -97,11 +101,11 @@ module Micro
|
|
97
101
|
|
98
102
|
private_class_method def self.__flow_use_cases_get
|
99
103
|
Array(__flow_use_cases)
|
100
|
-
.map { |use_case| use_case == self ? self.
|
104
|
+
.map { |use_case| use_case == self ? self.__call__! : use_case }
|
101
105
|
end
|
102
106
|
|
103
|
-
def self.
|
104
|
-
__flow_set(__flow_use_cases_get) if !
|
107
|
+
def self.__flow_set__!
|
108
|
+
__flow_set(__flow_use_cases_get) if !__flow_get__ && __flow_use_cases
|
105
109
|
end
|
106
110
|
|
107
111
|
def initialize(input)
|
@@ -112,8 +116,8 @@ module Micro
|
|
112
116
|
raise NotImplementedError
|
113
117
|
end
|
114
118
|
|
115
|
-
def
|
116
|
-
__call
|
119
|
+
def __call__
|
120
|
+
__call!
|
117
121
|
end
|
118
122
|
|
119
123
|
def __set_result__(result)
|
@@ -125,15 +129,19 @@ module Micro
|
|
125
129
|
|
126
130
|
private
|
127
131
|
|
132
|
+
# This method was reserved for a new feature
|
133
|
+
def call
|
134
|
+
end
|
135
|
+
|
128
136
|
def __setup_use_case(input)
|
129
|
-
self.class.
|
137
|
+
self.class.__flow_set__!
|
130
138
|
|
131
139
|
@__input = input
|
132
140
|
|
133
141
|
self.attributes = input
|
134
142
|
end
|
135
143
|
|
136
|
-
def __call
|
144
|
+
def __call!
|
137
145
|
return __call_use_case_flow if __call_use_case_flow?
|
138
146
|
|
139
147
|
__call_use_case
|
@@ -144,15 +152,15 @@ module Micro
|
|
144
152
|
|
145
153
|
return result if result.is_a?(Result)
|
146
154
|
|
147
|
-
raise Error::UnexpectedResult.new(self.class)
|
155
|
+
raise Error::UnexpectedResult.new("#{self.class.name}#call!")
|
148
156
|
end
|
149
157
|
|
150
158
|
def __call_use_case_flow?
|
151
|
-
self.class.
|
159
|
+
self.class.__flow_get__
|
152
160
|
end
|
153
161
|
|
154
162
|
def __call_use_case_flow
|
155
|
-
self.class.
|
163
|
+
self.class.__flow_get__.call(@__input)
|
156
164
|
end
|
157
165
|
|
158
166
|
def Success(type = :ok, result: nil)
|
@@ -177,14 +185,22 @@ module Micro
|
|
177
185
|
__get_result(false, value, type)
|
178
186
|
end
|
179
187
|
|
180
|
-
def
|
188
|
+
def __result
|
181
189
|
@__result ||= Result.new
|
182
190
|
end
|
183
191
|
|
184
192
|
def __get_result(is_success, value, type)
|
185
|
-
|
193
|
+
__result.__set__(is_success, value, type, self)
|
186
194
|
end
|
187
195
|
|
188
196
|
private_constant :MapFailureType
|
189
197
|
end
|
198
|
+
|
199
|
+
def self.case?(arg)
|
200
|
+
(arg.is_a?(Class) && arg < Case) || arg.is_a?(Case)
|
201
|
+
end
|
202
|
+
|
203
|
+
def self.case_or_flow?(arg)
|
204
|
+
case?(arg) || arg.is_a?(Cases::Flow)
|
205
|
+
end
|
190
206
|
end
|
data/lib/micro/case/error.rb
CHANGED
@@ -4,9 +4,11 @@ module Micro
|
|
4
4
|
class Case
|
5
5
|
module Error
|
6
6
|
class UnexpectedResult < TypeError
|
7
|
-
MESSAGE = '
|
7
|
+
MESSAGE = 'must return an instance of Micro::Case::Result'.freeze
|
8
8
|
|
9
|
-
def initialize(
|
9
|
+
def initialize(context)
|
10
|
+
super("#{context} #{MESSAGE}")
|
11
|
+
end
|
10
12
|
end
|
11
13
|
|
12
14
|
class ResultIsAlreadyDefined < ArgumentError
|
data/lib/micro/case/result.rb
CHANGED
@@ -11,7 +11,7 @@ module Micro
|
|
11
11
|
|
12
12
|
attr_reader :type, :data, :use_case
|
13
13
|
|
14
|
-
|
14
|
+
alias value data
|
15
15
|
|
16
16
|
def initialize
|
17
17
|
@__transitions__ = []
|
@@ -39,7 +39,7 @@ module Micro
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def on_success(expected_type = nil)
|
42
|
-
return self unless
|
42
|
+
return self unless __success_type?(expected_type)
|
43
43
|
|
44
44
|
hook_data = expected_type.nil? ? self : data
|
45
45
|
|
@@ -49,7 +49,7 @@ module Micro
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def on_failure(expected_type = nil)
|
52
|
-
return self unless
|
52
|
+
return self unless __failure_type?(expected_type)
|
53
53
|
|
54
54
|
hook_data = expected_type.nil? ? self : data
|
55
55
|
|
@@ -59,7 +59,7 @@ module Micro
|
|
59
59
|
end
|
60
60
|
|
61
61
|
def on_exception(expected_exception = nil)
|
62
|
-
return self unless
|
62
|
+
return self unless __failure_type?(:exception)
|
63
63
|
|
64
64
|
if !expected_exception || (Kind.is(Exception, expected_exception) && data.fetch(:exception).is_a?(expected_exception))
|
65
65
|
yield(data, @use_case)
|
@@ -68,27 +68,42 @@ module Micro
|
|
68
68
|
self
|
69
69
|
end
|
70
70
|
|
71
|
-
def then(
|
71
|
+
def then(use_case = nil, attributes = nil, &block)
|
72
72
|
can_yield_self = respond_to?(:yield_self)
|
73
73
|
|
74
74
|
if block
|
75
|
-
raise Error::InvalidInvocationOfTheThenMethod if
|
75
|
+
raise Error::InvalidInvocationOfTheThenMethod if use_case
|
76
76
|
raise NotImplementedError if !can_yield_self
|
77
77
|
|
78
78
|
yield_self(&block)
|
79
79
|
else
|
80
|
-
return yield_self if !
|
80
|
+
return yield_self if !use_case && can_yield_self
|
81
81
|
|
82
|
-
|
82
|
+
if use_case.is_a?(Proc)
|
83
|
+
return failure? ? self : __call_proc(use_case, expected: 'then(-> {})'.freeze)
|
84
|
+
end
|
85
|
+
|
86
|
+
# TODO: Test the then method with a Micro::Cases.{flow,safe_flow}() instance.
|
87
|
+
raise Error::InvalidInvocationOfTheThenMethod unless ::Micro.case_or_flow?(use_case)
|
83
88
|
|
84
89
|
return self if failure?
|
85
90
|
|
86
91
|
input = attributes.is_a?(Hash) ? self.data.merge(attributes) : self.data
|
87
92
|
|
88
|
-
|
93
|
+
use_case.__call_and_set_transition__(self, input)
|
89
94
|
end
|
90
95
|
end
|
91
96
|
|
97
|
+
def |(arg)
|
98
|
+
return self if failure?
|
99
|
+
|
100
|
+
return __call_proc(arg, expected: '| -> {}'.freeze) if arg.is_a?(Proc)
|
101
|
+
|
102
|
+
raise Error::InvalidInvocationOfTheThenMethod unless ::Micro.case_or_flow?(arg)
|
103
|
+
|
104
|
+
failure? ? self : arg.__call_and_set_transition__(self, data)
|
105
|
+
end
|
106
|
+
|
92
107
|
def transitions
|
93
108
|
@__transitions__.clone
|
94
109
|
end
|
@@ -102,7 +117,7 @@ module Micro
|
|
102
117
|
|
103
118
|
def __set__(is_success, data, type, use_case)
|
104
119
|
raise Error::InvalidResultType unless type.is_a?(Symbol)
|
105
|
-
raise Error::InvalidUseCase
|
120
|
+
raise Error::InvalidUseCase unless ::Micro.case?(use_case)
|
106
121
|
|
107
122
|
@success, @type, @use_case = is_success, type, use_case
|
108
123
|
|
@@ -110,7 +125,7 @@ module Micro
|
|
110
125
|
|
111
126
|
raise Micro::Case::Error::InvalidResult.new(is_success, type, use_case) unless @data
|
112
127
|
|
113
|
-
|
128
|
+
__set_transition unless @@transition_tracking_disabled
|
114
129
|
|
115
130
|
self
|
116
131
|
end
|
@@ -118,39 +133,39 @@ module Micro
|
|
118
133
|
def __set_transitions_accessible_attributes__(attributes_data)
|
119
134
|
return attributes_data if @@transition_tracking_disabled
|
120
135
|
|
121
|
-
|
136
|
+
attributes = Utils.symbolize_hash_keys(attributes_data)
|
137
|
+
|
138
|
+
__update_transitions_accessible_attributes(attributes)
|
122
139
|
end
|
123
140
|
|
124
141
|
private
|
125
142
|
|
126
|
-
def
|
127
|
-
|
128
|
-
end
|
143
|
+
def __call_proc(arg, expected:)
|
144
|
+
result = arg.arity.zero? ? arg.call : arg.call(data.clone)
|
129
145
|
|
130
|
-
|
131
|
-
failure? && (expected_type.nil? || expected_type == type)
|
132
|
-
end
|
146
|
+
return result if result.is_a?(Result)
|
133
147
|
|
134
|
-
|
135
|
-
(arg.is_a?(Class) && arg < ::Micro::Case) || arg.is_a?(::Micro::Case)
|
148
|
+
raise Error::UnexpectedResult.new("#{Result.name}##{expected}")
|
136
149
|
end
|
137
150
|
|
138
|
-
def
|
139
|
-
|
151
|
+
def __success_type?(expected_type)
|
152
|
+
success? && (expected_type.nil? || expected_type == type)
|
153
|
+
end
|
140
154
|
|
141
|
-
|
155
|
+
def __failure_type?(expected_type)
|
156
|
+
failure? && (expected_type.nil? || expected_type == type)
|
142
157
|
end
|
143
158
|
|
144
|
-
def
|
159
|
+
def __update_transitions_accessible_attributes(attributes)
|
145
160
|
@__transitions_accessible_attributes__.merge!(attributes)
|
146
161
|
@__transitions_accessible_attributes__
|
147
162
|
end
|
148
163
|
|
149
|
-
def
|
164
|
+
def __set_transition
|
150
165
|
use_case_class = @use_case.class
|
151
166
|
use_case_attributes = Utils.symbolize_hash_keys(@use_case.attributes)
|
152
167
|
|
153
|
-
|
168
|
+
__update_transitions_accessible_attributes(use_case_attributes)
|
154
169
|
|
155
170
|
result = @success ? :success : :failure
|
156
171
|
|
data/lib/micro/case/safe.rb
CHANGED
data/lib/micro/case/version.rb
CHANGED
data/lib/micro/cases/flow.rb
CHANGED
@@ -32,55 +32,51 @@ module Micro
|
|
32
32
|
def call(arg = {})
|
33
33
|
memo = arg.is_a?(Hash) ? arg.dup : {}
|
34
34
|
|
35
|
-
first_result =
|
35
|
+
first_result = __first_use_case_result(arg)
|
36
36
|
|
37
37
|
return first_result if @next_use_cases.empty?
|
38
38
|
|
39
|
-
|
39
|
+
__next_use_cases_result(first_result, memo)
|
40
40
|
end
|
41
41
|
|
42
|
+
alias __call__ call
|
43
|
+
|
42
44
|
def to_proc
|
43
45
|
Proc.new { |arg| call(arg) }
|
44
46
|
end
|
45
47
|
|
46
48
|
private
|
47
49
|
|
48
|
-
def
|
50
|
+
def __is_a_result?(arg)
|
49
51
|
arg.is_a?(Case::Result)
|
50
52
|
end
|
51
53
|
|
52
|
-
def
|
53
|
-
|
54
|
-
return true if arg.is_a?(Class) && arg < ::Micro::Case
|
55
|
-
return false
|
56
|
-
end
|
57
|
-
|
58
|
-
def call_arg(arg)
|
59
|
-
output = arg.call
|
54
|
+
def __call_arg(arg)
|
55
|
+
output = arg.__call__
|
60
56
|
|
61
|
-
|
57
|
+
__is_a_result?(output) ? output.value : output
|
62
58
|
end
|
63
59
|
|
64
|
-
def
|
65
|
-
return
|
66
|
-
return arg.value if
|
60
|
+
def __first_use_case_input(arg)
|
61
|
+
return __call_arg(arg) if ::Micro.case_or_flow?(arg)
|
62
|
+
return arg.value if __is_a_result?(arg)
|
67
63
|
|
68
64
|
arg
|
69
65
|
end
|
70
66
|
|
71
|
-
def
|
72
|
-
input =
|
67
|
+
def __first_use_case_result(arg)
|
68
|
+
input = __first_use_case_input(arg)
|
73
69
|
|
74
70
|
result = Case::Result.new
|
75
71
|
|
76
72
|
@first_use_case.__call_and_set_transition__(result, input)
|
77
73
|
end
|
78
74
|
|
79
|
-
def
|
80
|
-
use_case.__new__(result, input).
|
75
|
+
def __next_use_case_result(use_case, result, input)
|
76
|
+
use_case.__new__(result, input).__call__
|
81
77
|
end
|
82
78
|
|
83
|
-
def
|
79
|
+
def __next_use_cases_result(first_result, memo)
|
84
80
|
@next_use_cases.reduce(first_result) do |result, use_case|
|
85
81
|
break result if result.failure?
|
86
82
|
|
@@ -88,7 +84,7 @@ module Micro
|
|
88
84
|
|
89
85
|
result.__set_transitions_accessible_attributes__(memo)
|
90
86
|
|
91
|
-
|
87
|
+
__next_use_case_result(use_case, result, memo)
|
92
88
|
end
|
93
89
|
end
|
94
90
|
end
|
@@ -4,9 +4,9 @@ module Micro
|
|
4
4
|
module Cases
|
5
5
|
module Safe
|
6
6
|
class Flow < Cases::Flow
|
7
|
-
private def
|
7
|
+
private def __next_use_case_result(use_case, result, input)
|
8
8
|
instance = use_case.__new__(result, input)
|
9
|
-
instance.
|
9
|
+
instance.__call__
|
10
10
|
rescue => exception
|
11
11
|
raise exception if Case::Error.by_wrong_usage?(exception)
|
12
12
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: u-case
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.0.
|
4
|
+
version: 3.0.0.rc4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-08-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kind
|