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

132 lines
3.5 KiB
Ruby
Raw Normal View History

require File.join(File.dirname(__FILE__), 'event')
2008-02-21 12:32:04 -05:00
require File.join(File.dirname(__FILE__), 'state')
require File.join(File.dirname(__FILE__), 'persistence')
2008-01-07 14:11:38 -05:00
module AASM
class InvalidTransition < Exception
end
2008-01-07 14:11:38 -05:00
def self.included(base) #:nodoc:
base.extend AASM::ClassMethods
AASM::Persistence.set_persistence(base)
2008-01-07 14:11:38 -05:00
end
module ClassMethods
2008-02-21 12:59:28 -05:00
def aasm_initial_state(set_state=nil)
if set_state
aasm_initial_state = set_state
else
@aasm_initial_state
end
2008-01-07 14:11:38 -05:00
end
def aasm_initial_state=(state)
@aasm_initial_state = state
end
2008-02-21 12:59:28 -05:00
def aasm_state(name, options={})
2008-05-31 17:33:45 -04:00
aasm_states << AASM::SupportingClasses::State.new(name, options) unless aasm_states.include?(name)
self.aasm_initial_state = name unless self.aasm_initial_state
2008-01-07 14:11:38 -05:00
define_method("#{name.to_s}?") do
2008-01-08 09:39:00 -05:00
aasm_current_state == name
2008-01-07 14:11:38 -05:00
end
end
def aasm_event(name, options = {}, &block)
unless aasm_events.has_key?(name)
aasm_events[name] = AASM::SupportingClasses::Event.new(name, options, &block)
end
2008-01-07 14:11:38 -05:00
define_method("#{name.to_s}!") do
aasm_fire_event(name, true)
end
define_method("#{name.to_s}") do
aasm_fire_event(name, false)
2008-01-07 14:11:38 -05:00
end
end
def aasm_states
@aasm_states ||= []
end
2008-01-08 10:03:18 -05:00
def aasm_events
@aasm_events ||= {}
2008-01-07 14:11:38 -05:00
end
def aasm_states_for_select
2008-05-31 17:33:45 -04:00
aasm_states.collect { |state| [state.name.to_s.gsub(/_/, ' ').capitalize, state.name.to_s] }
end
2008-01-07 14:11:38 -05:00
end
# Instance methods
2008-01-08 09:39:00 -05:00
def aasm_current_state
return @aasm_current_state if @aasm_current_state
if self.respond_to?(:aasm_read_state) || self.private_methods.include?('aasm_read_state')
@aasm_current_state = aasm_read_state
end
return @aasm_current_state if @aasm_current_state
self.class.aasm_initial_state
2008-01-07 14:11:38 -05:00
end
2008-01-08 09:39:00 -05:00
def aasm_events_for_current_state
aasm_events_for_state(aasm_current_state)
end
def aasm_events_for_state(state)
events = self.class.aasm_events.values.select {|event| event.transitions_from_state?(state) }
events.map {|event| event.name}
end
2008-01-08 09:39:00 -05:00
private
def aasm_current_state_with_persistence=(state)
if self.respond_to?(:aasm_write_state) || self.private_methods.include?('aasm_write_state')
2008-02-21 11:15:40 -05:00
aasm_write_state(state)
2008-01-08 09:39:00 -05:00
end
self.aasm_current_state = state
2008-01-08 09:39:00 -05:00
end
def aasm_current_state=(state)
if self.respond_to?(:aasm_write_state_without_persistence) || self.private_methods.include?('aasm_write_state_without_persistence')
aasm_write_state_without_persistence(state)
end
@aasm_current_state = state
end
def aasm_state_object_for_state(name)
self.class.aasm_states.find {|s| s == name}
end
def aasm_fire_event(name, persist)
aasm_state_object_for_state(aasm_current_state).call_action(:exit, self)
new_state = self.class.aasm_events[name].fire(self)
unless new_state.nil?
aasm_state_object_for_state(new_state).call_action(:enter, self)
if self.respond_to?(:aasm_event_fired)
self.aasm_event_fired(self.aasm_current_state, new_state)
end
if persist
self.aasm_current_state_with_persistence = new_state
self.send(self.class.aasm_events[name].success) if self.class.aasm_events[name].success
else
self.aasm_current_state = new_state
end
true
else
if self.respond_to?(:aasm_event_failed)
self.aasm_event_failed(name)
end
false
end
end
2008-01-07 14:11:38 -05:00
end