use_case 0.2.0 → 0.3.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/version.rb +1 -1
- data/lib/use_case.rb +10 -0
- data/test/sample_use_case.rb +33 -0
- data/test/use_case_test.rb +25 -0
- metadata +2 -2
data/Gemfile.lock
CHANGED
data/lib/use_case/version.rb
CHANGED
data/lib/use_case.rb
CHANGED
@@ -43,6 +43,10 @@ module UseCase
|
|
43
43
|
@command = command
|
44
44
|
end
|
45
45
|
|
46
|
+
def builder(builder)
|
47
|
+
@builder = builder
|
48
|
+
end
|
49
|
+
|
46
50
|
def execute(params)
|
47
51
|
input = @input_class && @input_class.new(params) || params
|
48
52
|
|
@@ -50,6 +54,12 @@ module UseCase
|
|
50
54
|
return outcome
|
51
55
|
end
|
52
56
|
|
57
|
+
begin
|
58
|
+
input = @builder.build(input) if @builder
|
59
|
+
rescue Exception => err
|
60
|
+
return PreConditionFailed.new(self, err)
|
61
|
+
end
|
62
|
+
|
53
63
|
if outcome = validate_params(input)
|
54
64
|
return outcome
|
55
65
|
end
|
data/test/sample_use_case.rb
CHANGED
@@ -90,3 +90,36 @@ class ExplodingRepository
|
|
90
90
|
command(cmd)
|
91
91
|
end
|
92
92
|
end
|
93
|
+
|
94
|
+
class RepositoryBuilder
|
95
|
+
attr_reader :name
|
96
|
+
def initialize(name); @name = name; end
|
97
|
+
def self.build(params)
|
98
|
+
return new(nil) if params[:name] == "invalid"
|
99
|
+
new(params[:name] + "!")
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
class CreateRepositoryWithBuilder
|
104
|
+
include UseCase
|
105
|
+
|
106
|
+
def initialize(user)
|
107
|
+
input_class(NewRepositoryInput)
|
108
|
+
builder(RepositoryBuilder)
|
109
|
+
validator(NewRepositoryValidator)
|
110
|
+
command(CreateRepositoryCommand.new(user))
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class CreateRepositoryWithExplodingBuilder
|
115
|
+
include UseCase
|
116
|
+
|
117
|
+
def initialize(user)
|
118
|
+
input_class(NewRepositoryInput)
|
119
|
+
builder(self)
|
120
|
+
validator(NewRepositoryValidator)
|
121
|
+
command(CreateRepositoryCommand.new(user))
|
122
|
+
end
|
123
|
+
|
124
|
+
def build; raise "Oops"; end
|
125
|
+
end
|
data/test/use_case_test.rb
CHANGED
@@ -88,4 +88,29 @@ describe UseCase do
|
|
88
88
|
use_case.execute(nil)
|
89
89
|
end
|
90
90
|
end
|
91
|
+
|
92
|
+
it "fails when builder processed inputs fail validation" do
|
93
|
+
outcome = CreateRepositoryWithBuilder.new(@logged_in_user).execute({ :name => "invalid" })
|
94
|
+
|
95
|
+
validation = outcome.failure do |v|
|
96
|
+
refute v.valid?
|
97
|
+
assert_equal 1, v.errors.count
|
98
|
+
assert v.errors[:name]
|
99
|
+
end
|
100
|
+
|
101
|
+
refute_nil validation
|
102
|
+
end
|
103
|
+
|
104
|
+
it "passes builder processed inputs to command" do
|
105
|
+
outcome = CreateRepositoryWithBuilder.new(@logged_in_user).execute({ :name => "Dude" })
|
106
|
+
|
107
|
+
assert outcome.success?, outcome.failure && outcome.failure.full_messages.join("\n")
|
108
|
+
assert_equal "Dude!", outcome.result.name
|
109
|
+
end
|
110
|
+
|
111
|
+
it "treats builder error as failed pre-condition" do
|
112
|
+
outcome = CreateRepositoryWithExplodingBuilder.new(@logged_in_user).execute({ :name => "Dude" })
|
113
|
+
|
114
|
+
assert outcome.pre_condition_failed?
|
115
|
+
end
|
91
116
|
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.3.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-
|
12
|
+
date: 2013-04-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|