2011-11-26 17:30:47 +00:00
|
|
|
module AASM
|
|
|
|
class StateMachine
|
2016-03-19 02:41:04 +00:00
|
|
|
# the following four methods provide the storage of all state machines
|
2016-03-18 01:53:19 +00:00
|
|
|
|
2015-09-28 08:19:02 +00:00
|
|
|
attr_accessor :states, :events, :initial_state, :config, :name, :global_callbacks
|
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 = {}
|
2015-09-28 08:19:02 +00:00
|
|
|
@global_callbacks = {}
|
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
|
2017-03-08 06:58:27 +00:00
|
|
|
@states = orig.states.collect { |state| state.clone }
|
|
|
|
@events = {}
|
|
|
|
orig.events.each_pair { |name, event| @events[name] = event.clone }
|
|
|
|
@global_callbacks = @global_callbacks.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
|
|
|
|
2015-09-28 08:19:02 +00:00
|
|
|
def add_global_callbacks(name, *callbacks, &block)
|
|
|
|
@global_callbacks[name] ||= []
|
|
|
|
callbacks.each do |callback|
|
2016-01-20 11:32:31 +00:00
|
|
|
@global_callbacks[name] << callback unless @global_callbacks[name].include? callback
|
2015-09-28 08:19:02 +00:00
|
|
|
end
|
|
|
|
@global_callbacks[name] << block if block
|
|
|
|
end
|
|
|
|
|
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
|