use_case 0.10.0 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/use_case/outcome.rb +3 -12
- data/lib/use_case/version.rb +1 -1
- data/lib/use_case.rb +5 -5
- data/test/use_case/outcome_test.rb +11 -39
- metadata +1 -1
data/Gemfile.lock
CHANGED
data/lib/use_case/outcome.rb
CHANGED
@@ -25,12 +25,6 @@
|
|
25
25
|
|
26
26
|
module UseCase
|
27
27
|
class Outcome
|
28
|
-
attr_reader :use_case
|
29
|
-
|
30
|
-
def initialize(use_case = nil)
|
31
|
-
@use_case = use_case
|
32
|
-
end
|
33
|
-
|
34
28
|
def pre_condition_failed?; false; end
|
35
29
|
def success?; false; end
|
36
30
|
def success; end
|
@@ -40,8 +34,7 @@ module UseCase
|
|
40
34
|
end
|
41
35
|
|
42
36
|
class SuccessfulOutcome < Outcome
|
43
|
-
def initialize(
|
44
|
-
super(use_case)
|
37
|
+
def initialize(result = nil)
|
45
38
|
@result = result
|
46
39
|
end
|
47
40
|
|
@@ -60,8 +53,7 @@ module UseCase
|
|
60
53
|
end
|
61
54
|
|
62
55
|
class PreConditionFailed < Outcome
|
63
|
-
def initialize(
|
64
|
-
super(use_case)
|
56
|
+
def initialize(pre_condition = nil)
|
65
57
|
@pre_condition = pre_condition
|
66
58
|
@failure = PreConditionFailure.new(@pre_condition)
|
67
59
|
end
|
@@ -105,8 +97,7 @@ module UseCase
|
|
105
97
|
class FailedOutcome < Outcome
|
106
98
|
attr_reader :input
|
107
99
|
|
108
|
-
def initialize(
|
109
|
-
super(use_case)
|
100
|
+
def initialize(errors = nil, input = nil)
|
110
101
|
@errors = errors
|
111
102
|
@input = input
|
112
103
|
end
|
data/lib/use_case/version.rb
CHANGED
data/lib/use_case.rb
CHANGED
@@ -60,7 +60,7 @@ module UseCase
|
|
60
60
|
begin
|
61
61
|
input = prepare_input(input, step)
|
62
62
|
rescue Exception => err
|
63
|
-
return PreConditionFailed.new(
|
63
|
+
return PreConditionFailed.new(err)
|
64
64
|
end
|
65
65
|
|
66
66
|
if outcome = validate_params(input, step[:validators])
|
@@ -74,15 +74,15 @@ module UseCase
|
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
77
|
-
SuccessfulOutcome.new(
|
77
|
+
SuccessfulOutcome.new(result)
|
78
78
|
end
|
79
79
|
|
80
80
|
def verify_pre_conditions(input)
|
81
81
|
pre_conditions.each do |pc|
|
82
82
|
begin
|
83
|
-
return PreConditionFailed.new(
|
83
|
+
return PreConditionFailed.new(pc) if !pc.satisfied?(input)
|
84
84
|
rescue Exception => err
|
85
|
-
return PreConditionFailed.new(
|
85
|
+
return PreConditionFailed.new(err)
|
86
86
|
end
|
87
87
|
end
|
88
88
|
nil
|
@@ -98,7 +98,7 @@ module UseCase
|
|
98
98
|
def validate_params(input, validators)
|
99
99
|
validators.each do |validator|
|
100
100
|
result = validator.call(input)
|
101
|
-
return FailedOutcome.new(
|
101
|
+
return FailedOutcome.new(result, input) if !result.valid?
|
102
102
|
end
|
103
103
|
nil
|
104
104
|
end
|
@@ -30,13 +30,6 @@ class MyPreCondition
|
|
30
30
|
end
|
31
31
|
|
32
32
|
describe UseCase::Outcome do
|
33
|
-
it "exposes use case" do
|
34
|
-
use_case = 42
|
35
|
-
outcome = UseCase::Outcome.new(use_case)
|
36
|
-
|
37
|
-
assert_equal use_case, outcome.use_case
|
38
|
-
end
|
39
|
-
|
40
33
|
it "defaults to not failing and not being successful (noop)" do
|
41
34
|
outcome = UseCase::Outcome.new
|
42
35
|
outcome.success { fail "Shouldn't succeed" }
|
@@ -48,13 +41,6 @@ describe UseCase::Outcome do
|
|
48
41
|
end
|
49
42
|
|
50
43
|
describe UseCase::SuccessfulOutcome do
|
51
|
-
it "exposes use case" do
|
52
|
-
use_case = { :id => 42 }
|
53
|
-
outcome = UseCase::SuccessfulOutcome.new(use_case)
|
54
|
-
|
55
|
-
assert_equal use_case, outcome.use_case
|
56
|
-
end
|
57
|
-
|
58
44
|
it "does not fail" do
|
59
45
|
outcome = UseCase::SuccessfulOutcome.new
|
60
46
|
outcome.pre_condition_failed { fail "Shouldn't have failed pre-conditions" }
|
@@ -67,7 +53,7 @@ describe UseCase::Outcome do
|
|
67
53
|
it "yields and returns result" do
|
68
54
|
result = 42
|
69
55
|
yielded_result = nil
|
70
|
-
outcome = UseCase::SuccessfulOutcome.new(
|
56
|
+
outcome = UseCase::SuccessfulOutcome.new(result)
|
71
57
|
returned_result = outcome.success { |res| yielded_result = res }
|
72
58
|
|
73
59
|
assert_equal result, yielded_result
|
@@ -75,19 +61,12 @@ describe UseCase::Outcome do
|
|
75
61
|
end
|
76
62
|
|
77
63
|
it "gets result without block" do
|
78
|
-
outcome = UseCase::SuccessfulOutcome.new(
|
64
|
+
outcome = UseCase::SuccessfulOutcome.new(42)
|
79
65
|
assert_equal 42, outcome.success
|
80
66
|
end
|
81
67
|
end
|
82
68
|
|
83
69
|
describe UseCase::PreConditionFailed do
|
84
|
-
it "exposes use case" do
|
85
|
-
use_case = { :id => 42 }
|
86
|
-
outcome = UseCase::PreConditionFailed.new(use_case)
|
87
|
-
|
88
|
-
assert_equal use_case, outcome.use_case
|
89
|
-
end
|
90
|
-
|
91
70
|
it "does not succeed or fail" do
|
92
71
|
outcome = UseCase::PreConditionFailed.new
|
93
72
|
outcome.success { fail "Shouldn't succeed" }
|
@@ -99,7 +78,7 @@ describe UseCase::Outcome do
|
|
99
78
|
|
100
79
|
it "returns failed pre-condition" do
|
101
80
|
pre_condition = 42
|
102
|
-
outcome = UseCase::PreConditionFailed.new(
|
81
|
+
outcome = UseCase::PreConditionFailed.new(pre_condition)
|
103
82
|
returned_pc = outcome.pre_condition_failed
|
104
83
|
|
105
84
|
assert_equal pre_condition, returned_pc
|
@@ -109,7 +88,7 @@ describe UseCase::Outcome do
|
|
109
88
|
it "has flow control API" do
|
110
89
|
yielded = false
|
111
90
|
pre_condition = Array.new
|
112
|
-
outcome = UseCase::PreConditionFailed.new(
|
91
|
+
outcome = UseCase::PreConditionFailed.new(pre_condition)
|
113
92
|
|
114
93
|
returned_pc = outcome.pre_condition_failed do |f|
|
115
94
|
f.when(:array) { |pc| yielded = pc }
|
@@ -121,7 +100,7 @@ describe UseCase::Outcome do
|
|
121
100
|
it "does not call non-matching block" do
|
122
101
|
yielded = nil
|
123
102
|
pre_condition = Array.new
|
124
|
-
outcome = UseCase::PreConditionFailed.new(
|
103
|
+
outcome = UseCase::PreConditionFailed.new(pre_condition)
|
125
104
|
|
126
105
|
returned_pc = outcome.pre_condition_failed do |f|
|
127
106
|
f.when(:something) { |pc| yielded = pc }
|
@@ -133,7 +112,7 @@ describe UseCase::Outcome do
|
|
133
112
|
it "matches by class symbol" do
|
134
113
|
yielded = false
|
135
114
|
pre_condition = MyPreCondition.new
|
136
|
-
outcome = UseCase::PreConditionFailed.new(
|
115
|
+
outcome = UseCase::PreConditionFailed.new(pre_condition)
|
137
116
|
|
138
117
|
returned_pc = outcome.pre_condition_failed do |f|
|
139
118
|
f.when(:something) { |pc| yielded = pc }
|
@@ -145,7 +124,7 @@ describe UseCase::Outcome do
|
|
145
124
|
it "yields to otherwise if no match" do
|
146
125
|
yielded = false
|
147
126
|
pre_condition = MyPreCondition.new
|
148
|
-
outcome = UseCase::PreConditionFailed.new(
|
127
|
+
outcome = UseCase::PreConditionFailed.new(pre_condition)
|
149
128
|
|
150
129
|
returned_pc = outcome.pre_condition_failed do |f|
|
151
130
|
f.when(:nothing) { |pc| yielded = 42 }
|
@@ -157,7 +136,7 @@ describe UseCase::Outcome do
|
|
157
136
|
|
158
137
|
it "raises if calling when after otherwise" do
|
159
138
|
pre_condition = MyPreCondition.new
|
160
|
-
outcome = UseCase::PreConditionFailed.new(
|
139
|
+
outcome = UseCase::PreConditionFailed.new(pre_condition)
|
161
140
|
|
162
141
|
assert_raises(Exception) do
|
163
142
|
returned_pc = outcome.pre_condition_failed do |f|
|
@@ -169,7 +148,7 @@ describe UseCase::Outcome do
|
|
169
148
|
|
170
149
|
it "accesses pre-condition symbol" do
|
171
150
|
pre_condition = MyPreCondition.new
|
172
|
-
outcome = UseCase::PreConditionFailed.new(
|
151
|
+
outcome = UseCase::PreConditionFailed.new(pre_condition)
|
173
152
|
failure = nil
|
174
153
|
|
175
154
|
outcome.pre_condition_failed do |f|
|
@@ -182,13 +161,6 @@ describe UseCase::Outcome do
|
|
182
161
|
end
|
183
162
|
|
184
163
|
describe UseCase::FailedOutcome do
|
185
|
-
it "exposes use case" do
|
186
|
-
use_case = { :id => 42 }
|
187
|
-
outcome = UseCase::FailedOutcome.new(use_case)
|
188
|
-
|
189
|
-
assert_equal use_case, outcome.use_case
|
190
|
-
end
|
191
|
-
|
192
164
|
it "does not succeed or fail pre-conditions" do
|
193
165
|
outcome = UseCase::FailedOutcome.new
|
194
166
|
outcome.success { fail "Shouldn't succeed" }
|
@@ -201,7 +173,7 @@ describe UseCase::Outcome do
|
|
201
173
|
it "yields and returns validation failure" do
|
202
174
|
failure = 42
|
203
175
|
yielded_result = nil
|
204
|
-
outcome = UseCase::FailedOutcome.new(
|
176
|
+
outcome = UseCase::FailedOutcome.new(failure)
|
205
177
|
returned_result = outcome.failure { |result| yielded_result = result }
|
206
178
|
|
207
179
|
assert_equal failure, yielded_result
|
@@ -209,7 +181,7 @@ describe UseCase::Outcome do
|
|
209
181
|
end
|
210
182
|
|
211
183
|
it "gets failure without block" do
|
212
|
-
outcome = UseCase::FailedOutcome.new(
|
184
|
+
outcome = UseCase::FailedOutcome.new(42)
|
213
185
|
assert_equal 42, outcome.failure
|
214
186
|
end
|
215
187
|
end
|