u-case 2.6.0 → 3.0.0.rc1
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 +3 -2
- data/Gemfile +8 -0
- data/README.md +320 -306
- data/lib/micro/case.rb +56 -69
- data/lib/micro/case/error.rb +17 -12
- data/lib/micro/case/result.rb +53 -37
- data/lib/micro/case/safe.rb +6 -2
- data/lib/micro/case/version.rb +1 -1
- data/lib/micro/case/with_activemodel_validation.rb +1 -1
- data/lib/micro/cases.rb +16 -0
- data/lib/micro/cases/flow.rb +96 -0
- data/lib/micro/cases/safe/flow.rb +18 -0
- metadata +7 -7
- data/lib/micro/case/flow.rb +0 -48
- data/lib/micro/case/flow/reducer.rb +0 -104
- data/lib/micro/case/safe/flow.rb +0 -44
@@ -0,0 +1,96 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Micro
|
4
|
+
module Cases
|
5
|
+
class Flow
|
6
|
+
class InvalidUseCases < ArgumentError
|
7
|
+
def initialize; super('argument must be a collection of `Micro::Case` classes'.freeze); end
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_reader :use_cases
|
11
|
+
|
12
|
+
def self.map_use_cases(arg)
|
13
|
+
return arg.use_cases if arg.is_a?(Flow)
|
14
|
+
|
15
|
+
Array(arg)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.build(args)
|
19
|
+
use_cases = Array(args).flat_map { |arg| map_use_cases(arg) }
|
20
|
+
|
21
|
+
raise InvalidUseCases if use_cases.any? { |klass| !(klass < ::Micro::Case) }
|
22
|
+
|
23
|
+
new(use_cases)
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(use_cases)
|
27
|
+
@use_cases = use_cases
|
28
|
+
@first_use_case = use_cases[0]
|
29
|
+
@next_use_cases = use_cases[1..-1]
|
30
|
+
end
|
31
|
+
|
32
|
+
def call(arg = {})
|
33
|
+
memo = arg.is_a?(Hash) ? arg.dup : {}
|
34
|
+
|
35
|
+
first_result = first_use_case_result(arg)
|
36
|
+
|
37
|
+
return first_result if @next_use_cases.empty?
|
38
|
+
|
39
|
+
next_use_cases_result(first_result, memo)
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_proc
|
43
|
+
Proc.new { |arg| call(arg) }
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def is_a_result?(arg)
|
49
|
+
arg.is_a?(Case::Result)
|
50
|
+
end
|
51
|
+
|
52
|
+
def arg_to_call?(arg)
|
53
|
+
return true if arg.is_a?(::Micro::Case) || arg.is_a?(Flow)
|
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
|
60
|
+
|
61
|
+
is_a_result?(output) ? output.value : output
|
62
|
+
end
|
63
|
+
|
64
|
+
def first_use_case_input(arg)
|
65
|
+
return call_arg(arg) if arg_to_call?(arg)
|
66
|
+
return arg.value if is_a_result?(arg)
|
67
|
+
|
68
|
+
arg
|
69
|
+
end
|
70
|
+
|
71
|
+
def first_use_case_result(arg)
|
72
|
+
input = first_use_case_input(arg)
|
73
|
+
|
74
|
+
result = Case::Result.new
|
75
|
+
|
76
|
+
@first_use_case.__call_and_set_transition__(result, input)
|
77
|
+
end
|
78
|
+
|
79
|
+
def next_use_case_result(use_case, result, input)
|
80
|
+
use_case.__new__(result, input).call
|
81
|
+
end
|
82
|
+
|
83
|
+
def next_use_cases_result(first_result, memo)
|
84
|
+
@next_use_cases.reduce(first_result) do |result, use_case|
|
85
|
+
break result if result.failure?
|
86
|
+
|
87
|
+
memo.merge!(result.value)
|
88
|
+
|
89
|
+
result.__set_transitions_accessible_attributes__(memo)
|
90
|
+
|
91
|
+
next_use_case_result(use_case, result, memo)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Micro
|
4
|
+
module Cases
|
5
|
+
module Safe
|
6
|
+
class Flow < Cases::Flow
|
7
|
+
private def next_use_case_result(use_case, result, input)
|
8
|
+
instance = use_case.__new__(result, input)
|
9
|
+
instance.call
|
10
|
+
rescue => exception
|
11
|
+
raise exception if Case::Error.by_wrong_usage?(exception)
|
12
|
+
|
13
|
+
result.__set__(false, exception, :exception, instance)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
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
|
+
version: 3.0.0.rc1
|
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-07-
|
11
|
+
date: 2020-07-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kind
|
@@ -86,15 +86,15 @@ files:
|
|
86
86
|
- bin/setup
|
87
87
|
- lib/micro/case.rb
|
88
88
|
- lib/micro/case/error.rb
|
89
|
-
- lib/micro/case/flow.rb
|
90
|
-
- lib/micro/case/flow/reducer.rb
|
91
89
|
- lib/micro/case/result.rb
|
92
90
|
- lib/micro/case/safe.rb
|
93
|
-
- lib/micro/case/safe/flow.rb
|
94
91
|
- lib/micro/case/strict.rb
|
95
92
|
- lib/micro/case/utils.rb
|
96
93
|
- lib/micro/case/version.rb
|
97
94
|
- lib/micro/case/with_activemodel_validation.rb
|
95
|
+
- lib/micro/cases.rb
|
96
|
+
- lib/micro/cases/flow.rb
|
97
|
+
- lib/micro/cases/safe/flow.rb
|
98
98
|
- lib/u-case.rb
|
99
99
|
- lib/u-case/with_activemodel_validation.rb
|
100
100
|
- lib/u-case/with_validation.rb
|
@@ -115,9 +115,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
115
115
|
version: 2.2.0
|
116
116
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
117
|
requirements:
|
118
|
-
- - "
|
118
|
+
- - ">"
|
119
119
|
- !ruby/object:Gem::Version
|
120
|
-
version:
|
120
|
+
version: 1.3.1
|
121
121
|
requirements: []
|
122
122
|
rubygems_version: 3.0.6
|
123
123
|
signing_key:
|
data/lib/micro/case/flow.rb
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Micro
|
4
|
-
class Case
|
5
|
-
module Flow
|
6
|
-
module ClassMethods
|
7
|
-
def __flow__
|
8
|
-
return @__flow if defined?(@__flow)
|
9
|
-
end
|
10
|
-
|
11
|
-
def flow(*args)
|
12
|
-
@__flow = flow_reducer.build(args)
|
13
|
-
end
|
14
|
-
|
15
|
-
def call(options = {})
|
16
|
-
new(options).call
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
CONSTRUCTOR = <<-RUBY
|
21
|
-
def initialize(options)
|
22
|
-
@options = options
|
23
|
-
|
24
|
-
flow = self.class.__flow__
|
25
|
-
|
26
|
-
raise Error::UndefinedFlow unless flow
|
27
|
-
end
|
28
|
-
RUBY
|
29
|
-
|
30
|
-
private_constant :ClassMethods, :CONSTRUCTOR
|
31
|
-
|
32
|
-
# Deprecated: Classes with flows are now defined via `Micro::Case` inheritance
|
33
|
-
def self.included(base)
|
34
|
-
warn 'Deprecation: Micro::Case::Flow mixin is being deprecated, please use `Micro::Case` inheritance instead.'
|
35
|
-
|
36
|
-
def base.flow_reducer; Reducer; end
|
37
|
-
|
38
|
-
base.extend(ClassMethods)
|
39
|
-
|
40
|
-
base.class_eval(CONSTRUCTOR)
|
41
|
-
end
|
42
|
-
|
43
|
-
def call
|
44
|
-
self.class.__flow__.call(@options)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
@@ -1,104 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Micro
|
4
|
-
class Case
|
5
|
-
module Flow
|
6
|
-
class Reducer
|
7
|
-
attr_reader :use_cases
|
8
|
-
|
9
|
-
def self.map_use_cases(arg)
|
10
|
-
return arg.use_cases if arg.is_a?(Reducer)
|
11
|
-
return arg.__flow__.use_cases if arg.is_a?(Class) && arg < ::Micro::Case::Flow
|
12
|
-
|
13
|
-
Array(arg)
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.build(args)
|
17
|
-
use_cases = Array(args).flat_map { |arg| map_use_cases(arg) }
|
18
|
-
|
19
|
-
raise Error::InvalidUseCases if use_cases.any? { |klass| !(klass < ::Micro::Case) }
|
20
|
-
|
21
|
-
new(use_cases)
|
22
|
-
end
|
23
|
-
|
24
|
-
def initialize(use_cases)
|
25
|
-
@use_cases = use_cases
|
26
|
-
@first_use_case = use_cases[0]
|
27
|
-
@next_use_cases = use_cases[1..-1]
|
28
|
-
end
|
29
|
-
|
30
|
-
def call(arg = {})
|
31
|
-
memo = arg.is_a?(Hash) ? arg.dup : {}
|
32
|
-
|
33
|
-
first_result = first_use_case_result(arg)
|
34
|
-
|
35
|
-
return first_result if @next_use_cases.empty?
|
36
|
-
|
37
|
-
next_use_cases_result(first_result, memo)
|
38
|
-
end
|
39
|
-
|
40
|
-
def >>(arg)
|
41
|
-
self.class.build(use_cases + self.class.map_use_cases(arg))
|
42
|
-
end
|
43
|
-
|
44
|
-
def &(arg)
|
45
|
-
raise NoMethodError, "undefined method `&' for #{self.inspect}. Please, use the method `>>' to avoid this error."
|
46
|
-
end
|
47
|
-
|
48
|
-
def to_proc
|
49
|
-
Proc.new { |arg| call(arg) }
|
50
|
-
end
|
51
|
-
|
52
|
-
private
|
53
|
-
|
54
|
-
def is_a_result?(arg)
|
55
|
-
arg.is_a?(Micro::Case::Result)
|
56
|
-
end
|
57
|
-
|
58
|
-
def arg_to_call?(arg)
|
59
|
-
return true if arg.is_a?(::Micro::Case) || arg.is_a?(Reducer)
|
60
|
-
return true if arg.is_a?(Class) && (arg < ::Micro::Case || arg < ::Micro::Case::Flow)
|
61
|
-
return false
|
62
|
-
end
|
63
|
-
|
64
|
-
def call_arg(arg)
|
65
|
-
output = arg.call
|
66
|
-
|
67
|
-
is_a_result?(output) ? output.value : output
|
68
|
-
end
|
69
|
-
|
70
|
-
def first_use_case_input(arg)
|
71
|
-
return call_arg(arg) if arg_to_call?(arg)
|
72
|
-
return arg.value if is_a_result?(arg)
|
73
|
-
|
74
|
-
arg
|
75
|
-
end
|
76
|
-
|
77
|
-
def first_use_case_result(arg)
|
78
|
-
input = first_use_case_input(arg)
|
79
|
-
|
80
|
-
result = ::Micro::Case::Result.new
|
81
|
-
|
82
|
-
@first_use_case.__call_and_set_transition__(result, input)
|
83
|
-
end
|
84
|
-
|
85
|
-
def next_use_case_result(use_case, result, input)
|
86
|
-
use_case.__new__(result, input).call
|
87
|
-
end
|
88
|
-
|
89
|
-
def next_use_cases_result(first_result, memo)
|
90
|
-
@next_use_cases.reduce(first_result) do |result, use_case|
|
91
|
-
break result if result.failure?
|
92
|
-
|
93
|
-
value = result.value
|
94
|
-
input = value.is_a?(Hash) ? memo.tap { |data| data.merge!(value) } : value
|
95
|
-
|
96
|
-
result.__set_transitions_accessible_attributes__(memo)
|
97
|
-
|
98
|
-
next_use_case_result(use_case, result, input)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
data/lib/micro/case/safe/flow.rb
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Micro
|
4
|
-
class Case
|
5
|
-
class Safe
|
6
|
-
module Flow
|
7
|
-
def self.included(base)
|
8
|
-
base.send(:include, ::Micro::Case::Flow)
|
9
|
-
|
10
|
-
def base.flow_reducer; Reducer; end
|
11
|
-
end
|
12
|
-
|
13
|
-
class Reducer < ::Micro::Case::Flow::Reducer
|
14
|
-
alias_method :&, :>>
|
15
|
-
|
16
|
-
def >>(arg)
|
17
|
-
raise NoMethodError, "undefined method `>>' for #{self.inspect}. Please, use the method `&' to avoid this error."
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
def next_use_case_result(use_case, result, input)
|
23
|
-
begin
|
24
|
-
instance = use_case.__new__(result, input)
|
25
|
-
instance.call
|
26
|
-
rescue => exception
|
27
|
-
raise exception if Error::ByWrongUsage.check(exception)
|
28
|
-
|
29
|
-
result.__set__(false, exception, :exception, instance)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.Flow(args)
|
36
|
-
Flow::Reducer.build(Array(args))
|
37
|
-
end
|
38
|
-
|
39
|
-
def self.__flow_reducer
|
40
|
-
Flow::Reducer
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|