use_case 0.4.0 → 0.5.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.
- data/Readme.md +10 -5
- data/lib/use_case.rb +4 -2
- data/lib/use_case/version.rb +1 -1
- data/test/sample_use_case.rb +17 -1
- data/test/use_case_test.rb +7 -0
- metadata +2 -2
data/Readme.md
CHANGED
@@ -170,7 +170,7 @@ This is the high-level overview of how `UseCase` strings up a pipeline
|
|
170
170
|
for you to plug in various kinds of business logic:
|
171
171
|
|
172
172
|
```
|
173
|
-
User input (-> input sanitation) (-> pre-conditions) (-> builder) (-> validations) -> command
|
173
|
+
User input (-> input sanitation) (-> pre-conditions) (-> builder) (-> validations) -> command (-> command...)
|
174
174
|
```
|
175
175
|
|
176
176
|
* Start with a hash of user input
|
@@ -179,7 +179,7 @@ User input (-> input sanitation) (-> pre-conditions) (-> builder) (-> validation
|
|
179
179
|
* Optionally run pre-conditions on the santized input
|
180
180
|
* Optionally refine input by running it through a pre-execution "builder"
|
181
181
|
* Optionally (refined) input through one or more validators
|
182
|
-
* Execute command with (refined) input
|
182
|
+
* Execute command(s) with (refined) input
|
183
183
|
|
184
184
|
## Input sanitation
|
185
185
|
|
@@ -221,11 +221,11 @@ dependency. To use this feature, `gem install activemodel`.
|
|
221
221
|
|
222
222
|
When user input has passed input sanitation and pre-conditions have
|
223
223
|
been satisfied, you can optionally pipe input through a "builder"
|
224
|
-
before handing it over to validations and the
|
224
|
+
before handing it over to validations and the commands.
|
225
225
|
|
226
226
|
The builder should be an object with a `build` method. The method will
|
227
227
|
be called with santized input. The return value from `build` will be
|
228
|
-
passed on to validators and the
|
228
|
+
passed on to validators and the commands.
|
229
229
|
|
230
230
|
Builders can be useful if you want to run validations on a domain
|
231
231
|
object rather than directly on "dumb" input.
|
@@ -307,7 +307,12 @@ better name is welcome.
|
|
307
307
|
A command is any Ruby object that defines an `execute(params)` method. Its
|
308
308
|
return value will be passed to the outcome's `success` block. Any errors raised
|
309
309
|
by this method is not rescued, so be sure to wrap `use_case.execute(params)` in
|
310
|
-
a rescue block if you're worried that it raises.
|
310
|
+
a rescue block if you're worried that it raises. Better yet, detect known causes
|
311
|
+
of exceptions in a pre-condition so you know that the command does not raise.
|
312
|
+
|
313
|
+
A use case can execute multiple commands. When you do, the result of the first
|
314
|
+
command will be the input to the second command and so on. The result of the
|
315
|
+
last command will be the final `outcome.result`.
|
311
316
|
|
312
317
|
## Use cases
|
313
318
|
|
data/lib/use_case.rb
CHANGED
@@ -40,7 +40,8 @@ module UseCase
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def command(command)
|
43
|
-
@
|
43
|
+
@commands ||= []
|
44
|
+
@commands << command
|
44
45
|
end
|
45
46
|
|
46
47
|
def builder(builder)
|
@@ -64,7 +65,8 @@ module UseCase
|
|
64
65
|
return outcome
|
65
66
|
end
|
66
67
|
|
67
|
-
|
68
|
+
result = @commands.inject(input) { |input, command| command.execute(input) }
|
69
|
+
SuccessfulOutcome.new(self, result)
|
68
70
|
end
|
69
71
|
|
70
72
|
private
|
data/lib/use_case/version.rb
CHANGED
data/test/sample_use_case.rb
CHANGED
@@ -26,7 +26,7 @@ require "virtus"
|
|
26
26
|
require "use_case"
|
27
27
|
|
28
28
|
class Model
|
29
|
-
|
29
|
+
attr_accessor :id, :name
|
30
30
|
|
31
31
|
def initialize(id, name)
|
32
32
|
@id = id
|
@@ -123,3 +123,19 @@ class CreateRepositoryWithExplodingBuilder
|
|
123
123
|
|
124
124
|
def build; raise "Oops"; end
|
125
125
|
end
|
126
|
+
|
127
|
+
class PimpRepositoryCommand
|
128
|
+
def execute(repository); repository.name += " (Pimped)"; repository end
|
129
|
+
end
|
130
|
+
|
131
|
+
class CreatePimpedRepository
|
132
|
+
include UseCase
|
133
|
+
|
134
|
+
def initialize(user)
|
135
|
+
input_class(NewRepositoryInput)
|
136
|
+
command(CreateRepositoryCommand.new(user))
|
137
|
+
command(PimpRepositoryCommand.new)
|
138
|
+
end
|
139
|
+
|
140
|
+
def build; raise "Oops"; end
|
141
|
+
end
|
data/test/use_case_test.rb
CHANGED
@@ -113,4 +113,11 @@ describe UseCase do
|
|
113
113
|
|
114
114
|
assert outcome.pre_condition_failed?
|
115
115
|
end
|
116
|
+
|
117
|
+
it "chains two commands" do
|
118
|
+
outcome = CreatePimpedRepository.new(@logged_in_user).execute({ :name => "Mr" })
|
119
|
+
|
120
|
+
assert_equal 1349, outcome.result.id
|
121
|
+
assert_equal "Mr (Pimped)", outcome.result.name
|
122
|
+
end
|
116
123
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: use_case
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|