1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

Added tags file to gitignore. This is the index file created by ctags

which is used by some text editors and is developer environment
specific.

Added the option to use :if syntax as syntactic sugar for guard.

Added the option to use :unless syntax which essentially behaves
as a negated guard.

Added a couple of tests for the :unless syntax since the logic was
slightly different.

Noticed a couple of tests were just returning true/false at
the end rather than checking for expectations.  Fixed a couple
of those; still some remaining.

Changed AASM#perform method to allowed?
This commit is contained in:
Joiey Seeley 2014-11-08 00:44:24 -06:00
parent 5b77559ef3
commit 92c9175574
4 changed files with 46 additions and 10 deletions

2
.gitignore vendored
View file

@ -16,4 +16,4 @@ TODO
alto
.rspec
.bundle
tags

View file

@ -6,7 +6,8 @@ module AASM
def initialize(name, options = {}, &block)
@name = name
@transitions = []
@guards = Array(options[:guard] || options[:guards])
@guards = Array(options[:guard] || options[:guards] || options[:if])
@unless = Array(options[:unless]) #TODO: This could use a better name
update(options, &block)
end
@ -74,9 +75,13 @@ module AASM
def attach_event_guards(definitions)
unless @guards.empty?
given_guards = Array(definitions.delete(:guard) || definitions.delete(:guards))
given_guards = Array(definitions.delete(:guard) || definitions.delete(:guards) || definitions.delete(:if))
definitions[:guards] = given_guards + @guards
end
unless @unless.empty?
given_unless = Array(definitions.delete(:unless))
definitions[:unless] = given_unless + @unless
end
definitions
end
@ -100,7 +105,7 @@ module AASM
transitions.each do |transition|
next if to_state and !Array(transition.to).include?(to_state)
if transition.perform(obj, *args)
if transition.allowed?(obj, *args)
if test
result = true
else

View file

@ -6,13 +6,14 @@ module AASM
def initialize(opts)
@from = opts[:from]
@to = opts[:to]
@guards = Array(opts[:guard] || opts[:guards])
@guards = Array(opts[:guard] || opts[:guards] || opts[:if])
@unless = Array(opts[:unless]) #TODO: This could use a better name
@on_transition = opts[:on_transition]
@opts = opts
end
# TODO: should be named allowed? or similar
def perform(obj, *args)
def allowed?(obj, *args)
@guards.each do |guard|
case guard
when Symbol, String
@ -21,6 +22,15 @@ module AASM
return false unless guard.call(obj, *args)
end
end
@unless.each do |guard|
case guard
when Symbol, String
return false if obj.send(guard, *args)
when Proc
return false if guard.call(obj, *args)
end
end
true
end

View file

@ -1,4 +1,5 @@
require 'spec_helper'
require 'byebug'
describe 'transitions' do
@ -100,7 +101,7 @@ describe AASM::Transition, '- when performing guard checks' do
opts = {:from => 'foo', :to => 'bar'}
st = AASM::Transition.new(opts)
expect(st.perform(nil)).to be_true
expect(st.allowed?(nil)).to be_true
end
it 'should call the method on the object if guard is a symbol' do
@ -110,7 +111,17 @@ describe AASM::Transition, '- when performing guard checks' do
obj = double('object')
expect(obj).to receive(:test)
st.perform(obj)
expect(st.allowed?(obj)).to be false
end
it 'should call the method on the object if unless is a symbol' do
opts = {:from => 'foo', :to => 'bar', :unless => :test}
st = AASM::Transition.new(opts)
obj = double('object')
expect(obj).to receive(:test)
expect(st.allowed?(obj)).to be true
end
it 'should call the method on the object if guard is a string' do
@ -120,7 +131,17 @@ describe AASM::Transition, '- when performing guard checks' do
obj = double('object')
expect(obj).to receive(:test)
st.perform(obj)
expect(st.allowed?(obj)).to be false
end
it 'should call the method on the object if unless is a string' do
opts = {:from => 'foo', :to => 'bar', :unless => 'test'}
st = AASM::Transition.new(opts)
obj = double('object')
expect(obj).to receive(:test)
expect(st.allowed?(obj)).to be true
end
it 'should call the proc passing the object if the guard is a proc' do
@ -130,7 +151,7 @@ describe AASM::Transition, '- when performing guard checks' do
obj = double('object')
expect(obj).to receive(:test)
st.perform(obj)
expect(st.allowed?(obj)).to be false
end
end