diff --git a/lib/motion-state-machine/base.rb b/lib/motion-state-machine/base.rb index 1171d77..aca545b 100644 --- a/lib/motion-state-machine/base.rb +++ b/lib/motion-state-machine/base.rb @@ -137,9 +137,9 @@ def start! # @example # my_state_machine.event :some_event - def event(event_symbol) + def event(event_symbol, *args) transition = @events[event_symbol] - transition.send(:handle_in_source_state) unless transition.nil? + transition.send(:handle_in_source_state, args) unless transition.nil? end diff --git a/lib/motion-state-machine/state.rb b/lib/motion-state-machine/state.rb index d3f522a..e610ab9 100644 --- a/lib/motion-state-machine/state.rb +++ b/lib/motion-state-machine/state.rb @@ -248,18 +248,28 @@ def die(options) # # @see Base#when - def on_entry(&block) - @state.entry_actions << block + def on_entry(blk = nil, &block) + if block_given? + @state.entry_actions << block + else + @state.entry_actions << blk + end end + + # Defines a block that will be called without parameters when the # source state is exited. # # @see Base#when - def on_exit(&block) - @state.exit_actions << block + def on_exit(blk = nil, &block) + if block_given? + @state.exit_actions << block + else + @state.exit_actions << blk + end end end @@ -336,7 +346,7 @@ def cleanup # @raise [RuntimeError] if multiple transitions would be allowed at # the same time. - def guarded_execute(event_type, event_trigger_value) + def guarded_execute(event_type, event_trigger_value, args = nil) @state_machine.raise_outside_initial_queue return if terminating? @@ -368,7 +378,7 @@ def guarded_execute(event_type, event_trigger_value) else transition = allowed_transitions.first unless transition.nil? - transition.send :unguarded_execute + transition.send :unguarded_execute, args end end diff --git a/lib/motion-state-machine/transition.rb b/lib/motion-state-machine/transition.rb index 239725c..72911a9 100644 --- a/lib/motion-state-machine/transition.rb +++ b/lib/motion-state-machine/transition.rb @@ -186,7 +186,7 @@ def self.type(type_symbol) # makes sure that there are no ambiguous transitions for the # same event. - def handle_in_source_state + def handle_in_source_state(args=nil) if @state_machine.initial_queue.nil? raise RuntimeError, "State machine not started yet." end @@ -199,7 +199,8 @@ def handle_in_source_state @source_state.send :guarded_execute, self.class.event_type, - @event_trigger_value + @event_trigger_value, + args end @@ -211,11 +212,15 @@ def handle_in_source_state # the transition's +:action+ block and calls {State#enter!} on # the destination state. - def unguarded_execute + def unguarded_execute(args = nil) @source_state.send :exit! unless @options[:internal] == true # Could use @state_machine.instance_eval(&options[:action]) here, # but this would be much slower - @options[:action] && @options[:action].call(@state_machine) + if args + @options[:action] && @options[:action].call(@state_machine, *args) + else + @options[:action] && @options[:action].call(@state_machine) + end @destination_state.send :enter! unless @options[:internal] == true @state_machine.log "#{event_log_text}" diff --git a/lib/motion-state-machine/version.rb b/lib/motion-state-machine/version.rb index 2083069..544e798 100644 --- a/lib/motion-state-machine/version.rb +++ b/lib/motion-state-machine/version.rb @@ -1,3 +1,3 @@ module StateMachine - VERSION = "0.8.3" + VERSION = "0.8.5" end