lib/fsm/machine.rb in simplificator-fsm-0.2.0 vs lib/fsm/machine.rb in simplificator-fsm-0.2.1

- old
+ new

@@ -36,11 +36,11 @@ to_state = self.states[to_name] transition = Transition.new(name, from_state, to_state, options) from_state.add_transition(transition) - define_transition_method(name, to_name) + define_transition_method(name) end def self.get_current_state_name(target) value = target.send(Machine[target.class].current_state_attribute_name) @@ -65,24 +65,38 @@ self.states[Machine.get_current_state_name(target)].transitions.values end private - def define_transition_method(name, to_name) + def define_transition_method(name) @target_class.instance_eval do define_method(name) do |*args| machine = Machine[self.class] from_name = Machine.get_current_state_name(self) from_state = machine.states[from_name] - to_state = machine.states[to_name] - transition = from_state.transitions[to_name] - raise InvalidStateTransition.new("No transition defined from #{from_name} -> #{to_name}") unless transition + entry = from_state.transitions.detect() {|to_name, tr| tr.name == name} + transition = entry.last if entry + raise InvalidStateTransition.new("No transition with name '#{name}' defined from '#{from_name}'") unless transition + to_state = transition.to + from_state.exit(self) transition.fire_event(self, args) to_state.enter(self) - Machine.set_current_state_name(self, to_name) - true # at the moment always return true ... as soon as we have guards or thelike this could be false as well + Machine.set_current_state_name(self, to_state.name) + true + + + + #to_state = machine.states[to_name] + #transition = from_state.transitions[to_name] + #raise InvalidStateTransition.new("No transition defined from #{from_name} -> #{to_name}") unless transition + + #from_state.exit(self) + #transition.fire_event(self, args) + #to_state.enter(self) + #Machine.set_current_state_name(self, to_name) + #true # at the moment always return true ... as soon as we have guards or thelike this could be false as well end end end \ No newline at end of file