Class: Transition

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

Overview

A Transition represents a transition between two States in a StateMachine. It stores all valid characters for the transition and it's target state.

Constant Summary collapse

TYPES =

List of valid types a transition can be

%w{initial final}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Transition


20
21
22
23
24
# File 'transition.rb', line 20

def initialize(args = {})
  self.to_state = args.fetch(:to_state)
  self.valid_characters = args.fetch(:valid_characters)
  self.type = args.fetch(:type)
end

Instance Attribute Details

#to_stateState

The state this transition leads to


14
15
16
# File 'transition.rb', line 14

def to_state
  @to_state
end

#typeSymbol

The transition's type (see TYPES)


18
19
20
# File 'transition.rb', line 18

def type
  @type
end

#valid_charactersRegex, String

The collection of characters for which this transition is valid


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

def valid_characters
  @valid_characters
end

Instance Method Details

#final?Boolean

Checks if this transition ends in the starting node


46
47
48
# File 'transition.rb', line 46

def final?
  type == :final
end

#initial?Boolean

Checks if this transition comes from the starting node


40
41
42
# File 'transition.rb', line 40

def initial?
  type == :initial
end

#valid?(character) ⇒ Boolean

Checks whether this character is valid for this transition


29
30
31
32
33
34
35
36
# File 'transition.rb', line 29

def valid?(character)
  case character.class
    when String then
      valid_characters.any?(character)
    else
      valid_characters =~ character
  end
end