u-case 4.1.0 → 4.3.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/.travis.sh +39 -16
- data/.travis.yml +9 -6
- data/Gemfile +22 -10
- data/README.md +108 -106
- data/README.pt-BR.md +112 -112
- data/lib/micro/case.rb +52 -5
- data/lib/micro/case/config.rb +3 -3
- data/lib/micro/case/result.rb +25 -25
- data/lib/micro/case/result/wrapper.rb +20 -15
- data/lib/micro/case/utils.rb +20 -24
- data/lib/micro/case/version.rb +1 -1
- data/lib/micro/cases/flow.rb +6 -2
- data/lib/micro/cases/map.rb +2 -2
- data/u-case.gemspec +2 -2
- metadata +16 -10
data/README.pt-BR.md
CHANGED
@@ -39,7 +39,7 @@ Principais objetivos deste projeto:
|
|
39
39
|
Versão | Documentação
|
40
40
|
--------- | -------------
|
41
41
|
unreleased| https://github.com/serradura/u-case/blob/main/README.md
|
42
|
-
4.
|
42
|
+
4.3.0 | https://github.com/serradura/u-case/blob/v4.x/README.md
|
43
43
|
3.1.0 | https://github.com/serradura/u-case/blob/v3.x/README.md
|
44
44
|
2.6.0 | https://github.com/serradura/u-case/blob/v2.x/README.md
|
45
45
|
1.1.0 | https://github.com/serradura/u-case/blob/v1.x/README.md
|
@@ -70,9 +70,8 @@ unreleased| https://github.com/serradura/u-case/blob/main/README.md
|
|
70
70
|
- [É possível declarar um fluxo que inclui o próprio caso de uso?](#é-possível-declarar-um-fluxo-que-inclui-o-próprio-caso-de-uso)
|
71
71
|
- [`Micro::Case::Strict` - O que é um caso de uso estrito?](#microcasestrict---o-que-é-um-caso-de-uso-estrito)
|
72
72
|
- [`Micro::Case::Safe` - Existe algum recurso para lidar automaticamente com exceções dentro de um caso de uso ou fluxo?](#microcasesafe---existe-algum-recurso-para-lidar-automaticamente-com-exceções-dentro-de-um-caso-de-uso-ou-fluxo)
|
73
|
-
- [`Micro::Case::Result#on_exception`](#microcaseresulton_exception)
|
74
73
|
- [`Micro::Cases::Safe::Flow`](#microcasessafeflow)
|
75
|
-
- [`Micro::Case::Result#on_exception`](#microcaseresulton_exception
|
74
|
+
- [`Micro::Case::Result#on_exception`](#microcaseresulton_exception)
|
76
75
|
- [`u-case/with_activemodel_validation` - Como validar os atributos do caso de uso?](#u-casewith_activemodel_validation---como-validar-os-atributos-do-caso-de-uso)
|
77
76
|
- [Se eu habilitei a validação automática, é possível desabilitá-la apenas em casos de uso específicos?](#se-eu-habilitei-a-validação-automática-é-possível-desabilitá-la-apenas-em-casos-de-uso-específicos)
|
78
77
|
- [`Kind::Validator`](#kindvalidator)
|
@@ -98,12 +97,13 @@ unreleased| https://github.com/serradura/u-case/blob/main/README.md
|
|
98
97
|
|
99
98
|
## Compatibilidade
|
100
99
|
|
101
|
-
| u-case | branch | ruby |
|
102
|
-
| -------------- | ------- | -------- | ------------- |
|
103
|
-
|
|
104
|
-
| 3.
|
105
|
-
|
|
106
|
-
|
|
100
|
+
| u-case | branch | ruby | activemodel | u-attributes- |
|
101
|
+
| -------------- | ------- | -------- | ------------- | ------------- |
|
102
|
+
| unreleased | main | >= 2.2.0 | >= 3.2, < 7.0 | >= 2.7, < 3.0 |
|
103
|
+
| 4.3.0 | v4.x | >= 2.2.0 | >= 3.2, < 7.0 | >= 2.7, < 3.0 |
|
104
|
+
| 3.1.0 | v3.x | >= 2.2.0 | >= 3.2, < 6.1 | ~> 1.1 |
|
105
|
+
| 2.6.0 | v2.x | >= 2.2.0 | >= 3.2, < 6.1 | ~> 1.1 |
|
106
|
+
| 1.1.0 | v1.x | >= 2.2.0 | >= 3.2, < 6.1 | ~> 1.1 |
|
107
107
|
|
108
108
|
> Nota: O activemodel é uma dependência opcional, esse módulo que [pode ser habilitado](#u-casewith_activemodel_validation---como-validar-os-atributos-do-caso-de-uso) para validar os atributos dos casos de uso.
|
109
109
|
|
@@ -113,7 +113,7 @@ unreleased| https://github.com/serradura/u-case/blob/main/README.md
|
|
113
113
|
|
114
114
|
Sistema de tipos simples (em runtime) para Ruby.
|
115
115
|
|
116
|
-
É usado para validar os inputs de alguns métodos do u-case, além de expor um validador de tipos através do [`activemodel validation`](https://github.com/serradura/kind#kindvalidator-activemodelvalidations) ([veja como habilitar]((#u-casewith_activemodel_validation---how-to-validate-use-case-attributes))).
|
116
|
+
É usado para validar os inputs de alguns métodos do u-case, além de expor um validador de tipos através do [`activemodel validation`](https://github.com/serradura/kind#kindvalidator-activemodelvalidations) ([veja como habilitar]((#u-casewith_activemodel_validation---how-to-validate-use-case-attributes))).
|
117
117
|
2. [`u-attributes`](https://github.com/serradura/u-attributes) gem.
|
118
118
|
|
119
119
|
Essa gem permite definir atributos de leitura (read-only), ou seja, os seus objetos só terão getters para acessar os dados dos seus atributos.
|
@@ -976,8 +976,6 @@ result.on_failure(:exception) do |result|
|
|
976
976
|
end
|
977
977
|
```
|
978
978
|
|
979
|
-
#### `Micro::Case::Result#on_exception`
|
980
|
-
|
981
979
|
Se você precisar lidar com um erro específico, recomendo o uso de uma instrução case. Exemplo:
|
982
980
|
|
983
981
|
```ruby
|
@@ -1055,8 +1053,8 @@ Divide
|
|
1055
1053
|
# Can't divide a number by 0
|
1056
1054
|
# Oh no, something went wrong!
|
1057
1055
|
|
1058
|
-
Divide
|
1059
|
-
.call(a: 2, b: '2')
|
1056
|
+
Divide
|
1057
|
+
.call(a: 2, b: '2')
|
1060
1058
|
.on_success { |result| puts result[:division] }
|
1061
1059
|
.on_exception(TypeError) { puts 'Please, use only numeric attributes.' }
|
1062
1060
|
.on_exception(ZeroDivisionError) { |_error| puts "Can't divide a number by 0." }
|
@@ -1201,44 +1199,46 @@ end
|
|
1201
1199
|
|
1202
1200
|
#### Success results
|
1203
1201
|
|
1204
|
-
| Gem / Abstração | Iterações por segundo |
|
1205
|
-
| ----------------- | --------------------: |
|
1206
|
-
| Dry::Monads |
|
1207
|
-
| **Micro::Case** |
|
1208
|
-
| Interactor |
|
1209
|
-
| Trailblazer::Operation |
|
1210
|
-
| Dry::Transaction |
|
1202
|
+
| Gem / Abstração | Iterações por segundo | Comparação |
|
1203
|
+
| ----------------- | --------------------: | -------------------: |
|
1204
|
+
| Dry::Monads | 315635.1 | _**O mais rápido**_ |
|
1205
|
+
| **Micro::Case** | 75837.7 | 4.16x mais lento |
|
1206
|
+
| Interactor | 59745.5 | 5.28x mais lento |
|
1207
|
+
| Trailblazer::Operation | 28423.9 | 11.10x mais lento |
|
1208
|
+
| Dry::Transaction | 10130.9 | 31.16x mais lento |
|
1211
1209
|
|
1212
1210
|
<details>
|
1213
1211
|
<summary>Show the full <a href="https://github.com/evanphx/benchmark-ips">benchmark/ips</a> results.</summary>
|
1214
1212
|
|
1215
1213
|
```ruby
|
1216
1214
|
# Warming up --------------------------------------
|
1217
|
-
# Interactor 5.
|
1218
|
-
# Trailblazer::Operation
|
1219
|
-
#
|
1220
|
-
#
|
1221
|
-
#
|
1222
|
-
#
|
1223
|
-
#
|
1215
|
+
# Interactor 5.711k i/100ms
|
1216
|
+
# Trailblazer::Operation
|
1217
|
+
# 2.283k i/100ms
|
1218
|
+
# Dry::Monads 31.130k i/100ms
|
1219
|
+
# Dry::Transaction 994.000 i/100ms
|
1220
|
+
# Micro::Case 7.911k i/100ms
|
1221
|
+
# Micro::Case::Safe 7.911k i/100ms
|
1222
|
+
# Micro::Case::Strict 6.248k i/100ms
|
1224
1223
|
|
1225
1224
|
# Calculating -------------------------------------
|
1226
|
-
# Interactor
|
1227
|
-
# Trailblazer::Operation
|
1228
|
-
#
|
1229
|
-
#
|
1230
|
-
#
|
1231
|
-
#
|
1232
|
-
#
|
1225
|
+
# Interactor 59.746k (±29.9%) i/s - 274.128k in 5.049901s
|
1226
|
+
# Trailblazer::Operation
|
1227
|
+
# 28.424k (±15.8%) i/s - 141.546k in 5.087882s
|
1228
|
+
# Dry::Monads 315.635k (± 6.1%) i/s - 1.588M in 5.048914s
|
1229
|
+
# Dry::Transaction 10.131k (± 6.4%) i/s - 50.694k in 5.025150s
|
1230
|
+
# Micro::Case 75.838k (± 9.7%) i/s - 379.728k in 5.052573s
|
1231
|
+
# Micro::Case::Safe 75.461k (±10.1%) i/s - 379.728k in 5.079238s
|
1232
|
+
# Micro::Case::Strict 64.235k (± 9.0%) i/s - 324.896k in 5.097028s
|
1233
1233
|
|
1234
1234
|
# Comparison:
|
1235
|
-
# Dry::Monads:
|
1236
|
-
# Micro::Case:
|
1237
|
-
# Micro::Case::Safe:
|
1238
|
-
# Micro::Case::Strict:
|
1239
|
-
# Interactor:
|
1240
|
-
# Trailblazer::Operation:
|
1241
|
-
# Dry::Transaction:
|
1235
|
+
# Dry::Monads: 315635.1 i/s
|
1236
|
+
# Micro::Case: 75837.7 i/s - 4.16x (± 0.00) slower
|
1237
|
+
# Micro::Case::Safe: 75461.3 i/s - 4.18x (± 0.00) slower
|
1238
|
+
# Micro::Case::Strict: 64234.9 i/s - 4.91x (± 0.00) slower
|
1239
|
+
# Interactor: 59745.5 i/s - 5.28x (± 0.00) slower
|
1240
|
+
# Trailblazer::Operation: 28423.9 i/s - 11.10x (± 0.00) slower
|
1241
|
+
# Dry::Transaction: 10130.9 i/s - 31.16x (± 0.00) slower
|
1242
1242
|
```
|
1243
1243
|
</details>
|
1244
1244
|
|
@@ -1246,44 +1246,44 @@ https://github.com/serradura/u-case/blob/main/benchmarks/perfomance/use_case/suc
|
|
1246
1246
|
|
1247
1247
|
#### Failure results
|
1248
1248
|
|
1249
|
-
| Gem / Abstração | Iterações por segundo |
|
1250
|
-
| ----------------- | --------------------: |
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
| Trailblazer::Operation |
|
1254
|
-
| Interactor |
|
1255
|
-
| Dry::Transaction |
|
1249
|
+
| Gem / Abstração | Iterações por segundo | Comparação |
|
1250
|
+
| ----------------- | --------------------: | -------------------: |
|
1251
|
+
| Dry::Monads | 135386.9 | _**O mais rápido**_ |
|
1252
|
+
| **Micro::Case** | 73489.3 | 1.85x mais lento |
|
1253
|
+
| Trailblazer::Operation | 29016.4 | 4.67x mais lento |
|
1254
|
+
| Interactor | 27037.0 | 5.01x mais lento |
|
1255
|
+
| Dry::Transaction | 8988.6 | 15.06x mais lento |
|
1256
1256
|
|
1257
1257
|
<details>
|
1258
1258
|
<summary>Mostrar o resultado completo do <a href="https://github.com/evanphx/benchmark-ips">benchmark/ips</a>.</summary>
|
1259
1259
|
|
1260
1260
|
```ruby
|
1261
1261
|
# Warming up --------------------------------------
|
1262
|
-
# Interactor 2.
|
1263
|
-
# Trailblazer::Operation
|
1264
|
-
# Dry::Monads 13.
|
1265
|
-
# Dry::Transaction
|
1266
|
-
# Micro::Case
|
1267
|
-
# Micro::Case::Safe
|
1268
|
-
# Micro::Case::Strict
|
1262
|
+
# Interactor 2.626k i/100ms
|
1263
|
+
# Trailblazer::Operation 2.343k i/100ms
|
1264
|
+
# Dry::Monads 13.386k i/100ms
|
1265
|
+
# Dry::Transaction 868.000 i/100ms
|
1266
|
+
# Micro::Case 7.603k i/100ms
|
1267
|
+
# Micro::Case::Safe 7.598k i/100ms
|
1268
|
+
# Micro::Case::Strict 6.178k i/100ms
|
1269
1269
|
|
1270
1270
|
# Calculating -------------------------------------
|
1271
|
-
# Interactor
|
1272
|
-
# Trailblazer::Operation
|
1273
|
-
# Dry::Monads
|
1274
|
-
# Dry::Transaction
|
1275
|
-
# Micro::Case
|
1276
|
-
# Micro::Case::Safe
|
1277
|
-
# Micro::Case::Strict
|
1271
|
+
# Interactor 27.037k (±24.9%) i/s - 128.674k in 5.102133s
|
1272
|
+
# Trailblazer::Operation 29.016k (±12.4%) i/s - 145.266k in 5.074991s
|
1273
|
+
# Dry::Monads 135.387k (±15.1%) i/s - 669.300k in 5.055356s
|
1274
|
+
# Dry::Transaction 8.989k (± 9.2%) i/s - 45.136k in 5.084820s
|
1275
|
+
# Micro::Case 73.247k (± 9.9%) i/s - 364.944k in 5.030449s
|
1276
|
+
# Micro::Case::Safe 73.489k (± 9.6%) i/s - 364.704k in 5.007282s
|
1277
|
+
# Micro::Case::Strict 61.980k (± 8.0%) i/s - 308.900k in 5.014821s
|
1278
1278
|
|
1279
1279
|
# Comparison:
|
1280
|
-
#
|
1281
|
-
#
|
1282
|
-
# Micro::Case:
|
1283
|
-
# Micro::Case::Strict:
|
1284
|
-
# Trailblazer::Operation:
|
1285
|
-
# Interactor:
|
1286
|
-
# Dry::Transaction:
|
1280
|
+
# Dry::Monads: 135386.9 i/s
|
1281
|
+
# Micro::Case::Safe: 73489.3 i/s - 1.84x (± 0.00) slower
|
1282
|
+
# Micro::Case: 73246.6 i/s - 1.85x (± 0.00) slower
|
1283
|
+
# Micro::Case::Strict: 61979.7 i/s - 2.18x (± 0.00) slower
|
1284
|
+
# Trailblazer::Operation: 29016.4 i/s - 4.67x (± 0.00) slower
|
1285
|
+
# Interactor: 27037.0 i/s - 5.01x (± 0.00) slower
|
1286
|
+
# Dry::Transaction: 8988.6 i/s - 15.06x (± 0.00) slower
|
1287
1287
|
```
|
1288
1288
|
</details>
|
1289
1289
|
|
@@ -1295,12 +1295,12 @@ https://github.com/serradura/u-case/blob/main/benchmarks/perfomance/use_case/fai
|
|
1295
1295
|
|
1296
1296
|
| Gem / Abstração | [Resultados de sucesso](https://github.com/serradura/u-case/blob/main/benchmarks/perfomance/flow/success_results.rb) | [Resultados de falha](https://github.com/serradura/u-case/blob/main/benchmarks/perfomance/flow/failure_results.rb) |
|
1297
1297
|
| ------------------------------------------- | ----------------: | ----------------: |
|
1298
|
-
| Micro::Case::Result `pipe` method |
|
1299
|
-
| Micro::Case::Result `then` method |
|
1300
|
-
| Micro::Cases.flow |
|
1301
|
-
| Micro::Case class with an inner flow |
|
1302
|
-
| Micro::Case class including itself as a step|
|
1303
|
-
| Interactor::Organizer |
|
1298
|
+
| Micro::Case::Result `pipe` method | 80936.2 i/s | 78280.4 i/s |
|
1299
|
+
| Micro::Case::Result `then` method | 0x mais lento | 0x mais lento |
|
1300
|
+
| Micro::Cases.flow | 0x mais lento | 0x mais lento |
|
1301
|
+
| Micro::Case class with an inner flow | 1.72x mais lento | 1.68x mais lento |
|
1302
|
+
| Micro::Case class including itself as a step| 1.93x mais lento | 1.87x mais lento |
|
1303
|
+
| Interactor::Organizer | 3.33x mais lento | 3.22x mais lento |
|
1304
1304
|
|
1305
1305
|
\* As gems `Dry::Monads`, `Dry::Transaction`, `Trailblazer::Operation` estão fora desta análise por não terem esse tipo de funcionalidade.
|
1306
1306
|
|
@@ -1309,28 +1309,28 @@ https://github.com/serradura/u-case/blob/main/benchmarks/perfomance/use_case/fai
|
|
1309
1309
|
|
1310
1310
|
```ruby
|
1311
1311
|
# Warming up --------------------------------------
|
1312
|
-
# Interactor::Organizer
|
1313
|
-
# Micro::Cases.flow([])
|
1314
|
-
# Micro::Case flow in a class
|
1315
|
-
# Micro::Case including the class
|
1316
|
-
# Micro::Case::Result#|
|
1317
|
-
# Micro::Case::Result#then
|
1312
|
+
# Interactor::Organizer 1.809k i/100ms
|
1313
|
+
# Micro::Cases.flow([]) 7.808k i/100ms
|
1314
|
+
# Micro::Case flow in a class 4.816k i/100ms
|
1315
|
+
# Micro::Case including the class 4.094k i/100ms
|
1316
|
+
# Micro::Case::Result#| 7.656k i/100ms
|
1317
|
+
# Micro::Case::Result#then 7.138k i/100ms
|
1318
1318
|
|
1319
1319
|
# Calculating -------------------------------------
|
1320
|
-
# Interactor::Organizer
|
1321
|
-
# Micro::Cases.flow([])
|
1322
|
-
# Micro::Case flow in a class
|
1323
|
-
# Micro::Case including the class
|
1324
|
-
# Micro::Case::Result#|
|
1325
|
-
# Micro::Case::Result#then
|
1320
|
+
# Interactor::Organizer 24.290k (±24.0%) i/s - 113.967k in 5.032825s
|
1321
|
+
# Micro::Cases.flow([]) 74.790k (±11.1%) i/s - 374.784k in 5.071740s
|
1322
|
+
# Micro::Case flow in a class 47.043k (± 8.0%) i/s - 235.984k in 5.047477s
|
1323
|
+
# Micro::Case including the class 42.030k (± 8.5%) i/s - 208.794k in 5.002138s
|
1324
|
+
# Micro::Case::Result#| 80.936k (±15.9%) i/s - 398.112k in 5.052531s
|
1325
|
+
# Micro::Case::Result#then 71.459k (± 8.8%) i/s - 356.900k in 5.030526s
|
1326
1326
|
|
1327
1327
|
# Comparison:
|
1328
|
-
# Micro::Case::Result#|:
|
1329
|
-
# Micro::
|
1330
|
-
# Micro::
|
1331
|
-
# Micro::Case flow in a class:
|
1332
|
-
# Micro::Case including the class:
|
1333
|
-
# Interactor::Organizer:
|
1328
|
+
# Micro::Case::Result#|: 80936.2 i/s
|
1329
|
+
# Micro::Cases.flow([]): 74790.1 i/s - same-ish: difference falls within error
|
1330
|
+
# Micro::Case::Result#then: 71459.5 i/s - same-ish: difference falls within error
|
1331
|
+
# Micro::Case flow in a class: 47042.6 i/s - 1.72x (± 0.00) slower
|
1332
|
+
# Micro::Case including the class: 42030.2 i/s - 1.93x (± 0.00) slower
|
1333
|
+
# Interactor::Organizer: 24290.3 i/s - 3.33x (± 0.00) slower
|
1334
1334
|
```
|
1335
1335
|
</details>
|
1336
1336
|
|
@@ -1339,28 +1339,28 @@ https://github.com/serradura/u-case/blob/main/benchmarks/perfomance/use_case/fai
|
|
1339
1339
|
|
1340
1340
|
```ruby
|
1341
1341
|
# Warming up --------------------------------------
|
1342
|
-
# Interactor::Organizer
|
1343
|
-
# Micro::Cases.flow([])
|
1344
|
-
# Micro::Case flow in a class
|
1345
|
-
# Micro::Case including the class
|
1346
|
-
# Micro::Case::Result#|
|
1347
|
-
# Micro::Case::Result#then
|
1342
|
+
# Interactor::Organizer 1.734k i/100ms
|
1343
|
+
# Micro::Cases.flow([]) 7.515k i/100ms
|
1344
|
+
# Micro::Case flow in a class 4.636k i/100ms
|
1345
|
+
# Micro::Case including the class 4.114k i/100ms
|
1346
|
+
# Micro::Case::Result#| 7.588k i/100ms
|
1347
|
+
# Micro::Case::Result#then 6.681k i/100ms
|
1348
1348
|
|
1349
1349
|
# Calculating -------------------------------------
|
1350
|
-
# Interactor::Organizer
|
1351
|
-
# Micro::Cases.flow([])
|
1352
|
-
# Micro::Case flow in a class
|
1353
|
-
# Micro::Case including the class
|
1354
|
-
# Micro::Case::Result#|
|
1355
|
-
# Micro::Case::Result#then
|
1350
|
+
# Interactor::Organizer 24.280k (±24.5%) i/s - 112.710k in 5.013334s
|
1351
|
+
# Micro::Cases.flow([]) 74.999k (± 9.8%) i/s - 375.750k in 5.055777s
|
1352
|
+
# Micro::Case flow in a class 46.681k (± 9.3%) i/s - 236.436k in 5.105105s
|
1353
|
+
# Micro::Case including the class 41.921k (± 8.9%) i/s - 209.814k in 5.043622s
|
1354
|
+
# Micro::Case::Result#| 78.280k (±12.6%) i/s - 386.988k in 5.022146s
|
1355
|
+
# Micro::Case::Result#then 68.898k (± 8.8%) i/s - 347.412k in 5.080116s
|
1356
1356
|
|
1357
1357
|
# Comparison:
|
1358
|
-
# Micro::Case::Result#|:
|
1359
|
-
# Micro::
|
1360
|
-
# Micro::
|
1361
|
-
# Micro::Case flow in a class:
|
1362
|
-
# Micro::Case including the class:
|
1363
|
-
# Interactor::Organizer:
|
1358
|
+
# Micro::Case::Result#|: 78280.4 i/s
|
1359
|
+
# Micro::Cases.flow([]): 74999.4 i/s - same-ish: difference falls within error
|
1360
|
+
# Micro::Case::Result#then: 68898.4 i/s - same-ish: difference falls within error
|
1361
|
+
# Micro::Case flow in a class: 46681.0 i/s - 1.68x (± 0.00) slower
|
1362
|
+
# Micro::Case including the class: 41920.8 i/s - 1.87x (± 0.00) slower
|
1363
|
+
# Interactor::Organizer: 24280.0 i/s - 3.22x (± 0.00) slower
|
1364
1364
|
```
|
1365
1365
|
</details>
|
1366
1366
|
|
data/lib/micro/case.rb
CHANGED
@@ -24,7 +24,11 @@ module Micro
|
|
24
24
|
|
25
25
|
return result unless block_given?
|
26
26
|
|
27
|
-
|
27
|
+
result_wrapper = Result::Wrapper.new(result)
|
28
|
+
|
29
|
+
yield(result_wrapper)
|
30
|
+
|
31
|
+
result_wrapper.output
|
28
32
|
end
|
29
33
|
|
30
34
|
INVALID_INVOCATION_OF_THE_THEN_METHOD =
|
@@ -129,11 +133,25 @@ module Micro
|
|
129
133
|
__flow_set(__flow_use_cases_get) if !__flow_get__ && __flow_use_cases
|
130
134
|
end
|
131
135
|
|
136
|
+
InspectKey = :__inspect_key__ # :nodoc:
|
137
|
+
|
132
138
|
def self.inspect
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
139
|
+
ids = (Thread.current[InspectKey] ||= [])
|
140
|
+
|
141
|
+
if ids.include?(object_id)
|
142
|
+
return sprintf('#<%s: ...>', self)
|
143
|
+
end
|
144
|
+
|
145
|
+
begin
|
146
|
+
ids << object_id
|
147
|
+
|
148
|
+
if __flow_use_cases
|
149
|
+
return '<%s (%s) use_cases=%s>' % [self, __flow_builder__, @__flow_use_cases]
|
150
|
+
else
|
151
|
+
return '<%s (%s) attributes=%s>' % [self, self.superclass, attributes]
|
152
|
+
end
|
153
|
+
ensure
|
154
|
+
ids.pop
|
137
155
|
end
|
138
156
|
end
|
139
157
|
|
@@ -225,10 +243,39 @@ module Micro
|
|
225
243
|
__get_result(false, value, type)
|
226
244
|
end
|
227
245
|
|
246
|
+
|
247
|
+
def Check(type = nil, result: nil, on: Kind::Empty::HASH)
|
248
|
+
result_key = type || :check
|
249
|
+
|
250
|
+
if value
|
251
|
+
result = on[:success] || { result_key => true }
|
252
|
+
|
253
|
+
Success(type || :ok, result: result)
|
254
|
+
else
|
255
|
+
result = on[:failure] || { result_key => false }
|
256
|
+
|
257
|
+
Failure(type || :error, result: result)
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
228
261
|
def __get_result(is_success, value, type)
|
229
262
|
@__result.__set__(is_success, value, type, self)
|
230
263
|
end
|
231
264
|
|
265
|
+
def transaction(adapter = :activerecord)
|
266
|
+
raise NotImplementedError unless adapter == :activerecord
|
267
|
+
|
268
|
+
result = nil
|
269
|
+
|
270
|
+
ActiveRecord::Base.transaction do
|
271
|
+
result = yield
|
272
|
+
|
273
|
+
raise ActiveRecord::Rollback if result.failure?
|
274
|
+
end
|
275
|
+
|
276
|
+
result
|
277
|
+
end
|
278
|
+
|
232
279
|
private_constant :MapFailureType, :INVALID_INVOCATION_OF_THE_THEN_METHOD
|
233
280
|
end
|
234
281
|
|
data/lib/micro/case/config.rb
CHANGED
@@ -9,12 +9,12 @@ module Micro
|
|
9
9
|
|
10
10
|
def enable_transitions=(value)
|
11
11
|
Micro::Case::Result.class_variable_set(
|
12
|
-
:@@transitions_enabled, Kind::
|
12
|
+
:@@transitions_enabled, Kind::Boolean[value]
|
13
13
|
)
|
14
14
|
end
|
15
15
|
|
16
16
|
def enable_activemodel_validation=(value)
|
17
|
-
return unless Kind::
|
17
|
+
return unless Kind::Boolean[value]
|
18
18
|
|
19
19
|
require 'micro/case/with_activemodel_validation'
|
20
20
|
end
|
@@ -22,7 +22,7 @@ module Micro
|
|
22
22
|
def set_activemodel_validation_errors_failure=(value)
|
23
23
|
return unless value
|
24
24
|
|
25
|
-
@activemodel_validation_errors_failure = Kind
|
25
|
+
@activemodel_validation_errors_failure = Kind::Symbol[value]
|
26
26
|
end
|
27
27
|
|
28
28
|
def activemodel_validation_errors_failure
|
data/lib/micro/case/result.rb
CHANGED
@@ -8,8 +8,6 @@ module Micro
|
|
8
8
|
require 'micro/case/result/wrapper'
|
9
9
|
require 'micro/case/result/transitions'
|
10
10
|
|
11
|
-
Kind::Types.add(self)
|
12
|
-
|
13
11
|
INVALID_INVOCATION_OF_THE_THEN_METHOD =
|
14
12
|
Error::InvalidInvocationOfTheThenMethod.new("#{self.name}#")
|
15
13
|
|
@@ -41,7 +39,7 @@ module Micro
|
|
41
39
|
instance_info = '%s (%s) type=:%s data=%s' % [pretty_type, self.class, @type, data]
|
42
40
|
transitions_info = ' transitions=%d' % [@__transitions.size] if Micro::Case::Result.transitions_enabled?
|
43
41
|
|
44
|
-
"
|
42
|
+
"#<#{instance_info}#{transitions_info}>"
|
45
43
|
end
|
46
44
|
|
47
45
|
def to_ary
|
@@ -138,32 +136,12 @@ module Micro
|
|
138
136
|
yield_self(&block)
|
139
137
|
else
|
140
138
|
return yield_self if !use_case && can_yield_self
|
141
|
-
return failure? ? self :
|
142
|
-
return failure? ? self : __call_method(use_case, attributes) if use_case.is_a?(Method)
|
143
|
-
|
144
|
-
raise INVALID_INVOCATION_OF_THE_THEN_METHOD unless ::Micro.case_or_flow?(use_case)
|
145
|
-
|
146
|
-
return self if failure?
|
147
|
-
|
148
|
-
input = attributes.is_a?(Hash) ? self.data.merge(attributes) : self.data
|
149
|
-
|
150
|
-
if use_case.is_a?(::Micro::Cases::Flow)
|
151
|
-
use_case.call!(input: input, result: self)
|
152
|
-
else
|
153
|
-
use_case.__new__(self, input).__call__
|
154
|
-
end
|
139
|
+
return failure? ? self : __call_use_case(use_case, attributes)
|
155
140
|
end
|
156
141
|
end
|
157
142
|
|
158
143
|
def |(arg)
|
159
|
-
|
160
|
-
|
161
|
-
return __call_proc(arg, '| -> {}'.freeze) if arg.is_a?(Proc)
|
162
|
-
return __call_method(arg) if arg.is_a?(Method)
|
163
|
-
|
164
|
-
raise INVALID_INVOCATION_OF_THE_THEN_METHOD unless ::Micro.case_or_flow?(arg)
|
165
|
-
|
166
|
-
failure? ? self : arg.__new__(self, data).__call__
|
144
|
+
failure? ? self : __call_use_case(arg)
|
167
145
|
end
|
168
146
|
|
169
147
|
def transitions
|
@@ -221,6 +199,28 @@ module Micro
|
|
221
199
|
@__accessible_attributes.dup
|
222
200
|
end
|
223
201
|
|
202
|
+
def __call_use_case(use_case, attributes = nil)
|
203
|
+
case use_case
|
204
|
+
when Proc
|
205
|
+
__call_proc(use_case, 'then(-> {})'.freeze)
|
206
|
+
when Method
|
207
|
+
__call_method(use_case, attributes)
|
208
|
+
when String, Symbol
|
209
|
+
use_case_method = self.use_case.method(use_case)
|
210
|
+
__call_method(use_case_method, attributes)
|
211
|
+
else
|
212
|
+
raise INVALID_INVOCATION_OF_THE_THEN_METHOD unless ::Micro.case_or_flow?(use_case)
|
213
|
+
|
214
|
+
input = attributes.is_a?(Hash) ? self.data.merge(attributes) : self.data
|
215
|
+
|
216
|
+
if use_case.is_a?(::Micro::Cases::Flow)
|
217
|
+
use_case.call!(input: input, result: self)
|
218
|
+
else
|
219
|
+
use_case.__new__(self, input).__call__
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
224
|
def __call_proc(fn, expected)
|
225
225
|
__update_accessible_attributes(@__accumulated_data)
|
226
226
|
|