u-case 3.0.0.rc9 → 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 +4 -4
- data/README.md +4 -4
- data/README.pt-BR.md +6 -6
- data/lib/micro/case.rb +10 -11
- data/lib/micro/case/config.rb +16 -4
- data/lib/micro/case/error.rb +3 -1
- data/lib/micro/case/result.rb +21 -18
- data/lib/micro/case/safe.rb +1 -1
- data/lib/micro/case/version.rb +1 -1
- data/lib/micro/case/with_activemodel_validation.rb +3 -1
- data/lib/micro/cases/flow.rb +12 -14
- data/lib/micro/cases/safe/flow.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7fe9a88c0c01e52d2f89ba28578c4b67c8cd1d6e303a49ff1ce2d11e5e543159
|
4
|
+
data.tar.gz: a105e4f13e760d03e201a6d6246a0b39e96e0452465953ca0d1767e93d927711
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfed6efdf05b4960bb63c43926cfb2aa6a35b394f665c9a1e472f2ebf05568d418bd04e4c075bea2c990e896131ba57281455ab9fc153d4d4ee4196773798ff4
|
7
|
+
data.tar.gz: 6573ad028e83794fa6851f15a27e3b96a1368d84345e1a8aede633c6f1962bde2ff20e0222bdad9c833dabbe22e90a190ea1c092ef8cab4258d443c9bf91f652
|
data/README.md
CHANGED
@@ -21,7 +21,7 @@ The main project goals are:
|
|
21
21
|
|
22
22
|
Version | Documentation
|
23
23
|
--------- | -------------
|
24
|
-
3.0.0
|
24
|
+
3.0.0 | https://github.com/serradura/u-case/blob/main/README.md
|
25
25
|
2.6.0 | https://github.com/serradura/u-case/blob/v2.x/README.md
|
26
26
|
1.1.0 | https://github.com/serradura/u-case/blob/v1.x/README.md
|
27
27
|
|
@@ -79,7 +79,7 @@ Version | Documentation
|
|
79
79
|
|
80
80
|
| u-case | branch | ruby | activemodel |
|
81
81
|
| -------------- | ------- | -------- | ------------- |
|
82
|
-
| 3.0.0
|
82
|
+
| 3.0.0 | main | >= 2.2.0 | >= 3.2, < 6.1 |
|
83
83
|
| 2.6.0 | v2.x | >= 2.2.0 | >= 3.2, < 6.1 |
|
84
84
|
| 1.1.0 | v1.x | >= 2.2.0 | >= 3.2, < 6.1 |
|
85
85
|
|
@@ -102,7 +102,7 @@ Version | Documentation
|
|
102
102
|
Add this line to your application's Gemfile:
|
103
103
|
|
104
104
|
```ruby
|
105
|
-
gem 'u-case', '~> 3.0.0
|
105
|
+
gem 'u-case', '~> 3.0.0'
|
106
106
|
```
|
107
107
|
|
108
108
|
And then execute:
|
@@ -1060,7 +1060,7 @@ class Multiply < Micro::Case
|
|
1060
1060
|
validates :a, :b, presence: true, numericality: true
|
1061
1061
|
|
1062
1062
|
def call!
|
1063
|
-
return Failure :
|
1063
|
+
return Failure :invalid_attributes, result: { errors: self.errors } if invalid?
|
1064
1064
|
|
1065
1065
|
Success result: { number: a * b }
|
1066
1066
|
end
|
data/README.pt-BR.md
CHANGED
@@ -21,7 +21,7 @@ Principais objetivos deste projeto:
|
|
21
21
|
|
22
22
|
Versão | Documentação
|
23
23
|
--------- | -------------
|
24
|
-
3.0.0
|
24
|
+
3.0.0 | https://github.com/serradura/u-case/blob/main/README.md
|
25
25
|
2.6.0 | https://github.com/serradura/u-case/blob/v2.x/README.md
|
26
26
|
1.1.0 | https://github.com/serradura/u-case/blob/v1.x/README.md
|
27
27
|
|
@@ -34,7 +34,7 @@ Versão | Documentação
|
|
34
34
|
- [`Micro::Case::Result` - O que é o resultado de um caso de uso?](#microcaseresult---o-que-é-o-resultado-de-um-caso-de-uso)
|
35
35
|
- [O que são os tipos de resultados?](#o-que-são-os-tipos-de-resultados)
|
36
36
|
- [Como difinir tipos customizados de resultados?](#como-difinir-tipos-customizados-de-resultados)
|
37
|
-
- [É
|
37
|
+
- [É possível definir um tipo sem definir os dados do resultado?](#é-possível-definir-um-tipo-sem-definir-os-dados-do-resultado)
|
38
38
|
- [Como utilizar os hooks dos resultados?](#como-utilizar-os-hooks-dos-resultados)
|
39
39
|
- [Por que o hook sem um tipo definido expõe o próprio resultado?](#por-que-o-hook-sem-um-tipo-definido-expõe-o-próprio-resultado)
|
40
40
|
- [Usando decomposição para acessar os dados e tipo do resultado](#usando-decomposição-para-acessar-os-dados-e-tipo-do-resultado)
|
@@ -78,7 +78,7 @@ Versão | Documentação
|
|
78
78
|
|
79
79
|
| u-case | branch | ruby | activemodel |
|
80
80
|
| -------------- | ------- | -------- | ------------- |
|
81
|
-
| 3.0.0
|
81
|
+
| 3.0.0 | main | >= 2.2.0 | >= 3.2, < 6.1 |
|
82
82
|
| 2.6.0 | v2.x | >= 2.2.0 | >= 3.2, < 6.1 |
|
83
83
|
| 1.1.0 | v1.x | >= 2.2.0 | >= 3.2, < 6.1 |
|
84
84
|
|
@@ -101,7 +101,7 @@ Versão | Documentação
|
|
101
101
|
Adicione essa linha ao Gemfile da sua aplicação:
|
102
102
|
|
103
103
|
```ruby
|
104
|
-
gem 'u-case', '~> 3.0.0
|
104
|
+
gem 'u-case', '~> 3.0.0'
|
105
105
|
```
|
106
106
|
|
107
107
|
E então execute:
|
@@ -276,7 +276,7 @@ bad_result.failure? # true
|
|
276
276
|
|
277
277
|
[⬆️ Voltar para o índice](#índice-)
|
278
278
|
|
279
|
-
#### É
|
279
|
+
#### É possível definir um tipo sem definir os dados do resultado?
|
280
280
|
|
281
281
|
Resposta: Sim, é possível. Mas isso terá um comportamento especial por conta dos dados do resultado ser um hash com o tipo definido como chave e `true` como o valor.
|
282
282
|
|
@@ -1064,7 +1064,7 @@ class Multiply < Micro::Case
|
|
1064
1064
|
validates :a, :b, presence: true, numericality: true
|
1065
1065
|
|
1066
1066
|
def call!
|
1067
|
-
return Failure :
|
1067
|
+
return Failure :invalid_attributes, result: { errors: self.errors } if invalid?
|
1068
1068
|
|
1069
1069
|
Success result: { number: a * b }
|
1070
1070
|
end
|
data/lib/micro/case.rb
CHANGED
@@ -8,8 +8,8 @@ require 'micro/case/version'
|
|
8
8
|
module Micro
|
9
9
|
class Case
|
10
10
|
require 'micro/case/utils'
|
11
|
-
require 'micro/case/result'
|
12
11
|
require 'micro/case/error'
|
12
|
+
require 'micro/case/result'
|
13
13
|
require 'micro/case/safe'
|
14
14
|
require 'micro/case/strict'
|
15
15
|
require 'micro/case/config'
|
@@ -22,11 +22,14 @@ module Micro
|
|
22
22
|
new(options).__call__
|
23
23
|
end
|
24
24
|
|
25
|
+
INVALID_INVOCATION_OF_THE_THE_METHOD =
|
26
|
+
Error::InvalidInvocationOfTheThenMethod.new(self.name)
|
27
|
+
|
25
28
|
def self.then(use_case = nil, &block)
|
26
29
|
can_yield_self = respond_to?(:yield_self)
|
27
30
|
|
28
31
|
if block
|
29
|
-
raise
|
32
|
+
raise INVALID_INVOCATION_OF_THE_THE_METHOD if use_case
|
30
33
|
raise NotImplementedError if !can_yield_self
|
31
34
|
|
32
35
|
yield_self(&block)
|
@@ -70,7 +73,7 @@ module Micro
|
|
70
73
|
end
|
71
74
|
|
72
75
|
def self.__new__(result, arg)
|
73
|
-
input = result.
|
76
|
+
input = result.__set_accessible_attributes__(arg)
|
74
77
|
|
75
78
|
new(input).__set_result__(result)
|
76
79
|
end
|
@@ -125,7 +128,7 @@ module Micro
|
|
125
128
|
end
|
126
129
|
|
127
130
|
def __call__
|
128
|
-
|
131
|
+
call
|
129
132
|
end
|
130
133
|
|
131
134
|
def __set_result__(result)
|
@@ -139,8 +142,10 @@ module Micro
|
|
139
142
|
|
140
143
|
private
|
141
144
|
|
142
|
-
# This method was reserved for a new feature
|
143
145
|
def call
|
146
|
+
return __call_use_case_flow if __call_use_case_flow?
|
147
|
+
|
148
|
+
__call_use_case
|
144
149
|
end
|
145
150
|
|
146
151
|
def __setup_use_case(input)
|
@@ -151,12 +156,6 @@ module Micro
|
|
151
156
|
self.attributes = input
|
152
157
|
end
|
153
158
|
|
154
|
-
def __call!
|
155
|
-
return __call_use_case_flow if __call_use_case_flow?
|
156
|
-
|
157
|
-
__call_use_case
|
158
|
-
end
|
159
|
-
|
160
159
|
def __call_use_case
|
161
160
|
result = call!
|
162
161
|
|
data/lib/micro/case/config.rb
CHANGED
@@ -7,16 +7,28 @@ module Micro
|
|
7
7
|
class Config
|
8
8
|
include Singleton
|
9
9
|
|
10
|
+
def enable_transitions=(value)
|
11
|
+
Micro::Case::Result.class_variable_set(
|
12
|
+
:@@transitions_enabled, Kind::Of::Boolean(value)
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
10
16
|
def enable_activemodel_validation=(value)
|
11
17
|
return unless Kind::Of::Boolean(value)
|
12
18
|
|
13
19
|
require 'micro/case/with_activemodel_validation'
|
14
20
|
end
|
15
21
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
)
|
22
|
+
def set_activemodel_validation_errors_failure=(value)
|
23
|
+
return unless value
|
24
|
+
|
25
|
+
@activemodel_validation_errors_failure = Kind.of(Symbol, value)
|
26
|
+
end
|
27
|
+
|
28
|
+
def activemodel_validation_errors_failure
|
29
|
+
@activemodel_validation_errors_failure if defined?(@activemodel_validation_errors_failure)
|
30
|
+
|
31
|
+
@activemodel_validation_errors_failure = :invalid_attributes
|
20
32
|
end
|
21
33
|
end
|
22
34
|
end
|
data/lib/micro/case/error.rb
CHANGED
@@ -46,7 +46,9 @@ module Micro
|
|
46
46
|
end
|
47
47
|
|
48
48
|
class InvalidInvocationOfTheThenMethod < StandardError
|
49
|
-
def initialize
|
49
|
+
def initialize(class_name)
|
50
|
+
super("Invalid invocation of the #{class_name}#then method")
|
51
|
+
end
|
50
52
|
end
|
51
53
|
|
52
54
|
def self.by_wrong_usage?(exception)
|
data/lib/micro/case/result.rb
CHANGED
@@ -7,6 +7,9 @@ module Micro
|
|
7
7
|
class Result
|
8
8
|
Kind::Types.add(self)
|
9
9
|
|
10
|
+
INVALID_INVOCATION_OF_THE_THE_METHOD =
|
11
|
+
Error::InvalidInvocationOfTheThenMethod.new(self.name)
|
12
|
+
|
10
13
|
@@transitions_enabled = true
|
11
14
|
|
12
15
|
def self.transitions_enabled?
|
@@ -18,9 +21,9 @@ module Micro
|
|
18
21
|
alias value data
|
19
22
|
|
20
23
|
def initialize
|
21
|
-
@__transitions = []
|
22
|
-
@
|
23
|
-
@
|
24
|
+
@__transitions = @@transitions_enabled ? [] : Kind::Empty::ARRAY
|
25
|
+
@__accumulated_data = {}
|
26
|
+
@__accessible_attributes = {}
|
24
27
|
end
|
25
28
|
|
26
29
|
def to_ary
|
@@ -89,7 +92,7 @@ module Micro
|
|
89
92
|
can_yield_self = respond_to?(:yield_self)
|
90
93
|
|
91
94
|
if block
|
92
|
-
raise
|
95
|
+
raise INVALID_INVOCATION_OF_THE_THE_METHOD if use_case
|
93
96
|
raise NotImplementedError if !can_yield_self
|
94
97
|
|
95
98
|
yield_self(&block)
|
@@ -98,7 +101,7 @@ module Micro
|
|
98
101
|
return failure? ? self : __call_proc(use_case, 'then(-> {})'.freeze) if use_case.is_a?(Proc)
|
99
102
|
return failure? ? self : __call_method(use_case, attributes) if use_case.is_a?(Method)
|
100
103
|
|
101
|
-
raise
|
104
|
+
raise INVALID_INVOCATION_OF_THE_THE_METHOD unless ::Micro.case_or_flow?(use_case)
|
102
105
|
|
103
106
|
return self if failure?
|
104
107
|
|
@@ -118,7 +121,7 @@ module Micro
|
|
118
121
|
return __call_proc(arg, '| -> {}'.freeze) if arg.is_a?(Proc)
|
119
122
|
return __call_method(arg) if arg.is_a?(Method)
|
120
123
|
|
121
|
-
raise
|
124
|
+
raise INVALID_INVOCATION_OF_THE_THE_METHOD unless ::Micro.case_or_flow?(arg)
|
122
125
|
|
123
126
|
failure? ? self : arg.__new__(self, data).__call__
|
124
127
|
end
|
@@ -144,30 +147,30 @@ module Micro
|
|
144
147
|
|
145
148
|
raise Micro::Case::Error::InvalidResult.new(is_success, type, use_case) unless @data
|
146
149
|
|
147
|
-
@
|
150
|
+
@__accumulated_data.merge!(@data)
|
148
151
|
|
149
152
|
use_case_attributes = Utils.symbolize_hash_keys(@use_case.attributes)
|
150
153
|
|
151
|
-
|
154
|
+
__update_accessible_attributes(use_case_attributes)
|
152
155
|
|
153
|
-
__set_transition(use_case_attributes)
|
156
|
+
__set_transition(use_case_attributes) unless @__transitions.frozen?
|
154
157
|
|
155
158
|
self
|
156
159
|
end
|
157
160
|
|
158
|
-
def
|
161
|
+
def __set_accessible_attributes__(arg)
|
159
162
|
return arg unless arg.is_a?(Hash)
|
160
163
|
|
161
164
|
attributes = Utils.symbolize_hash_keys(arg)
|
162
165
|
|
163
|
-
|
166
|
+
__update_accessible_attributes(attributes)
|
164
167
|
end
|
165
168
|
|
166
169
|
private
|
167
170
|
|
168
171
|
def __fetch_accumulated_data(opt = nil)
|
169
|
-
|
170
|
-
opt ? opt.merge(@
|
172
|
+
__update_accessible_attributes(
|
173
|
+
opt ? opt.merge(@__accumulated_data) : @__accumulated_data
|
171
174
|
)
|
172
175
|
end
|
173
176
|
|
@@ -199,9 +202,9 @@ module Micro
|
|
199
202
|
failure? && (expected_type.nil? || expected_type == type)
|
200
203
|
end
|
201
204
|
|
202
|
-
def
|
203
|
-
@
|
204
|
-
@
|
205
|
+
def __update_accessible_attributes(attributes)
|
206
|
+
@__accessible_attributes.merge!(attributes)
|
207
|
+
@__accessible_attributes.dup
|
205
208
|
end
|
206
209
|
|
207
210
|
def __set_transition(use_case_attributes)
|
@@ -212,11 +215,11 @@ module Micro
|
|
212
215
|
@__transitions << {
|
213
216
|
use_case: { class: use_case_class, attributes: use_case_attributes },
|
214
217
|
result => { type: @type, result: data },
|
215
|
-
accessible_attributes: @
|
218
|
+
accessible_attributes: @__accessible_attributes.keys
|
216
219
|
}
|
217
220
|
end
|
218
221
|
|
219
|
-
private_constant :FetchData
|
222
|
+
private_constant :FetchData, :INVALID_INVOCATION_OF_THE_THE_METHOD
|
220
223
|
end
|
221
224
|
end
|
222
225
|
end
|
data/lib/micro/case/safe.rb
CHANGED
data/lib/micro/case/version.rb
CHANGED
@@ -37,7 +37,9 @@ module Micro
|
|
37
37
|
def failure_by_validation_error(object)
|
38
38
|
errors = object.respond_to?(:errors) ? object.errors : object
|
39
39
|
|
40
|
-
Failure
|
40
|
+
Failure Micro::Case::Config.instance.activemodel_validation_errors_failure, result: {
|
41
|
+
errors: errors
|
42
|
+
}
|
41
43
|
end
|
42
44
|
end
|
43
45
|
end
|
data/lib/micro/cases/flow.rb
CHANGED
@@ -10,9 +10,7 @@ module Micro
|
|
10
10
|
attr_reader :use_cases
|
11
11
|
|
12
12
|
def self.map_use_cases(arg)
|
13
|
-
|
14
|
-
|
15
|
-
Array(arg)
|
13
|
+
arg.is_a?(Flow) ? arg.use_cases : Array(arg)
|
16
14
|
end
|
17
15
|
|
18
16
|
def self.build(args)
|
@@ -24,17 +22,17 @@ module Micro
|
|
24
22
|
end
|
25
23
|
|
26
24
|
def initialize(use_cases)
|
27
|
-
@use_cases = use_cases
|
28
|
-
@
|
29
|
-
@
|
25
|
+
@use_cases = use_cases.dup.freeze
|
26
|
+
@next_ones = use_cases.dup
|
27
|
+
@first = @next_ones.shift
|
30
28
|
end
|
31
29
|
|
32
30
|
def call!(input:, result:)
|
33
|
-
first_result =
|
31
|
+
first_result = __case_use_case(@first, result, input)
|
34
32
|
|
35
|
-
return first_result if @
|
33
|
+
return first_result if @next_ones.empty?
|
36
34
|
|
37
|
-
|
35
|
+
__call_next_use_cases(first_result)
|
38
36
|
end
|
39
37
|
|
40
38
|
def call(input = Kind::Empty::HASH)
|
@@ -51,7 +49,7 @@ module Micro
|
|
51
49
|
can_yield_self = respond_to?(:yield_self)
|
52
50
|
|
53
51
|
if block
|
54
|
-
raise Error::InvalidInvocationOfTheThenMethod if use_case
|
52
|
+
raise Error::InvalidInvocationOfTheThenMethod.new(self.class.name) if use_case
|
55
53
|
raise NotImplementedError if !can_yield_self
|
56
54
|
|
57
55
|
yield_self(&block)
|
@@ -64,15 +62,15 @@ module Micro
|
|
64
62
|
|
65
63
|
private
|
66
64
|
|
67
|
-
def
|
65
|
+
def __case_use_case(use_case, result, input)
|
68
66
|
use_case.__new__(result, input).__call__
|
69
67
|
end
|
70
68
|
|
71
|
-
def
|
72
|
-
@
|
69
|
+
def __call_next_use_cases(first_result)
|
70
|
+
@next_ones.reduce(first_result) do |result, use_case|
|
73
71
|
break result if result.failure?
|
74
72
|
|
75
|
-
|
73
|
+
__case_use_case(use_case, result, result.data)
|
76
74
|
end
|
77
75
|
end
|
78
76
|
end
|
@@ -4,7 +4,7 @@ module Micro
|
|
4
4
|
module Cases
|
5
5
|
module Safe
|
6
6
|
class Flow < Cases::Flow
|
7
|
-
private def
|
7
|
+
private def __case_use_case(use_case, result, input)
|
8
8
|
instance = use_case.__new__(result, input)
|
9
9
|
instance.__call__
|
10
10
|
rescue => exception
|
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
|
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-08-
|
11
|
+
date: 2020-08-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kind
|
@@ -122,9 +122,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
122
122
|
version: 2.2.0
|
123
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
124
|
requirements:
|
125
|
-
- - "
|
125
|
+
- - ">="
|
126
126
|
- !ruby/object:Gem::Version
|
127
|
-
version:
|
127
|
+
version: '0'
|
128
128
|
requirements: []
|
129
129
|
rubygems_version: 3.0.6
|
130
130
|
signing_key:
|