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

42 lines
1 KiB
Ruby
Raw Normal View History

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, :success
def initialize(name, options = {}, &block)
@name = name
@success = options[:success]
2008-01-07 14:11:38 -05:00
@transitions = []
instance_eval(&block) if block
end
def fire(obj)
2008-01-08 09:39:00 -05:00
transitions = @transitions.select { |t| t.from == obj.aasm_current_state }
2008-02-21 10:46:06 -05:00
raise AASM::InvalidTransition if transitions.size == 0
next_state = nil
transitions.each do |transition|
if transition.perform(obj)
next_state = transition.to
break
end
2008-01-07 14:11:38 -05:00
end
2008-02-21 10:46:06 -05:00
next_state
2008-01-07 14:11:38 -05:00
end
def transitions_from_state?(state)
@transitions.any? { |t| t.from == state }
end
2008-01-07 14:11:38 -05:00
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