2008-01-07 14:11:38 -05:00
|
|
|
require File.join(File.dirname(__FILE__), 'state_transition')
|
|
|
|
|
|
|
|
module AASM
|
|
|
|
module SupportingClasses
|
|
|
|
class Event
|
|
|
|
attr_reader :name
|
2008-01-07 15:07:38 -05:00
|
|
|
|
2008-01-07 14:11:38 -05:00
|
|
|
def initialize(name, &block)
|
2008-01-07 15:07:38 -05:00
|
|
|
@name = name
|
2008-01-07 14:11:38 -05:00
|
|
|
@transitions = []
|
|
|
|
instance_eval(&block) if block
|
|
|
|
end
|
|
|
|
|
2008-01-07 15:07:38 -05:00
|
|
|
def fire(obj)
|
2008-01-08 09:39:00 -05:00
|
|
|
transitions = @transitions.select { |t| t.from == obj.aasm_current_state }
|
2008-01-07 15:07:38 -05:00
|
|
|
if transitions.size == 0
|
|
|
|
raise AASM::InvalidTransition
|
|
|
|
else
|
2008-02-21 10:16:08 -05:00
|
|
|
transitions.first.to
|
2008-01-07 14:11:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def transitions(trans_opts)
|
|
|
|
Array(trans_opts[:from]).each do |s|
|
|
|
|
@transitions << SupportingClasses::StateTransition.new(trans_opts.merge({:from => s.to_sym}))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|