Class: Minimus

Inherits:
Object
  • Object
show all
Defined in:
lib/minimus.rb,
lib/version.rb

Constant Summary collapse

TransitionError =
Class.new(StandardError)
VERSION =
'0.1.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*states) ⇒ Minimus

Returns a new instance of Minimus.



10
11
12
13
14
15
# File 'lib/minimus.rb', line 10

def initialize(*states)
  @states = states
  @initial_state = states.first
  @current_state = initial_state
  @possibilities = {}
end

Instance Attribute Details

#current_stateObject

Returns the value of attribute current_state.



8
9
10
# File 'lib/minimus.rb', line 8

def current_state
  @current_state
end

#initial_stateObject (readonly)

Returns the value of attribute initial_state.



7
8
9
# File 'lib/minimus.rb', line 7

def initial_state
  @initial_state
end

#possibilitiesObject (readonly)

Returns the value of attribute possibilities.



7
8
9
# File 'lib/minimus.rb', line 7

def possibilities
  @possibilities
end

#statesObject (readonly)

Returns the value of attribute states.



7
8
9
# File 'lib/minimus.rb', line 7

def states
  @states
end

Instance Method Details

#can(state, possible:) ⇒ Object



34
35
36
37
38
39
# 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) ⇒ Object



17
18
19
20
21
22
# 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) ⇒ Object



24
25
26
27
28
29
30
31
32
# 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