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
|
|
|
|
2015-05-05 10:16:25 +00:00
|
|
|
attr_accessor :states, :events, :initial_state, :config, :name
|
2009-04-09 05:25:16 +00:00
|
|
|
|
2015-05-13 10:12:59 +00:00
|
|
|
def initialize(name)
|
2011-11-26 17:30:47 +00:00
|
|
|
@initial_state = nil
|
|
|
|
@states = []
|
|
|
|
@events = {}
|
2014-07-13 12:07:37 +00:00
|
|
|
@config = AASM::Configuration.new
|
2015-05-13 10:12:59 +00:00
|
|
|
@name = name
|
2011-11-26 17:30:47 +00:00
|
|
|
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
|
|
|
|
2015-05-13 10:12:59 +00:00
|
|
|
def add_state(state_name, klass, options)
|
|
|
|
set_initial_state(state_name, options)
|
2014-08-17 16:42:19 +00:00
|
|
|
|
|
|
|
# allow reloading, extending or redefining a state
|
2015-05-13 10:12:59 +00:00
|
|
|
@states.delete(state_name) if @states.include?(state_name)
|
2014-08-17 16:42:19 +00:00
|
|
|
|
2015-05-13 10:12:59 +00:00
|
|
|
@states << AASM::Core::State.new(state_name, klass, self, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_event(name, options, &block)
|
|
|
|
@events[name] = AASM::Core::Event.new(name, self, options, &block)
|
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
|
2011-11-26 17:30:47 +00:00
|
|
|
end
|
2012-11-28 09:42:41 +00:00
|
|
|
|
|
|
|
end # StateMachine
|
2011-11-26 17:30:47 +00:00
|
|
|
end # AASM
|