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

Merge branch 'add_states_on_one_line' of https://github.com/HParker/aasm into HParker-add_states_on_one_line

Conflicts:
	lib/aasm/base.rb
This commit is contained in:
Thorsten Böttger 2016-03-19 21:22:47 +13:00
commit 0e350c01ca
5 changed files with 58 additions and 23 deletions

View file

@ -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

View file

@ -73,28 +73,29 @@ module AASM
end
# define a state
def state(name, options={})
@state_machine.add_state(name, klass, options)
# args
# [0] state
# [1] options (or nil)
# or
# [0] state
# [1..] state
def state(*args)
names, options = interpret_state_args(args)
names.each do |name|
@state_machine.add_state(name, klass, options)
aasm_name = @name.to_sym
state = name.to_sym
aasm_name = @name.to_sym
state = name.to_sym
if namespace?
method_name = "#{namespace}_#{name}"
else
method_name = name
end
safely_define_method klass, "#{method_name}?", -> do
aasm(aasm_name).current_state == state
end
method_name = namespace? ? "#{namespace}_#{name}" : name
safely_define_method klass, "#{method_name}?", -> do
aasm(aasm_name).current_state == state
end
if namespace?
const_name = "STATE_#{namespace.upcase}_#{name.upcase}"
else
const_name = "STATE_#{name.upcase}"
end
unless klass.const_defined?(const_name)
klass.const_set(const_name, name)
const_name = namespace? ? "STATE_#{namespace.upcase}_#{name.upcase}" : "STATE_#{name.upcase}"
unless klass.const_defined?(const_name)
klass.const_set(const_name, name)
end
end
end
@ -172,6 +173,7 @@ module AASM
if options[:transition]
@state_machine.events[options[:transition]].transitions_to_state(state).flatten.map(&:from).flatten
else
events.map {|e| e.transitions_to_state(state)}.flatten.map(&:from).flatten
end
end
@ -209,5 +211,16 @@ module AASM
@state_machine.config.namespace
end
end
def interpret_state_args(args)
if args.last.is_a?(Hash) && args.size == 2
[[args.first], args.last]
elsif args.size > 0
[args, {}]
else
raise "count not parse states: #{args}"
end
end
end
end

View file

@ -0,0 +1,8 @@
class StatesOnOneLineExample
include AASM
aasm :one_line do
state :initial, :initial => true
state :first, :second
end
end

View file

@ -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

View 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