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:
parent
5b77559ef3
commit
92c9175574
4 changed files with 46 additions and 10 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -16,4 +16,4 @@ TODO
|
||||||
alto
|
alto
|
||||||
.rspec
|
.rspec
|
||||||
.bundle
|
.bundle
|
||||||
|
tags
|
||||||
|
|
|
@ -6,7 +6,8 @@ module AASM
|
||||||
def initialize(name, options = {}, &block)
|
def initialize(name, options = {}, &block)
|
||||||
@name = name
|
@name = name
|
||||||
@transitions = []
|
@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)
|
update(options, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -74,9 +75,13 @@ module AASM
|
||||||
|
|
||||||
def attach_event_guards(definitions)
|
def attach_event_guards(definitions)
|
||||||
unless @guards.empty?
|
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
|
definitions[:guards] = given_guards + @guards
|
||||||
end
|
end
|
||||||
|
unless @unless.empty?
|
||||||
|
given_unless = Array(definitions.delete(:unless))
|
||||||
|
definitions[:unless] = given_unless + @unless
|
||||||
|
end
|
||||||
definitions
|
definitions
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -100,7 +105,7 @@ module AASM
|
||||||
|
|
||||||
transitions.each do |transition|
|
transitions.each do |transition|
|
||||||
next if to_state and !Array(transition.to).include?(to_state)
|
next if to_state and !Array(transition.to).include?(to_state)
|
||||||
if transition.perform(obj, *args)
|
if transition.allowed?(obj, *args)
|
||||||
if test
|
if test
|
||||||
result = true
|
result = true
|
||||||
else
|
else
|
||||||
|
|
|
@ -6,13 +6,14 @@ module AASM
|
||||||
def initialize(opts)
|
def initialize(opts)
|
||||||
@from = opts[:from]
|
@from = opts[:from]
|
||||||
@to = opts[:to]
|
@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]
|
@on_transition = opts[:on_transition]
|
||||||
@opts = opts
|
@opts = opts
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: should be named allowed? or similar
|
# TODO: should be named allowed? or similar
|
||||||
def perform(obj, *args)
|
def allowed?(obj, *args)
|
||||||
@guards.each do |guard|
|
@guards.each do |guard|
|
||||||
case guard
|
case guard
|
||||||
when Symbol, String
|
when Symbol, String
|
||||||
|
@ -21,6 +22,15 @@ module AASM
|
||||||
return false unless guard.call(obj, *args)
|
return false unless guard.call(obj, *args)
|
||||||
end
|
end
|
||||||
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
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
require 'byebug'
|
||||||
|
|
||||||
describe 'transitions' do
|
describe 'transitions' do
|
||||||
|
|
||||||
|
@ -100,7 +101,7 @@ describe AASM::Transition, '- when performing guard checks' do
|
||||||
opts = {:from => 'foo', :to => 'bar'}
|
opts = {:from => 'foo', :to => 'bar'}
|
||||||
st = AASM::Transition.new(opts)
|
st = AASM::Transition.new(opts)
|
||||||
|
|
||||||
expect(st.perform(nil)).to be_true
|
expect(st.allowed?(nil)).to be_true
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should call the method on the object if guard is a symbol' do
|
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')
|
obj = double('object')
|
||||||
expect(obj).to receive(:test)
|
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
|
end
|
||||||
|
|
||||||
it 'should call the method on the object if guard is a string' do
|
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')
|
obj = double('object')
|
||||||
expect(obj).to receive(:test)
|
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
|
end
|
||||||
|
|
||||||
it 'should call the proc passing the object if the guard is a proc' do
|
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')
|
obj = double('object')
|
||||||
expect(obj).to receive(:test)
|
expect(obj).to receive(:test)
|
||||||
|
|
||||||
st.perform(obj)
|
expect(st.allowed?(obj)).to be false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue