From b017c0890f8358a3695a441ba1779ccff059ba51 Mon Sep 17 00:00:00 2001 From: buffpojken Date: Thu, 15 May 2014 20:15:21 +0200 Subject: [PATCH 1/3] added more support for on_entry, on_exit --- lib/motion-state-machine/state.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/motion-state-machine/state.rb b/lib/motion-state-machine/state.rb index d3f522a..9d72f39 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 From d406fa29ed4227c5b23c1251616436742ca3dd77 Mon Sep 17 00:00:00 2001 From: buffpojken Date: Tue, 23 Sep 2014 15:01:44 +0200 Subject: [PATCH 2/3] added support for passing arguments with events --- lib/motion-state-machine/base.rb | 4 ++-- lib/motion-state-machine/state.rb | 4 ++-- lib/motion-state-machine/transition.rb | 13 +++++++++---- 3 files changed, 13 insertions(+), 8 deletions(-) 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 9d72f39..e610ab9 100644 --- a/lib/motion-state-machine/state.rb +++ b/lib/motion-state-machine/state.rb @@ -346,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? @@ -378,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..82e2098 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}" From d497e03f7be25bdb5e8c9a66423f56d8e6483983 Mon Sep 17 00:00:00 2001 From: buffpojken Date: Tue, 23 Sep 2014 15:24:05 +0200 Subject: [PATCH 3/3] tweaked stuff --- lib/motion-state-machine/transition.rb | 2 +- lib/motion-state-machine/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/motion-state-machine/transition.rb b/lib/motion-state-machine/transition.rb index 82e2098..72911a9 100644 --- a/lib/motion-state-machine/transition.rb +++ b/lib/motion-state-machine/transition.rb @@ -217,7 +217,7 @@ def unguarded_execute(args = nil) # Could use @state_machine.instance_eval(&options[:action]) here, # but this would be much slower if args - @options[:action] && @options[:action].call(@state_machine, args) + @options[:action] && @options[:action].call(@state_machine, *args) else @options[:action] && @options[:action].call(@state_machine) end 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