Class: State

Inherits:
Object
  • Object
show all
Defined in:
state.rb

Overview

A State represents a state in a StateMachine, it may contain many transitions to other states and may have many transitions pointing to itself.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ State


12
13
14
15
# File 'state.rb', line 12

def initialize(args = {})
  self.name = args.fetch(:name)
  self.transitions = []
end

Instance Attribute Details

#nameString


6
7
8
# File 'state.rb', line 6

def name
  @name
end

#transitionsArray<Transition>

The collection of transitions going out of this state


10
11
12
# File 'state.rb', line 10

def transitions
  @transitions
end

Instance Method Details

#get_next_state(character) ⇒ Transition?

Gets the next state this character leads to. If there is no next state returns nil


27
28
29
30
31
32
33
# File 'state.rb', line 27

def get_next_state(character)
  transitions.each do |transition|
    is_valid = valid_transition?(transition, character)
    return transition if is_valid
  end
  nil
end

#push_transition(transition) ⇒ Array<Transition>

Adds a transition to this state


20
21
22
# File 'state.rb', line 20

def push_transition(transition)
  transitions.push(transition)
end

#valid_transition?(transition, character) ⇒ Boolean (protected)

Checks whether a given character is valid for a given transition


41
42
43
# File 'state.rb', line 41

def valid_transition?(transition, character)
  transition.valid?(character)
end