aasm/lib/aasm/state_machine.rb

54 lines
1.5 KiB
Ruby
Raw Normal View History

2011-11-26 17:30:47 +00:00
module AASM
class StateMachine
# the following four methods provide the storage of all state machines
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
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 = {}
@config = AASM::Configuration.new
@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()
def initialize_copy(orig)
super
@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
def add_state(state_name, klass, options)
set_initial_state(state_name, options)
# allow reloading, extending or redefining a state
@states.delete(state_name) if @states.include?(state_name)
@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|
@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
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