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 CHANGED
@@ -12,7 +12,7 @@ GIT
12
12
  PATH
13
13
  remote: .
14
14
  specs:
15
- use_case (0.1.0)
15
+ use_case (0.3.0)
16
16
 
17
17
  GEM
18
18
  remote: http://rubygems.org/
@@ -24,5 +24,5 @@
24
24
  #++
25
25
 
26
26
  module UseCase
27
- VERSION = "0.2.0"
27
+ VERSION = "0.3.0"
28
28
  end
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
@@ -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
@@ -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.2.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-03-27 00:00:00.000000000 Z
12
+ date: 2013-04-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest