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

Returns a new instance of 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

Returns the state's name or identifier.

Returns:

  • (String)

    the state's name or identifier


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

def name
  @name
end

#transitionsArray<Transition>

The collection of transitions going out of this state

Returns:


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

Parameters:

  • character (String)

    the character that is going to be processed

Returns:

  • (Transition, nil)

    the transition this character leads to


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

Parameters:

  • transition (Transition)

    the transition that is going to be added

Returns:

  • (Array<Transition>)

    this state's transitions


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

Parameters:

  • transition (Transition)

    the transition that is going to be checked

  • character (String)

    the character that will be processed

Returns:

  • (Boolean)

    is this transition valid for this character


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

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