Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support multiple guards per transition
- Loading branch information
Showing
5 changed files
with
85 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class Guardian | ||
include AASM | ||
|
||
aasm do | ||
state :alpha, :initial => true | ||
state :beta | ||
|
||
event :use_one_guard_that_succeeds do | ||
transitions :from => :alpha, :to => :beta, :guard => :succeed | ||
end | ||
event :use_one_guard_that_fails do | ||
transitions :from => :alpha, :to => :beta, :guard => :fail | ||
end | ||
event :use_guards_that_succeed do | ||
transitions :from => :alpha, :to => :beta, :guards => [:succeed, :another_succeed] | ||
end | ||
event :use_guards_where_the_first_fails do | ||
transitions :from => :alpha, :to => :beta, :guards => [:succeed, :fail] | ||
end | ||
event :use_guards_where_the_second_fails do | ||
transitions :from => :alpha, :to => :beta, :guards => [:fail, :succeed] | ||
end | ||
end | ||
|
||
def fail | ||
false | ||
end | ||
|
||
def succeed | ||
true | ||
end | ||
|
||
def another_succeed | ||
true | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'spec_helper' | ||
|
||
describe "per-transition guards" do | ||
let(:guardian) { Guardian.new } | ||
|
||
it "allows the transition if the guard succeeds" do | ||
expect { guardian.use_one_guard_that_succeeds! }.to_not raise_error | ||
expect(guardian).to be_beta | ||
end | ||
|
||
it "stops the transition if the guard fails" do | ||
expect { guardian.use_one_guard_that_fails! }.to raise_error(AASM::InvalidTransition) | ||
expect(guardian).to be_alpha | ||
end | ||
|
||
it "allows the transition if all guards succeeds" do | ||
expect { guardian.use_guards_that_succeed! }.to_not raise_error | ||
expect(guardian).to be_beta | ||
end | ||
|
||
it "stops the transition if the first guard fails" do | ||
expect { guardian.use_guards_where_the_first_fails! }.to raise_error(AASM::InvalidTransition) | ||
expect(guardian).to be_alpha | ||
end | ||
|
||
it "stops the transition if the second guard fails" do | ||
expect { guardian.use_guards_where_the_second_fails! }.to raise_error(AASM::InvalidTransition) | ||
expect(guardian).to be_alpha | ||
end | ||
end |