u-case 4.2.1 → 4.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.sh +40 -37
- data/.travis.yml +9 -6
- data/Gemfile +5 -4
- data/README.md +12 -12
- data/README.pt-BR.md +13 -15
- data/lib/micro/case.rb +20 -1
- data/lib/micro/case/config.rb +3 -3
- data/lib/micro/case/result.rb +27 -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/case/with_activemodel_validation.rb +2 -8
- data/lib/micro/cases/flow.rb +5 -1
- data/lib/micro/cases/map.rb +2 -2
- data/u-case.gemspec +2 -2
- metadata +16 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3b6d430bc2e38d5c9e26b06ad06dc506e24616acc19cc788c6fefbdec4e8060
|
4
|
+
data.tar.gz: 1deb14177315c48126e01c52c24a8aea1371de5d09f6d442ce3c257971bb0fe3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe8e553554fa2f664869ad481e545a12ba231027877de8099d074e64b57ac18a5573559772a34049ccbb1389054cf285a35e5bf53479cb583297fc673746645b
|
7
|
+
data.tar.gz: 492b4489377c918a5347ff205edd5babb3bb9ca6c8df7efb6fb2244626a2025d895b44a39e3ecd97c6a8fa01aa145f4d49948bc43f07958293188a0226598e61
|
data/.travis.sh
CHANGED
@@ -1,43 +1,46 @@
|
|
1
1
|
#!/bin/bash
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
3
|
+
RUBY_V=$(ruby -v)
|
4
|
+
|
5
|
+
function run_with_bundler {
|
6
|
+
rm Gemfile.lock
|
7
|
+
|
8
|
+
if [ ! -z "$1" ]; then
|
9
|
+
bundle_cmd="bundle _$1_"
|
10
|
+
else
|
11
|
+
bundle_cmd="bundle"
|
12
|
+
fi
|
13
|
+
|
14
|
+
eval "$2 $bundle_cmd update"
|
15
|
+
eval "$2 ENABLE_TRANSITIONS=true $bundle_cmd exec rake test"
|
16
|
+
eval "$2 ENABLE_TRANSITIONS=false $bundle_cmd exec rake test"
|
17
|
+
}
|
18
|
+
|
19
|
+
function run_with_ar_version_and_bundler {
|
20
|
+
run_with_bundler "$2" "ACTIVERECORD_VERSION=$1"
|
21
|
+
}
|
22
|
+
|
23
|
+
RUBY_2_2345="ruby 2.[2345]."
|
24
|
+
|
25
|
+
if [[ $RUBY_V =~ $RUBY_2_2345 ]]; then
|
26
|
+
run_with_bundler "$BUNDLER_V1"
|
27
|
+
|
28
|
+
run_with_ar_version_and_bundler "3.2" "$BUNDLER_V1"
|
29
|
+
run_with_ar_version_and_bundler "4.0" "$BUNDLER_V1"
|
30
|
+
run_with_ar_version_and_bundler "4.1" "$BUNDLER_V1"
|
31
|
+
run_with_ar_version_and_bundler "4.2" "$BUNDLER_V1"
|
32
|
+
run_with_ar_version_and_bundler "5.0" "$BUNDLER_V1"
|
33
|
+
run_with_ar_version_and_bundler "5.1" "$BUNDLER_V1"
|
34
|
+
run_with_ar_version_and_bundler "5.2" "$BUNDLER_V1"
|
35
|
+
fi
|
24
36
|
|
25
|
-
|
26
|
-
|
27
|
-
ACTIVERECORD_VERSION='5.1' ENABLE_TRANSITIONS='false' bundle exec rake test
|
37
|
+
RUBY_2_567="ruby 2.[567]."
|
38
|
+
RUBY_3_x_x="ruby 3.0."
|
28
39
|
|
29
|
-
if [[
|
30
|
-
|
31
|
-
ACTIVERECORD_VERSION='5.2' ENABLE_TRANSITIONS='true' bundle exec rake test
|
32
|
-
ACTIVERECORD_VERSION='5.2' ENABLE_TRANSITIONS='false' bundle exec rake test
|
33
|
-
fi
|
40
|
+
if [[ $RUBY_V =~ $RUBY_2_567 ]] || [[ $RUBY_V =~ $RUBY_3_x_x ]]; then
|
41
|
+
gem install bundler -v ">= 2" --no-doc
|
34
42
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
ACTIVERECORD_VERSION='6.0' ENABLE_TRANSITIONS='false' bundle exec rake test
|
43
|
+
run_with_bundler
|
44
|
+
run_with_ar_version_and_bundler "6.0"
|
45
|
+
run_with_ar_version_and_bundler "6.1"
|
39
46
|
fi
|
40
|
-
|
41
|
-
bundle update
|
42
|
-
ENABLE_TRANSITIONS='true' bundle exec rake test
|
43
|
-
ENABLE_TRANSITIONS='false' bundle exec rake test
|
data/.travis.yml
CHANGED
@@ -1,21 +1,24 @@
|
|
1
|
-
|
2
1
|
language: ruby
|
3
2
|
|
4
|
-
|
3
|
+
cache:
|
4
|
+
bundler: true
|
5
|
+
directories:
|
6
|
+
- /home/travis/.rvm/
|
5
7
|
|
6
8
|
rvm:
|
7
|
-
- 2.2.
|
9
|
+
- 2.2.2
|
8
10
|
- 2.3.0
|
9
11
|
- 2.4.0
|
10
12
|
- 2.5.0
|
11
13
|
- 2.6.0
|
12
14
|
- 2.7.0
|
15
|
+
- 3.0.0
|
13
16
|
|
14
|
-
|
17
|
+
env:
|
18
|
+
- BUNDLER_V1="1.17.3"
|
15
19
|
|
16
20
|
before_install:
|
17
|
-
- gem
|
18
|
-
- gem install bundler -v '< 2'
|
21
|
+
- gem install bundler -v "$BUNDLER_V1"
|
19
22
|
|
20
23
|
install: bundle install --jobs=3 --retry=3
|
21
24
|
|
data/Gemfile
CHANGED
@@ -2,7 +2,7 @@ source "https://rubygems.org"
|
|
2
2
|
|
3
3
|
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
4
4
|
|
5
|
-
activerecord_version = ENV.fetch('ACTIVERECORD_VERSION', '
|
5
|
+
activerecord_version = ENV.fetch('ACTIVERECORD_VERSION', '7')
|
6
6
|
|
7
7
|
activerecord = case activerecord_version
|
8
8
|
when '3.2' then '3.2.22'
|
@@ -11,8 +11,9 @@ activerecord = case activerecord_version
|
|
11
11
|
when '4.2' then '4.2.11'
|
12
12
|
when '5.0' then '5.0.7'
|
13
13
|
when '5.1' then '5.1.7'
|
14
|
-
when '5.2' then '5.2.
|
15
|
-
when '6.0' then '6.0.3'
|
14
|
+
when '5.2' then '5.2.4'
|
15
|
+
when '6.0' then '6.0.3.4'
|
16
|
+
when '6.1' then '6.1.2'
|
16
17
|
end
|
17
18
|
|
18
19
|
simplecov_version =
|
@@ -30,7 +31,7 @@ group :test do
|
|
30
31
|
if activerecord
|
31
32
|
sqlite3 =
|
32
33
|
case activerecord
|
33
|
-
when /\A6\.0/, nil then '~> 1.4.0'
|
34
|
+
when /\A6\.(0|1)/, nil then '~> 1.4.0'
|
34
35
|
else '~> 1.3.0'
|
35
36
|
end
|
36
37
|
|
data/README.md
CHANGED
@@ -39,7 +39,7 @@ The main project goals are:
|
|
39
39
|
Version | Documentation
|
40
40
|
--------- | -------------
|
41
41
|
unreleased| https://github.com/serradura/u-case/blob/main/README.md
|
42
|
-
4.
|
42
|
+
4.5.1 | 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
|
@@ -99,13 +99,13 @@ unreleased| https://github.com/serradura/u-case/blob/main/README.md
|
|
99
99
|
|
100
100
|
## Compatibility
|
101
101
|
|
102
|
-
| u-case | branch | ruby |
|
103
|
-
| -------------- | ------- | -------- | ------------- |
|
104
|
-
| unreleased | main | >= 2.2.0 | >= 3.2, <
|
105
|
-
| 4.
|
106
|
-
| 3.1.0 | v3.x | >= 2.2.0 | >= 3.2, < 6.1 |
|
107
|
-
| 2.6.0 | v2.x | >= 2.2.0 | >= 3.2, < 6.1 |
|
108
|
-
| 1.1.0 | v1.x | >= 2.2.0 | >= 3.2, < 6.1 |
|
102
|
+
| u-case | branch | ruby | activemodel | u-attributes |
|
103
|
+
| -------------- | ------- | -------- | ------------- | ------------- |
|
104
|
+
| unreleased | main | >= 2.2.0 | >= 3.2, < 7.0 | >= 2.7, < 3.0 |
|
105
|
+
| 4.5.1 | v4.x | >= 2.2.0 | >= 3.2, < 7.0 | >= 2.7, < 3.0 |
|
106
|
+
| 3.1.0 | v3.x | >= 2.2.0 | >= 3.2, < 6.1 | ~> 1.1 |
|
107
|
+
| 2.6.0 | v2.x | >= 2.2.0 | >= 3.2, < 6.1 | ~> 1.1 |
|
108
|
+
| 1.1.0 | v1.x | >= 2.2.0 | >= 3.2, < 6.1 | ~> 1.1 |
|
109
109
|
|
110
110
|
> Note: The activemodel is an optional dependency, this module [can be enabled](#u-casewith_activemodel_validation---how-to-validate-use-case-attributes) to validate the use cases' attributes.
|
111
111
|
|
@@ -115,7 +115,7 @@ unreleased| https://github.com/serradura/u-case/blob/main/README.md
|
|
115
115
|
|
116
116
|
A simple type system (at runtime) for Ruby.
|
117
117
|
|
118
|
-
It is used to validate some internal u-case's methods input. This gem also exposes an [`ActiveModel validator`](https://github.com/serradura/kind#kindvalidator-activemodelvalidations) when requiring the [`u-case/with_activemodel_validation`](#u-casewith_activemodel_validation---how-to-validate-use-case-attributes) module, or when the [`Micro::Case.config`](#microcaseconfig) was used to enable it.
|
118
|
+
It is used to validate some internal u-case's methods input. This gem also exposes an [`ActiveModel validator`](https://github.com/serradura/kind#kindvalidator-activemodelvalidations) when requiring the [`u-case/with_activemodel_validation`](#u-casewith_activemodel_validation---how-to-validate-use-case-attributes) module, or when the [`Micro::Case.config`](#microcaseconfig) was used to enable it.
|
119
119
|
2. [`u-attributes`](https://github.com/serradura/u-attributes) gem.
|
120
120
|
|
121
121
|
This gem allows defining read-only attributes, that is, your objects will have only getters to access their attributes data.
|
@@ -126,7 +126,7 @@ unreleased| https://github.com/serradura/u-case/blob/main/README.md
|
|
126
126
|
Add this line to your application's Gemfile:
|
127
127
|
|
128
128
|
```ruby
|
129
|
-
gem 'u-case', '~> 4.1
|
129
|
+
gem 'u-case', '~> 4.5.1'
|
130
130
|
```
|
131
131
|
|
132
132
|
And then execute:
|
@@ -1052,8 +1052,8 @@ Divide
|
|
1052
1052
|
# Can't divide a number by 0
|
1053
1053
|
# Oh no, something went wrong!
|
1054
1054
|
|
1055
|
-
Divide
|
1056
|
-
.call(a: 2, b: '2')
|
1055
|
+
Divide
|
1056
|
+
.call(a: 2, b: '2')
|
1057
1057
|
.on_success { |result| puts result[:division] }
|
1058
1058
|
.on_exception(TypeError) { puts 'Please, use only numeric attributes.' }
|
1059
1059
|
.on_exception(ZeroDivisionError) { |_error| puts "Can't divide a number by 0." }
|
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.5.1 | 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
|
-
|
|
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.5.1 | 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.
|
@@ -124,7 +124,7 @@ unreleased| https://github.com/serradura/u-case/blob/main/README.md
|
|
124
124
|
Adicione essa linha ao Gemfile da sua aplicação:
|
125
125
|
|
126
126
|
```ruby
|
127
|
-
gem 'u-case', '~> 4.1
|
127
|
+
gem 'u-case', '~> 4.5.1'
|
128
128
|
```
|
129
129
|
|
130
130
|
E então execute:
|
@@ -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." }
|
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 =
|
@@ -239,6 +243,21 @@ module Micro
|
|
239
243
|
__get_result(false, value, type)
|
240
244
|
end
|
241
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
|
+
|
242
261
|
def __get_result(is_success, value, type)
|
243
262
|
@__result.__set__(is_success, value, type, self)
|
244
263
|
end
|
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
|
|
@@ -113,7 +111,9 @@ module Micro
|
|
113
111
|
def on_exception(expected_exception = nil)
|
114
112
|
return self unless __failure_type?(:exception)
|
115
113
|
|
116
|
-
if !expected_exception || (Kind.is(Exception, expected_exception) && data.fetch(:exception).is_a?(expected_exception))
|
114
|
+
if !expected_exception || (Kind.is?(Exception, expected_exception) && data.fetch(:exception).is_a?(expected_exception))
|
115
|
+
@__is_unknown = false
|
116
|
+
|
117
117
|
yield(data, @use_case)
|
118
118
|
end
|
119
119
|
|
@@ -138,32 +138,12 @@ module Micro
|
|
138
138
|
yield_self(&block)
|
139
139
|
else
|
140
140
|
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
|
141
|
+
return failure? ? self : __call_use_case(use_case, attributes)
|
155
142
|
end
|
156
143
|
end
|
157
144
|
|
158
145
|
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__
|
146
|
+
failure? ? self : __call_use_case(arg)
|
167
147
|
end
|
168
148
|
|
169
149
|
def transitions
|
@@ -221,6 +201,28 @@ module Micro
|
|
221
201
|
@__accessible_attributes.dup
|
222
202
|
end
|
223
203
|
|
204
|
+
def __call_use_case(use_case, attributes = nil)
|
205
|
+
case use_case
|
206
|
+
when Proc
|
207
|
+
__call_proc(use_case, 'then(-> {})'.freeze)
|
208
|
+
when Method
|
209
|
+
__call_method(use_case, attributes)
|
210
|
+
when String, Symbol
|
211
|
+
use_case_method = self.use_case.method(use_case)
|
212
|
+
__call_method(use_case_method, attributes)
|
213
|
+
else
|
214
|
+
raise INVALID_INVOCATION_OF_THE_THEN_METHOD unless ::Micro.case_or_flow?(use_case)
|
215
|
+
|
216
|
+
input = attributes.is_a?(Hash) ? self.data.merge(attributes) : self.data
|
217
|
+
|
218
|
+
if use_case.is_a?(::Micro::Cases::Flow)
|
219
|
+
use_case.call!(input: input, result: self)
|
220
|
+
else
|
221
|
+
use_case.__new__(self, input).__call__
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
224
226
|
def __call_proc(fn, expected)
|
225
227
|
__update_accessible_attributes(@__accumulated_data)
|
226
228
|
|
@@ -4,38 +4,43 @@ module Micro
|
|
4
4
|
class Case
|
5
5
|
class Result
|
6
6
|
class Wrapper
|
7
|
-
|
8
|
-
@__is_unknown = true
|
7
|
+
attr_reader :output
|
9
8
|
|
9
|
+
def initialize(result)
|
10
10
|
@result = result
|
11
|
+
@output = ::Kind::Undefined
|
12
|
+
|
13
|
+
@__is_unknown = true
|
11
14
|
end
|
12
15
|
|
13
16
|
def failure(type = nil)
|
14
|
-
return if @result.success?
|
17
|
+
return if @result.success? || !undefined_output?
|
15
18
|
|
16
|
-
if result_type?(type)
|
17
|
-
@__is_unknown = false
|
18
|
-
|
19
|
-
yield(@result)
|
20
|
-
end
|
19
|
+
set_output(yield(@result)) if result_type?(type)
|
21
20
|
end
|
22
21
|
|
23
22
|
def success(type = nil)
|
24
|
-
return if @result.failure?
|
23
|
+
return if @result.failure? || !undefined_output?
|
25
24
|
|
26
|
-
if result_type?(type)
|
27
|
-
@__is_unknown = false
|
28
|
-
|
29
|
-
yield(@result)
|
30
|
-
end
|
25
|
+
set_output(yield(@result)) if result_type?(type)
|
31
26
|
end
|
32
27
|
|
33
28
|
def unknown
|
34
|
-
|
29
|
+
@output = yield(@result) if @__is_unknown && undefined_output?
|
35
30
|
end
|
36
31
|
|
37
32
|
private
|
38
33
|
|
34
|
+
def set_output(value)
|
35
|
+
@__is_unknown = false
|
36
|
+
|
37
|
+
@output = value
|
38
|
+
end
|
39
|
+
|
40
|
+
def undefined_output?
|
41
|
+
::Kind::Undefined == @output
|
42
|
+
end
|
43
|
+
|
39
44
|
def result_type?(type)
|
40
45
|
type.nil? || @result.type == type
|
41
46
|
end
|
data/lib/micro/case/utils.rb
CHANGED
@@ -1,36 +1,32 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module Micro
|
4
|
-
class Case
|
5
|
-
module Utils
|
3
|
+
module Micro::Case::Utils
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
def self.symbolize_keys(hash)
|
13
|
-
return hash.transform_keys { |key| key.to_sym rescue key } if respond_to?(hash, :transform_keys)
|
5
|
+
module Hashes
|
6
|
+
def self.respond_to?(hash, method)
|
7
|
+
Kind::Hash[hash].respond_to?(method)
|
8
|
+
end
|
14
9
|
|
15
|
-
|
16
|
-
|
17
|
-
memo[key] = v
|
18
|
-
end
|
19
|
-
end
|
10
|
+
def self.symbolize_keys(hash)
|
11
|
+
return hash.transform_keys { |key| key.to_sym rescue key } if respond_to?(hash, :transform_keys)
|
20
12
|
|
21
|
-
|
22
|
-
|
13
|
+
hash.each_with_object({}) do |(k, v), memo|
|
14
|
+
key = k.to_sym rescue k
|
15
|
+
memo[key] = v
|
16
|
+
end
|
17
|
+
end
|
23
18
|
|
24
|
-
|
25
|
-
|
19
|
+
def self.stringify_keys(hash)
|
20
|
+
return hash.transform_keys(&:to_s) if respond_to?(hash, :transform_keys)
|
26
21
|
|
27
|
-
|
28
|
-
|
22
|
+
hash.each_with_object({}) { |(k, v), memo| memo[k.to_s] = v }
|
23
|
+
end
|
29
24
|
|
30
|
-
|
31
|
-
|
32
|
-
end
|
25
|
+
def self.slice(hash, keys)
|
26
|
+
return hash.slice(*keys) if respond_to?(hash, :slice)
|
33
27
|
|
28
|
+
hash.select { |key, _value| keys.include?(key) }
|
34
29
|
end
|
35
30
|
end
|
31
|
+
|
36
32
|
end
|
data/lib/micro/case/version.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'kind/
|
3
|
+
require 'kind/validator'
|
4
4
|
|
5
5
|
require 'micro/case'
|
6
6
|
|
@@ -16,16 +16,10 @@ module Micro
|
|
16
16
|
@disable_auto_validation = true
|
17
17
|
end
|
18
18
|
|
19
|
-
def initialize(input)
|
20
|
-
__setup_use_case(input)
|
21
|
-
|
22
|
-
run_validations! if respond_to?(:run_validations!, true)
|
23
|
-
end
|
24
|
-
|
25
19
|
private
|
26
20
|
|
27
21
|
def __call_use_case
|
28
|
-
return failure_by_validation_error(self) if !self.class.auto_validation_disabled? &&
|
22
|
+
return failure_by_validation_error(self) if !self.class.auto_validation_disabled? && errors.present?
|
29
23
|
|
30
24
|
result = call!
|
31
25
|
|
data/lib/micro/cases/flow.rb
CHANGED
@@ -39,7 +39,11 @@ module Micro
|
|
39
39
|
|
40
40
|
return result unless block_given?
|
41
41
|
|
42
|
-
|
42
|
+
result_wrapper = ::Micro::Case::Result::Wrapper.new(result)
|
43
|
+
|
44
|
+
yield(result_wrapper)
|
45
|
+
|
46
|
+
result_wrapper.output
|
43
47
|
end
|
44
48
|
|
45
49
|
alias __call__ call
|
data/lib/micro/cases/map.rb
CHANGED
@@ -5,7 +5,7 @@ module Micro
|
|
5
5
|
class Map
|
6
6
|
IsAUseCaseOrFlowWithDefaults = -> arg { arg.is_a?(Array) && Micro.case_or_flow?(arg[0]) && arg[1].is_a?(Hash) }
|
7
7
|
IsAUseCaseOrFlow = -> arg { Micro.case_or_flow?(arg) || IsAUseCaseOrFlowWithDefaults[arg] }
|
8
|
-
HasValidArgs = -> (args) { Kind
|
8
|
+
HasValidArgs = -> (args) { Kind::Array[args].all?(&IsAUseCaseOrFlow) }
|
9
9
|
|
10
10
|
attr_reader :use_cases
|
11
11
|
|
@@ -28,7 +28,7 @@ module Micro
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def call(arg = {})
|
31
|
-
hash = Kind
|
31
|
+
hash = Kind::Hash[arg]
|
32
32
|
|
33
33
|
use_cases.map(&GetUseCaseResult[hash])
|
34
34
|
end
|
data/u-case.gemspec
CHANGED
@@ -25,8 +25,8 @@ Gem::Specification.new do |spec|
|
|
25
25
|
|
26
26
|
spec.required_ruby_version = '>= 2.2.0'
|
27
27
|
|
28
|
-
spec.add_runtime_dependency 'kind', '>=
|
29
|
-
spec.add_runtime_dependency 'u-attributes', '
|
28
|
+
spec.add_runtime_dependency 'kind', '>= 5.6', '< 6.0'
|
29
|
+
spec.add_runtime_dependency 'u-attributes', '>= 2.7', '< 3.0'
|
30
30
|
|
31
31
|
spec.add_development_dependency 'bundler'
|
32
32
|
spec.add_development_dependency 'rake', '~> 13.0'
|
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.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kind
|
@@ -16,34 +16,40 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5.6'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '6.0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
29
|
+
version: '5.6'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '6.0'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: u-attributes
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- - "
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '2.7'
|
40
|
+
- - "<"
|
38
41
|
- !ruby/object:Gem::Version
|
39
|
-
version: '
|
42
|
+
version: '3.0'
|
40
43
|
type: :runtime
|
41
44
|
prerelease: false
|
42
45
|
version_requirements: !ruby/object:Gem::Requirement
|
43
46
|
requirements:
|
44
|
-
- - "
|
47
|
+
- - ">="
|
45
48
|
- !ruby/object:Gem::Version
|
46
|
-
version: '2.
|
49
|
+
version: '2.7'
|
50
|
+
- - "<"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '3.0'
|
47
53
|
- !ruby/object:Gem::Dependency
|
48
54
|
name: bundler
|
49
55
|
requirement: !ruby/object:Gem::Requirement
|