class Minimus

Constants

TransitionError
VERSION

Attributes

current_state[RW]
initial_state[R]
possibilities[R]
states[R]

Public Class Methods

new(*states) click to toggle source
# File lib/minimus.rb, line 10
def initialize(*states)
  @states = states
  @initial_state = states.first
  @current_state = initial_state
  @possibilities = {}
end

Public Instance Methods

can(state, possible:) click to toggle source
# File lib/minimus.rb, line 34
def can(state, possible:)
  possible_array = Array(possible)
  return unless possible_array.all? { |value| states.include?(value) }

  possibilities[state] = possible_array
end
move(new_state) { || ... } click to toggle source
# File lib/minimus.rb, line 17
def move(new_state)
  return false unless move_possible?(new_state)
  self.current_state = new_state

  yield if block_given?
end
move!(new_state) { || ... } click to toggle source
# File lib/minimus.rb, line 24
def move!(new_state)
  unless move_possible?(new_state)
    raise TransitionError, %(unallowed transition from
       #{current_state} to #{new_state})
  end
  self.current_state = new_state

  yield if block_given?
end

Private Instance Methods

move_possible?(new_state) click to toggle source
# File lib/minimus.rb, line 43
def move_possible?(new_state)
  if state_possibilities = possibilities[current_state]
    state_possibilities.include?(new_state)
  else
    states[states.index(current_state) + 1] == new_state
  end
end