lib/state_machine/event.rb in state_machine-0.4.1 vs lib/state_machine/event.rb in state_machine-0.4.2
- old
+ new
@@ -42,11 +42,11 @@
end
# Creates a new transition that will be evaluated when the event is fired.
#
# Configuration options:
- # * +to+ - The state that being transitioned to. If not specified, then the transition will not change the state.
+ # * +to+ - The state that's being transitioned to. If not specified, then the transition will not change the state.
# * +from+ - A state or array of states that can be transitioned from. If not specified, then the transition can occur for *any* from state.
# * +except_from+ - A state or array of states that *cannot* be transitioned from.
# * +if+ - Specifies a method, proc or string to call to determine if the transition should occur (e.g. :if => :moving?, or :if => Proc.new {|car| car.speed > 60}). The method, proc or string should return or evaluate to a true or false value.
# * +unless+ - Specifies a method, proc or string to call to determine if the transition should not occur (e.g. :unless => :stopped?, or :unless => Proc.new {|car| car.speed <= 60}). The method, proc or string should return or evaluate to a true or false value.
#
@@ -117,35 +117,46 @@
else
false
end
end
+ # Draws a representation of this event on the given graph. This will
+ # create 1 or more edges on the graph for each guard (i.e. transition)
+ # configured.
+ #
+ # A collection of the generated edges will be returned.
+ def draw(graph)
+ valid_states = machine.states_order
+ guards.collect {|guard| guard.draw(graph, name, valid_states)}.flatten
+ end
+
protected
# Add the various instance methods that can transition the object using
# the current event
def add_actions
attribute = machine.attribute
- name = self.name
+ qualified_name = name = self.name
+ qualified_name = "#{name}_#{machine.namespace}" if machine.namespace
machine.owner_class.class_eval do
# Checks whether the event can be fired on the current object
- define_method("can_#{name}?") do
- self.class.state_machines[attribute].events[name].can_fire?(self)
+ define_method("can_#{qualified_name}?") do
+ self.class.state_machines[attribute].event(name).can_fire?(self)
end
# Gets the next transition that would be performed if the event were to be fired now
- define_method("next_#{name}_transition") do
- self.class.state_machines[attribute].events[name].next_transition(self)
+ define_method("next_#{qualified_name}_transition") do
+ self.class.state_machines[attribute].event(name).next_transition(self)
end
# Fires the event
- define_method(name) do |*args|
- self.class.state_machines[attribute].events[name].fire(self, *args)
+ define_method(qualified_name) do |*args|
+ self.class.state_machines[attribute].event(name).fire(self, *args)
end
# Fires the event, raising an exception if it fails to transition
- define_method("#{name}!") do |*args|
- send(name, *args) || raise(StateMachine::InvalidTransition, "Cannot transition via :#{name} from #{send(attribute).inspect}")
+ define_method("#{qualified_name}!") do |*args|
+ send(qualified_name, *args) || raise(StateMachine::InvalidTransition, "Cannot transition #{attribute} via :#{name} from #{send(attribute).inspect}")
end
end
end
end
end