Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/motion-state-machine/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
22 changes: 16 additions & 6 deletions lib/motion-state-machine/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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

Expand Down
13 changes: 9 additions & 4 deletions lib/motion-state-machine/transition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand All @@ -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}"
Expand Down
2 changes: 1 addition & 1 deletion lib/motion-state-machine/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module StateMachine
VERSION = "0.8.3"
VERSION = "0.8.5"
end