Class: GermanNumbers::StateMachine::Machine
- Inherits:
-
Object
- Object
- GermanNumbers::StateMachine::Machine
- Defined in:
- lib/german_numbers/state_machine.rb
Instance Attribute Summary collapse
-
#states ⇒ Object
readonly
Returns the value of attribute states.
-
#transitions ⇒ Object
readonly
Returns the value of attribute transitions.
Instance Method Summary collapse
-
#initialize ⇒ Machine
constructor
A new instance of Machine.
- #state(state, can_be_initial: false, final: true, unique: false) ⇒ Object
- #transition(from:, to:) ⇒ Object
- #validate_state!(*states) ⇒ Object
Constructor Details
#initialize ⇒ Machine
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
#states ⇒ Object (readonly)
Returns the value of attribute states.
32 33 34 |
# File 'lib/german_numbers/state_machine.rb', line 32 def states @states end |
#transitions ⇒ Object (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, can_be_initial: false, final: true, unique: false) ⇒ Object
39 40 41 |
# File 'lib/german_numbers/state_machine.rb', line 39 def state(state, can_be_initial: false, final: true, unique: false) @states[state] = State.new(state, can_be_initial, 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) to.each do |s| @transitions[from] = @transitions[from] << s 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 |