class Pushdown::Transition::Switch
A switch transition – remove the current state from the stack and add a different one.
Attributes
data[R]
The data object to pass to the state_class
's constructor
state_class[R]
The State to push to.
Public Class Methods
new( name, state_class, data=nil )
click to toggle source
Create a transition that will Switch
the current State with an instance of the given state_class
on the stack.
Calls superclass method
Pushdown::Transition::new
# File lib/pushdown/transition/switch.rb, line 12 def initialize( name, state_class, data=nil ) super( name ) @state_class = state_class @data = data end
Public Instance Methods
apply( stack )
click to toggle source
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