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
|
aasm do
|
||||||
state :sleeping, :initial => true
|
state :sleeping, :initial => true
|
||||||
state :running
|
state :running, :cleaning
|
||||||
state :cleaning
|
|
||||||
|
|
||||||
event :run do
|
event :run do
|
||||||
transitions :from => :sleeping, :to => :running
|
transitions :from => :sleeping, :to => :running
|
||||||
|
|
|
@ -63,7 +63,24 @@ module AASM
|
||||||
end
|
end
|
||||||
|
|
||||||
# define a state
|
# define a state
|
||||||
def state(name, options={})
|
# 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
|
||||||
|
|
||||||
|
names.each do |name|
|
||||||
@state_machine.add_state(name, @klass, options)
|
@state_machine.add_state(name, @klass, options)
|
||||||
|
|
||||||
if @klass.instance_methods.include?("#{name}?")
|
if @klass.instance_methods.include?("#{name}?")
|
||||||
|
@ -80,6 +97,7 @@ module AASM
|
||||||
@klass.const_set("STATE_#{name.upcase}", name)
|
@klass.const_set("STATE_#{name.upcase}", name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# define an event
|
# define an event
|
||||||
def event(name, options={}, &block)
|
def event(name, options={}, &block)
|
||||||
|
|
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
|
aasm do
|
||||||
state :sleeping, :initial => true
|
state :sleeping, :initial => true
|
||||||
state :running
|
state :running, :cleaning
|
||||||
state :cleaning
|
|
||||||
|
|
||||||
event :run do
|
event :run do
|
||||||
transitions :from => :sleeping, :to => :running
|
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