Switch

class
Superclass
Pushdown::Transition

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

anchor
new( name, state_class, data=nil )

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

Public Instance Methods

anchor
apply( stack )

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