mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
allow multiple states to be defined on one line
This commit is contained in:
parent
f5c9bfe5b5
commit
afe64b10cb
5 changed files with 52 additions and 12 deletions
|
@ -28,8 +28,7 @@ class Job
|
|||
|
||||
aasm do
|
||||
state :sleeping, :initial => true
|
||||
state :running
|
||||
state :cleaning
|
||||
state :running, :cleaning
|
||||
|
||||
event :run do
|
||||
transitions :from => :sleeping, :to => :running
|
||||
|
|
|
@ -63,21 +63,39 @@ module AASM
|
|||
end
|
||||
|
||||
# define a state
|
||||
def state(name, options={})
|
||||
@state_machine.add_state(name, @klass, options)
|
||||
|
||||
if @klass.instance_methods.include?("#{name}?")
|
||||
warn "#{@klass.name}: The aasm state name #{name} is already used!"
|
||||
# args
|
||||
# [0] state
|
||||
# [1] options (or nil)
|
||||
# or
|
||||
# [0] state
|
||||
# [1..] state
|
||||
def state(*args)
|
||||
if args.last.is_a?(Hash) && args.size == 2
|
||||
names = [args.first]
|
||||
options = args.last
|
||||
elsif
|
||||
names = args
|
||||
options = {}
|
||||
else
|
||||
raise "count not parse states: #{args}"
|
||||
end
|
||||
|
||||
@klass.class_eval <<-EORUBY, __FILE__, __LINE__ + 1
|
||||
names.each do |name|
|
||||
@state_machine.add_state(name, @klass, options)
|
||||
|
||||
if @klass.instance_methods.include?("#{name}?")
|
||||
warn "#{@klass.name}: The aasm state name #{name} is already used!"
|
||||
end
|
||||
|
||||
@klass.class_eval <<-EORUBY, __FILE__, __LINE__ + 1
|
||||
def #{name}?
|
||||
aasm(:#{@name}).current_state == :#{name}
|
||||
end
|
||||
EORUBY
|
||||
|
||||
unless @klass.const_defined?("STATE_#{name.upcase}")
|
||||
@klass.const_set("STATE_#{name.upcase}", name)
|
||||
unless @klass.const_defined?("STATE_#{name.upcase}")
|
||||
@klass.const_set("STATE_#{name.upcase}", name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
8
spec/models/states_on_one_line_example.rb
Normal file
8
spec/models/states_on_one_line_example.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
class StatesOnOneLineExample
|
||||
include AASM
|
||||
|
||||
aasm :one_line do
|
||||
state :initial, :initial => true
|
||||
state :first, :second
|
||||
end
|
||||
end
|
|
@ -8,8 +8,7 @@ describe 'testing the README examples' do
|
|||
|
||||
aasm do
|
||||
state :sleeping, :initial => true
|
||||
state :running
|
||||
state :cleaning
|
||||
state :running, :cleaning
|
||||
|
||||
event :run do
|
||||
transitions :from => :sleeping, :to => :running
|
||||
|
|
16
spec/unit/states_on_one_line_example_spec.rb
Normal file
16
spec/unit/states_on_one_line_example_spec.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe StatesOnOneLineExample do
|
||||
let(:example) { StatesOnOneLineExample.new }
|
||||
describe 'on initialize' do
|
||||
it 'should be in the initial state' do
|
||||
expect(example.aasm(:one_line).current_state).to eql :initial
|
||||
end
|
||||
end
|
||||
|
||||
describe 'states' do
|
||||
it 'should have all 3 states defined' do
|
||||
expect(example.aasm(:one_line).states.map(&:name)).to eq [:initial, :first, :second]
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue