Class: GermanNumbers::StateMachine::Machine

Inherits:
Object
  • Object
show all
Defined in:
lib/german_numbers/state_machine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMachine

Returns a new instance of Machine.



34
35
36
37
# File 'lib/german_numbers/state_machine.rb', line 34

def initialize
  @states = {}
  @transitions = Hash.new { [] }
end

Instance Attribute Details

#statesObject (readonly)

Returns the value of attribute states.



32
33
34
# File 'lib/german_numbers/state_machine.rb', line 32

def states
  @states
end

#transitionsObject (readonly)

Returns the value of attribute transitions.



32
33
34
# File 'lib/german_numbers/state_machine.rb', line 32

def transitions
  @transitions
end

Instance Method Details

#state(state, final: true, unique: false) ⇒ Object



39
40
41
# File 'lib/german_numbers/state_machine.rb', line 39

def state(state, final: true, unique: false)
  @states[state] = State.new(state, @states.empty?, final, unique)
end

#transition(from:, to:) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/german_numbers/state_machine.rb', line 43

def transition(from:, to:)
  to = [to].flatten
  validate_state!([from, to].flatten)
  to.each do
    @transitions[from] = @transitions[from] << _1
  end
end

#validate_state!(states) ⇒ Object



51
52
53
54
55
# File 'lib/german_numbers/state_machine.rb', line 51

def validate_state!(states)
  states.each do |state|
    raise StateError, "#{state} is unknown state" unless @states.include?(state)
  end
end