Class: Pushdown::Transition::Push

Inherits:
Pushdown::Transition show all
Defined in:
lib/pushdown/transition/push.rb

Overview

A push transition – add an instance of a given State to the top of the state stack.

Instance Attribute Summary collapse

Attributes inherited from Pushdown::Transition

#name

Instance Method Summary collapse

Methods inherited from Pushdown::Transition

inherited, #type_name

Constructor Details

#initialize(name, state_class, data = nil) ⇒ Push

Create a transition that will Push an instance of the given state_class to the stack.



14
15
16
17
18
19
# File 'lib/pushdown/transition/push.rb', line 14

def initialize( name, state_class, data=nil )
  super( name )

  @state_class = state_class
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

The data object to pass to the #state_class’s constructor



32
33
34
# File 'lib/pushdown/transition/push.rb', line 32

def data
  @data
end

#state_classObject (readonly)

The State to push to.



28
29
30
# File 'lib/pushdown/transition/push.rb', line 28

def state_class
  @state_class
end

Instance Method Details

#apply(stack) ⇒ Object

Apply the transition to the given stack.



36
37
38
39
40
41
42
43
44
45
# File 'lib/pushdown/transition/push.rb', line 36

def apply( stack )
  state = self.state_class.new( self.data )

  self.log.debug "pushing a new state: %p" % [ state ]
  stack.last.on_pause if stack.last
  stack.push( state )
  state.on_start

  return stack
end