2011-11-26 17:30:47 +00:00
|
|
|
module AASM
|
|
|
|
class StateMachine
|
2012-11-28 09:42:41 +00:00
|
|
|
|
|
|
|
# the following two methods provide the storage of all state machines
|
2014-05-24 06:42:20 +00:00
|
|
|
def self.[](klass)
|
|
|
|
(@machines ||= {})[klass.to_s]
|
2011-11-26 17:30:47 +00:00
|
|
|
end
|
2012-06-27 12:44:48 +00:00
|
|
|
|
2014-05-24 06:42:20 +00:00
|
|
|
def self.[]=(klass, machine)
|
|
|
|
(@machines ||= {})[klass.to_s] = machine
|
2011-11-26 17:30:47 +00:00
|
|
|
end
|
2012-06-27 12:44:48 +00:00
|
|
|
|
2011-11-26 17:30:47 +00:00
|
|
|
attr_accessor :states, :events, :initial_state, :config
|
2009-04-09 05:25:16 +00:00
|
|
|
|
2014-05-22 14:42:18 +00:00
|
|
|
def initialize
|
2011-11-26 17:30:47 +00:00
|
|
|
@initial_state = nil
|
|
|
|
@states = []
|
|
|
|
@events = {}
|
|
|
|
@config = OpenStruct.new
|
|
|
|
end
|
2008-05-31 22:33:17 +00:00
|
|
|
|
2012-11-28 09:42:41 +00:00
|
|
|
# called internally by Ruby 1.9 after clone()
|
2012-06-27 12:44:48 +00:00
|
|
|
def initialize_copy(orig)
|
|
|
|
super
|
|
|
|
@states = @states.dup
|
|
|
|
@events = @events.dup
|
2011-11-26 17:30:47 +00:00
|
|
|
end
|
2008-11-05 16:06:36 +00:00
|
|
|
|
2014-05-24 06:42:20 +00:00
|
|
|
def add_state(name, klass, options)
|
2014-05-25 12:18:54 +00:00
|
|
|
set_initial_state(name, options)
|
2014-05-24 06:42:20 +00:00
|
|
|
@states << AASM::State.new(name, klass, options) unless @states.include?(name)
|
2011-11-26 17:30:47 +00:00
|
|
|
end
|
2012-11-28 09:42:41 +00:00
|
|
|
|
2014-05-25 12:18:54 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_initial_state(name, options)
|
|
|
|
@initial_state = name if options[:initial] || !initial_state
|
|
|
|
end
|
|
|
|
|
2012-11-28 09:42:41 +00:00
|
|
|
end # StateMachine
|
2011-11-26 17:30:47 +00:00
|
|
|
end # AASM
|