class StateJacket::StateTransitionSystem

Attributes

transitions[R]

Public Class Methods

new() click to toggle source
# File lib/state_jacket/state_transition_system.rb, line 3
def initialize
  @transitions = {}
end

Public Instance Methods

add(state) click to toggle source
# File lib/state_jacket/state_transition_system.rb, line 11
def add(state)
  raise "states cannot be added after locking" if is_locked?
  if state.is_a?(Hash)
    from = state.keys.first.to_s
    transitions[from] = make_states(state.values.first)
  else
    transitions[state.to_s] = nil
  end
end
can_transition?(from_to) click to toggle source
# File lib/state_jacket/state_transition_system.rb, line 32
def can_transition?(from_to)
  raise ArgumentError.new("from_to should contain a single transition") unless from_to.size == 1
  from = from_to.keys.first.to_s
  to = make_states(from_to.values.first)
  allowed_states =  transitions[from] || []
  (to & allowed_states).length == to.length
end
is_locked?() click to toggle source
# File lib/state_jacket/state_transition_system.rb, line 28
def is_locked?
  !!@locked
end
is_state?(state) click to toggle source
# File lib/state_jacket/state_transition_system.rb, line 52
def is_state?(state)
  transitions.keys.include?(state.to_s)
end
is_terminator?(state) click to toggle source
# File lib/state_jacket/state_transition_system.rb, line 56
def is_terminator?(state)
  terminators.include?(state.to_s)
end
is_transitioner?(state) click to toggle source
# File lib/state_jacket/state_transition_system.rb, line 60
def is_transitioner?(state)
  transitioners.include?(state.to_s)
end
lock() click to toggle source
# File lib/state_jacket/state_transition_system.rb, line 21
def lock
  return true if is_locked?
  transitions.freeze
  transitions.values.each { |value| value.freeze unless value.nil? }
  @locked = true
end
states() click to toggle source
# File lib/state_jacket/state_transition_system.rb, line 40
def states
  transitions.keys
end
terminators() click to toggle source
# File lib/state_jacket/state_transition_system.rb, line 48
def terminators
  transitions.keys.select { |state| transitions[state] == nil }
end
to_h() click to toggle source
# File lib/state_jacket/state_transition_system.rb, line 7
def to_h
  transitions.dup
end
transitioners() click to toggle source
# File lib/state_jacket/state_transition_system.rb, line 44
def transitioners
  transitions.keys.select { |state| transitions[state] != nil }
end

Private Instance Methods

make_states(values) click to toggle source
# File lib/state_jacket/state_transition_system.rb, line 68
def make_states(values)
  values = [values.to_s] unless values.respond_to?(:map)
  values = values.map(&:to_s)
  values.each { |value| transitions[value] ||= nil } unless transitions.frozen?
  values
end