u-case 4.0.0 → 4.1.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 +4 -4
- data/README.md +6 -4
- data/README.pt-BR.md +4 -3
- data/lib/micro/case.rb +39 -15
- data/lib/micro/case/result.rb +36 -6
- data/lib/micro/case/result/transitions.rb +0 -1
- data/lib/micro/case/result/wrapper.rb +45 -0
- data/lib/micro/case/safe.rb +1 -1
- data/lib/micro/case/utils.rb +19 -10
- data/lib/micro/case/version.rb +1 -1
- data/lib/micro/cases.rb +7 -0
- data/lib/micro/cases/error.rb +13 -0
- data/lib/micro/cases/flow.rb +23 -14
- data/lib/micro/cases/map.rb +39 -0
- data/lib/micro/cases/safe/flow.rb +2 -2
- data/lib/micro/cases/utils.rb +21 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e43c3e14dcb10002feb94f83f7d9289b49164e5908f3e8b58fa64e62d69796db
|
4
|
+
data.tar.gz: 9f3a37d02e5e1c1369f74e8cdf14cc3038fad55df5db08bd1a330dd7004e5ba1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 744d7c78a228a3ae9197762f9e259dd72ab2f1b8c20af968fca4cbe46931c703abd9aa051c4aa0669b61f51b14d42a990834dc4fc34dc2f60a9f20d68867fbab
|
7
|
+
data.tar.gz: 7da45bc3752296dd000bf016ef35ff7c993d0ea5c2218e43e6f1d492ec7d41f457e7048cf8b281d0ecb6f40efe05df19d07809d69db3a7a41df02f10028a7e68
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
</p>
|
7
7
|
|
8
8
|
<p align="center">
|
9
|
-
<img src="https://img.shields.io/badge/ruby
|
9
|
+
<img src="https://img.shields.io/badge/ruby->%3D%202.2.0-ruby.svg?colorA=99004d&colorB=cc0066" alt="Ruby">
|
10
10
|
|
11
11
|
<a href="https://rubygems.org/gems/u-case">
|
12
12
|
<img alt="Gem" src="https://img.shields.io/gem/v/u-case.svg?style=flat-square">
|
@@ -38,7 +38,8 @@ The main project goals are:
|
|
38
38
|
|
39
39
|
Version | Documentation
|
40
40
|
--------- | -------------
|
41
|
-
|
41
|
+
unreleased| https://github.com/serradura/u-case/blob/main/README.md
|
42
|
+
4.1.0 | https://github.com/serradura/u-case/blob/v4.x/README.md
|
42
43
|
3.1.0 | https://github.com/serradura/u-case/blob/v3.x/README.md
|
43
44
|
2.6.0 | https://github.com/serradura/u-case/blob/v2.x/README.md
|
44
45
|
1.1.0 | https://github.com/serradura/u-case/blob/v1.x/README.md
|
@@ -100,7 +101,8 @@ Version | Documentation
|
|
100
101
|
|
101
102
|
| u-case | branch | ruby | activemodel | u-attributes |
|
102
103
|
| -------------- | ------- | -------- | ------------- | ------------ |
|
103
|
-
|
|
104
|
+
| unreleased | main | >= 2.2.0 | >= 3.2, < 6.1 | ~> 2.0 |
|
105
|
+
| 4.1.0 | v4.x | >= 2.2.0 | >= 3.2, < 6.1 | ~> 2.0 |
|
104
106
|
| 3.1.0 | v3.x | >= 2.2.0 | >= 3.2, < 6.1 | ~> 1.1 |
|
105
107
|
| 2.6.0 | v2.x | >= 2.2.0 | >= 3.2, < 6.1 | ~> 1.1 |
|
106
108
|
| 1.1.0 | v1.x | >= 2.2.0 | >= 3.2, < 6.1 | ~> 1.1 |
|
@@ -124,7 +126,7 @@ Version | Documentation
|
|
124
126
|
Add this line to your application's Gemfile:
|
125
127
|
|
126
128
|
```ruby
|
127
|
-
gem 'u-case', '~>
|
129
|
+
gem 'u-case', '~> 4.1.0'
|
128
130
|
```
|
129
131
|
|
130
132
|
And then execute:
|
data/README.pt-BR.md
CHANGED
@@ -38,7 +38,8 @@ Principais objetivos deste projeto:
|
|
38
38
|
|
39
39
|
Versão | Documentação
|
40
40
|
--------- | -------------
|
41
|
-
|
41
|
+
unreleased| https://github.com/serradura/u-case/blob/main/README.md
|
42
|
+
4.1.0 | https://github.com/serradura/u-case/blob/v4.x/README.md
|
42
43
|
3.1.0 | https://github.com/serradura/u-case/blob/v3.x/README.md
|
43
44
|
2.6.0 | https://github.com/serradura/u-case/blob/v2.x/README.md
|
44
45
|
1.1.0 | https://github.com/serradura/u-case/blob/v1.x/README.md
|
@@ -99,7 +100,7 @@ Versão | Documentação
|
|
99
100
|
|
100
101
|
| u-case | branch | ruby | activemodel | u-attributes |
|
101
102
|
| -------------- | ------- | -------- | ------------- | ------------ |
|
102
|
-
| 4.
|
103
|
+
| 4.1.0 | main | >= 2.2.0 | >= 3.2, < 6.1 | ~> 2.0 |
|
103
104
|
| 3.1.0 | v3.x | >= 2.2.0 | >= 3.2, < 6.1 | ~> 1.1 |
|
104
105
|
| 2.6.0 | v2.x | >= 2.2.0 | >= 3.2, < 6.1 | ~> 1.1 |
|
105
106
|
| 1.1.0 | v1.x | >= 2.2.0 | >= 3.2, < 6.1 | ~> 1.1 |
|
@@ -123,7 +124,7 @@ Versão | Documentação
|
|
123
124
|
Adicione essa linha ao Gemfile da sua aplicação:
|
124
125
|
|
125
126
|
```ruby
|
126
|
-
gem 'u-case', '~>
|
127
|
+
gem 'u-case', '~> 4.1.0'
|
127
128
|
```
|
128
129
|
|
129
130
|
E então execute:
|
data/lib/micro/case.rb
CHANGED
@@ -7,6 +7,7 @@ require 'micro/case/version'
|
|
7
7
|
|
8
8
|
module Micro
|
9
9
|
class Case
|
10
|
+
require 'micro/cases/utils'
|
10
11
|
require 'micro/case/utils'
|
11
12
|
require 'micro/case/error'
|
12
13
|
require 'micro/case/result'
|
@@ -16,10 +17,14 @@ module Micro
|
|
16
17
|
|
17
18
|
require 'micro/cases'
|
18
19
|
|
19
|
-
include Micro::Attributes
|
20
|
+
include Micro::Attributes
|
20
21
|
|
21
|
-
def self.call(
|
22
|
-
new
|
22
|
+
def self.call(input = Kind::Empty::HASH)
|
23
|
+
result = __new__(Result.new, input).__call__
|
24
|
+
|
25
|
+
return result unless block_given?
|
26
|
+
|
27
|
+
yield Result::Wrapper.new(result)
|
23
28
|
end
|
24
29
|
|
25
30
|
INVALID_INVOCATION_OF_THE_THEN_METHOD =
|
@@ -47,7 +52,7 @@ module Micro
|
|
47
52
|
end
|
48
53
|
|
49
54
|
def self.flow(*args)
|
50
|
-
@__flow_use_cases = args
|
55
|
+
@__flow_use_cases = Cases::Utils.map_use_cases(args)
|
51
56
|
end
|
52
57
|
|
53
58
|
class << self
|
@@ -81,6 +86,8 @@ module Micro
|
|
81
86
|
new(input).__set_result__(result)
|
82
87
|
end
|
83
88
|
|
89
|
+
private_class_method :new
|
90
|
+
|
84
91
|
def self.__flow_builder__
|
85
92
|
Cases::Flow
|
86
93
|
end
|
@@ -122,6 +129,14 @@ module Micro
|
|
122
129
|
__flow_set(__flow_use_cases_get) if !__flow_get__ && __flow_use_cases
|
123
130
|
end
|
124
131
|
|
132
|
+
def self.inspect
|
133
|
+
if __flow_use_cases
|
134
|
+
'<%s (%s) use_cases=%s>' % [self, __flow_builder__, @__flow_use_cases]
|
135
|
+
else
|
136
|
+
'<%s (%s) attributes=%s>' % [self, self.superclass, attributes]
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
125
140
|
def initialize(input)
|
126
141
|
__setup_use_case(input)
|
127
142
|
end
|
@@ -131,7 +146,7 @@ module Micro
|
|
131
146
|
end
|
132
147
|
|
133
148
|
def __call__
|
134
|
-
|
149
|
+
__call_the_use_case_or_its_flow
|
135
150
|
end
|
136
151
|
|
137
152
|
def __set_result__(result)
|
@@ -145,12 +160,21 @@ module Micro
|
|
145
160
|
|
146
161
|
private
|
147
162
|
|
163
|
+
def call(use_case, defaults = Kind::Empty::HASH)
|
164
|
+
raise Error::InvalidUseCase unless ::Micro.case_or_flow?(use_case)
|
165
|
+
|
166
|
+
input =
|
167
|
+
defaults.empty? ? attributes : attributes.merge(Utils::Hashes.stringify_keys(defaults))
|
168
|
+
|
169
|
+
use_case.__new__(@__result, input).__call__
|
170
|
+
end
|
171
|
+
|
148
172
|
def apply(name)
|
149
173
|
method(name)
|
150
174
|
end
|
151
175
|
|
152
|
-
def
|
153
|
-
return
|
176
|
+
def __call_the_use_case_or_its_flow
|
177
|
+
return __call_the_use_case_flow if __call_the_use_case_flow?
|
154
178
|
|
155
179
|
__call_use_case
|
156
180
|
end
|
@@ -171,11 +195,11 @@ module Micro
|
|
171
195
|
raise Error::UnexpectedResult.new("#{self.class.name}#call!")
|
172
196
|
end
|
173
197
|
|
174
|
-
def
|
198
|
+
def __call_the_use_case_flow?
|
175
199
|
self.class.__flow_get__
|
176
200
|
end
|
177
201
|
|
178
|
-
def
|
202
|
+
def __call_the_use_case_flow
|
179
203
|
self.class.__flow_get__.call(@__input)
|
180
204
|
end
|
181
205
|
|
@@ -201,18 +225,18 @@ module Micro
|
|
201
225
|
__get_result(false, value, type)
|
202
226
|
end
|
203
227
|
|
204
|
-
def __result
|
205
|
-
@__result ||= Result.new
|
206
|
-
end
|
207
|
-
|
208
228
|
def __get_result(is_success, value, type)
|
209
|
-
__result.__set__(is_success, value, type, self)
|
229
|
+
@__result.__set__(is_success, value, type, self)
|
210
230
|
end
|
211
231
|
|
212
232
|
private_constant :MapFailureType, :INVALID_INVOCATION_OF_THE_THEN_METHOD
|
213
233
|
end
|
214
234
|
|
235
|
+
def self.case?(arg)
|
236
|
+
arg.is_a?(Class) && arg < Case
|
237
|
+
end
|
238
|
+
|
215
239
|
def self.case_or_flow?(arg)
|
216
|
-
|
240
|
+
case?(arg) || arg.is_a?(Cases::Flow)
|
217
241
|
end
|
218
242
|
end
|
data/lib/micro/case/result.rb
CHANGED
@@ -5,6 +5,7 @@ require 'set'
|
|
5
5
|
module Micro
|
6
6
|
class Case
|
7
7
|
class Result
|
8
|
+
require 'micro/case/result/wrapper'
|
8
9
|
require 'micro/case/result/transitions'
|
9
10
|
|
10
11
|
Kind::Types.add(self)
|
@@ -23,15 +24,26 @@ module Micro
|
|
23
24
|
alias value data
|
24
25
|
|
25
26
|
def initialize(transitions_mapper = Transitions::MapEverything)
|
27
|
+
enable_transitions = @@transitions_enabled
|
28
|
+
|
29
|
+
@__is_unknown = true
|
26
30
|
@__accumulated_data = {}
|
31
|
+
@__tracked_use_cases = Set.new
|
27
32
|
@__accessible_attributes = {}
|
28
33
|
|
29
|
-
enable_transitions = @@transitions_enabled
|
30
|
-
|
31
34
|
@__transitions = enable_transitions ? [] : Kind::Empty::ARRAY
|
32
35
|
@__transitions_mapper = transitions_mapper if enable_transitions
|
33
36
|
end
|
34
37
|
|
38
|
+
def inspect
|
39
|
+
pretty_type = @__success ? 'Success' : 'Failure'
|
40
|
+
|
41
|
+
instance_info = '%s (%s) type=:%s data=%s' % [pretty_type, self.class, @type, data]
|
42
|
+
transitions_info = ' transitions=%d' % [@__transitions.size] if Micro::Case::Result.transitions_enabled?
|
43
|
+
|
44
|
+
"<#{instance_info}#{transitions_info}>"
|
45
|
+
end
|
46
|
+
|
35
47
|
def to_ary
|
36
48
|
[data, type]
|
37
49
|
end
|
@@ -57,7 +69,7 @@ module Micro
|
|
57
69
|
end
|
58
70
|
|
59
71
|
def slice(*keys)
|
60
|
-
Utils.
|
72
|
+
Utils::Hashes.slice(data, keys)
|
61
73
|
end
|
62
74
|
|
63
75
|
def success?
|
@@ -68,6 +80,10 @@ module Micro
|
|
68
80
|
!success?
|
69
81
|
end
|
70
82
|
|
83
|
+
def unknown?
|
84
|
+
@__is_unknown
|
85
|
+
end
|
86
|
+
|
71
87
|
def accessible_attributes
|
72
88
|
@__accessible_attributes.keys
|
73
89
|
end
|
@@ -75,6 +91,7 @@ module Micro
|
|
75
91
|
def on_success(expected_type = nil)
|
76
92
|
return self unless __success_type?(expected_type)
|
77
93
|
|
94
|
+
@__is_unknown = false
|
78
95
|
hook_data = expected_type.nil? ? self : data
|
79
96
|
|
80
97
|
yield(hook_data, @use_case)
|
@@ -85,6 +102,7 @@ module Micro
|
|
85
102
|
def on_failure(expected_type = nil)
|
86
103
|
return self unless __failure_type?(expected_type)
|
87
104
|
|
105
|
+
@__is_unknown = false
|
88
106
|
hook_data = expected_type.nil? ? self : data
|
89
107
|
|
90
108
|
yield(hook_data, @use_case)
|
@@ -102,6 +120,14 @@ module Micro
|
|
102
120
|
self
|
103
121
|
end
|
104
122
|
|
123
|
+
def on_unknown
|
124
|
+
return self unless unknown?
|
125
|
+
|
126
|
+
yield(self, @use_case)
|
127
|
+
|
128
|
+
self
|
129
|
+
end
|
130
|
+
|
105
131
|
def then(use_case = nil, attributes = nil, &block)
|
106
132
|
can_yield_self = respond_to?(:yield_self)
|
107
133
|
|
@@ -163,9 +189,13 @@ module Micro
|
|
163
189
|
|
164
190
|
@__accumulated_data.merge!(@data)
|
165
191
|
|
166
|
-
use_case_attributes = Utils.
|
192
|
+
use_case_attributes = Utils::Hashes.symbolize_keys(@use_case.attributes)
|
167
193
|
|
168
|
-
|
194
|
+
unless @__tracked_use_cases.member?(use_case_class = @use_case.class)
|
195
|
+
@__tracked_use_cases.add(use_case_class)
|
196
|
+
|
197
|
+
__update_accessible_attributes(use_case_attributes)
|
198
|
+
end
|
169
199
|
|
170
200
|
__set_transition(use_case_attributes) unless @__transitions.frozen?
|
171
201
|
|
@@ -175,7 +205,7 @@ module Micro
|
|
175
205
|
def __set_accessible_attributes__(arg)
|
176
206
|
return arg unless arg.is_a?(Hash)
|
177
207
|
|
178
|
-
attributes = Utils.
|
208
|
+
attributes = Utils::Hashes.symbolize_keys(arg)
|
179
209
|
|
180
210
|
__update_accessible_attributes(attributes)
|
181
211
|
__fetch_accessible_attributes
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Micro
|
4
|
+
class Case
|
5
|
+
class Result
|
6
|
+
class Wrapper
|
7
|
+
def initialize(result)
|
8
|
+
@__is_unknown = true
|
9
|
+
|
10
|
+
@result = result
|
11
|
+
end
|
12
|
+
|
13
|
+
def failure(type = nil)
|
14
|
+
return if @result.success?
|
15
|
+
|
16
|
+
if result_type?(type)
|
17
|
+
@__is_unknown = false
|
18
|
+
|
19
|
+
yield(@result)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def success(type = nil)
|
24
|
+
return if @result.failure?
|
25
|
+
|
26
|
+
if result_type?(type)
|
27
|
+
@__is_unknown = false
|
28
|
+
|
29
|
+
yield(@result)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def unknown
|
34
|
+
return yield(@result) if @__is_unknown
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def result_type?(type)
|
40
|
+
type.nil? || @result.type == type
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/micro/case/safe.rb
CHANGED
data/lib/micro/case/utils.rb
CHANGED
@@ -3,25 +3,34 @@
|
|
3
3
|
module Micro
|
4
4
|
class Case
|
5
5
|
module Utils
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
|
7
|
+
module Hashes
|
8
|
+
def self.respond_to?(hash, method)
|
9
|
+
Kind.of(Hash, hash).respond_to?(method)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.symbolize_keys(hash)
|
13
|
+
return hash.transform_keys { |key| key.to_sym rescue key } if respond_to?(hash, :transform_keys)
|
14
|
+
|
10
15
|
hash.each_with_object({}) do |(k, v), memo|
|
11
16
|
key = k.to_sym rescue k
|
12
|
-
|
13
17
|
memo[key] = v
|
14
18
|
end
|
15
19
|
end
|
16
|
-
end
|
17
20
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
21
|
+
def self.stringify_keys(hash)
|
22
|
+
return hash.transform_keys(&:to_s) if respond_to?(hash, :transform_keys)
|
23
|
+
|
24
|
+
hash.each_with_object({}) { |(k, v), memo| memo[k.to_s] = v }
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.slice(hash, keys)
|
28
|
+
return hash.slice(*keys) if respond_to?(hash, :slice)
|
29
|
+
|
22
30
|
hash.select { |key, _value| keys.include?(key) }
|
23
31
|
end
|
24
32
|
end
|
33
|
+
|
25
34
|
end
|
26
35
|
end
|
27
36
|
end
|
data/lib/micro/case/version.rb
CHANGED
data/lib/micro/cases.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'micro/cases/utils'
|
4
|
+
require 'micro/cases/error'
|
3
5
|
require 'micro/cases/flow'
|
4
6
|
require 'micro/cases/safe/flow'
|
7
|
+
require 'micro/cases/map'
|
5
8
|
|
6
9
|
module Micro
|
7
10
|
module Cases
|
@@ -12,5 +15,9 @@ module Micro
|
|
12
15
|
def self.safe_flow(args)
|
13
16
|
Safe::Flow.build(args)
|
14
17
|
end
|
18
|
+
|
19
|
+
def self.map(args)
|
20
|
+
Map.build(args)
|
21
|
+
end
|
15
22
|
end
|
16
23
|
end
|
data/lib/micro/cases/flow.rb
CHANGED
@@ -3,20 +3,15 @@
|
|
3
3
|
module Micro
|
4
4
|
module Cases
|
5
5
|
class Flow
|
6
|
-
|
7
|
-
|
8
|
-
end
|
6
|
+
IsAUseCaseWithDefaults = -> arg { arg.is_a?(Array) && Micro.case?(arg[0]) && arg[1].is_a?(Hash) }
|
7
|
+
IsAValidUseCase = -> use_case { Micro.case?(use_case) || IsAUseCaseWithDefaults[use_case] }
|
9
8
|
|
10
9
|
attr_reader :use_cases
|
11
10
|
|
12
|
-
def self.map_use_cases(arg)
|
13
|
-
arg.is_a?(Flow) ? arg.use_cases : Array(arg)
|
14
|
-
end
|
15
|
-
|
16
11
|
def self.build(args)
|
17
|
-
use_cases =
|
12
|
+
use_cases = Utils.map_use_cases(args)
|
18
13
|
|
19
|
-
raise InvalidUseCases if use_cases.
|
14
|
+
raise Error::InvalidUseCases if use_cases.none?(&IsAValidUseCase)
|
20
15
|
|
21
16
|
new(use_cases)
|
22
17
|
end
|
@@ -27,8 +22,12 @@ module Micro
|
|
27
22
|
@first = @next_ones.shift
|
28
23
|
end
|
29
24
|
|
25
|
+
def inspect
|
26
|
+
'<(%s) use_cases=%s>' % [self.class, @use_cases]
|
27
|
+
end
|
28
|
+
|
30
29
|
def call!(input:, result:)
|
31
|
-
first_result =
|
30
|
+
first_result = __call_use_case(@first, result, input)
|
32
31
|
|
33
32
|
return first_result if @next_ones.empty?
|
34
33
|
|
@@ -36,7 +35,11 @@ module Micro
|
|
36
35
|
end
|
37
36
|
|
38
37
|
def call(input = Kind::Empty::HASH)
|
39
|
-
call!(input: input, result: Case::Result.new)
|
38
|
+
result = call!(input: input, result: Case::Result.new)
|
39
|
+
|
40
|
+
return result unless block_given?
|
41
|
+
|
42
|
+
yield ::Micro::Case::Result::Wrapper.new(result)
|
40
43
|
end
|
41
44
|
|
42
45
|
alias __call__ call
|
@@ -68,17 +71,23 @@ module Micro
|
|
68
71
|
raise Case::Error::InvalidInvocationOfTheThenMethod.new("#{self.class.name}#")
|
69
72
|
end
|
70
73
|
|
71
|
-
def
|
72
|
-
use_case
|
74
|
+
def __call_use_case(use_case, result, input)
|
75
|
+
__build_use_case(use_case, result, input).__call__
|
73
76
|
end
|
74
77
|
|
75
78
|
def __call_next_use_cases(first_result)
|
76
79
|
@next_ones.reduce(first_result) do |result, use_case|
|
77
80
|
break result if result.failure?
|
78
81
|
|
79
|
-
|
82
|
+
__call_use_case(use_case, result, result.data)
|
80
83
|
end
|
81
84
|
end
|
85
|
+
|
86
|
+
def __build_use_case(use_case, result, input)
|
87
|
+
return use_case.__new__(result, input) unless use_case.is_a?(Array)
|
88
|
+
|
89
|
+
use_case[0].__new__(result, input.merge(use_case[1]))
|
90
|
+
end
|
82
91
|
end
|
83
92
|
end
|
84
93
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Micro
|
4
|
+
module Cases
|
5
|
+
class Map
|
6
|
+
IsAUseCaseOrFlowWithDefaults = -> arg { arg.is_a?(Array) && Micro.case_or_flow?(arg[0]) && arg[1].is_a?(Hash) }
|
7
|
+
IsAUseCaseOrFlow = -> arg { Micro.case_or_flow?(arg) || IsAUseCaseOrFlowWithDefaults[arg] }
|
8
|
+
HasValidArgs = -> (args) { Kind.of(Array, args).all?(&IsAUseCaseOrFlow) }
|
9
|
+
|
10
|
+
attr_reader :use_cases
|
11
|
+
|
12
|
+
def self.build(args)
|
13
|
+
raise Error::InvalidUseCases unless HasValidArgs[args]
|
14
|
+
|
15
|
+
new(args)
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(use_cases)
|
19
|
+
@use_cases = use_cases
|
20
|
+
end
|
21
|
+
|
22
|
+
GetUseCaseResult = -> (hash) do
|
23
|
+
-> (use_case) do
|
24
|
+
return use_case.call(hash) unless use_case.is_a?(Array)
|
25
|
+
|
26
|
+
use_case[0].call(hash.merge(use_case[1]))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def call(arg = {})
|
31
|
+
hash = Kind.of(Hash, arg)
|
32
|
+
|
33
|
+
use_cases.map(&GetUseCaseResult[hash])
|
34
|
+
end
|
35
|
+
|
36
|
+
private_constant :HasValidArgs, :IsAUseCaseOrFlow, :IsAUseCaseOrFlowWithDefaults, :GetUseCaseResult
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -4,8 +4,8 @@ module Micro
|
|
4
4
|
module Cases
|
5
5
|
module Safe
|
6
6
|
class Flow < Cases::Flow
|
7
|
-
private def
|
8
|
-
instance = use_case
|
7
|
+
private def __call_use_case(use_case, result, input)
|
8
|
+
instance = __build_use_case(use_case, result, input)
|
9
9
|
instance.__call__
|
10
10
|
rescue => exception
|
11
11
|
raise exception if Case::Error.by_wrong_usage?(exception)
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Micro
|
4
|
+
module Cases
|
5
|
+
|
6
|
+
module Utils
|
7
|
+
def self.map_use_cases(args)
|
8
|
+
collection = args.is_a?(Array) && args.size == 1 ? args[0] : args
|
9
|
+
|
10
|
+
Array(collection).each_with_object([]) do |arg, memo|
|
11
|
+
if arg.is_a?(Flow)
|
12
|
+
arg.use_cases.each { |use_case| memo << use_case }
|
13
|
+
else
|
14
|
+
memo << arg
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
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: 4.
|
4
|
+
version: 4.1.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: 2020-
|
11
|
+
date: 2020-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kind
|
@@ -97,14 +97,18 @@ files:
|
|
97
97
|
- lib/micro/case/error.rb
|
98
98
|
- lib/micro/case/result.rb
|
99
99
|
- lib/micro/case/result/transitions.rb
|
100
|
+
- lib/micro/case/result/wrapper.rb
|
100
101
|
- lib/micro/case/safe.rb
|
101
102
|
- lib/micro/case/strict.rb
|
102
103
|
- lib/micro/case/utils.rb
|
103
104
|
- lib/micro/case/version.rb
|
104
105
|
- lib/micro/case/with_activemodel_validation.rb
|
105
106
|
- lib/micro/cases.rb
|
107
|
+
- lib/micro/cases/error.rb
|
106
108
|
- lib/micro/cases/flow.rb
|
109
|
+
- lib/micro/cases/map.rb
|
107
110
|
- lib/micro/cases/safe/flow.rb
|
111
|
+
- lib/micro/cases/utils.rb
|
108
112
|
- lib/u-case.rb
|
109
113
|
- lib/u-case/with_activemodel_validation.rb
|
110
114
|
- test.sh
|