A switch transition – remove the current state from the stack and add a different one.
The data object to pass to the state_class
's constructor
The State to push to.
Create a transition that will Switch
the current State with an instance of the given state_class
on the stack.
# File lib/pushdown/transition/switch.rb, line 12
def initialize( name, state_class, data=nil )
super( name )
@state_class = state_class
@data = data
end
Apply the transition to the given stack
.
# File lib/pushdown/transition/switch.rb, line 34
def apply( stack )
raise Pushdown::TransitionError, "can't switch on an empty stack" if stack.empty?
state = self.state_class.new( self.data )
self.log.debug "switching current state with a new state: %p" % [ state ]
old_state = stack.pop
old_state.on_stop if old_state
stack.push( state )
state.on_start
return stack
end