class NxtStateMachine::State

Attributes

enum[RW]
index[RW]
initial[RW]
options[RW]
state_machine[RW]
transitions[RW]

Public Class Methods

new(enum, state_machine, **opts) click to toggle source
# File lib/nxt_state_machine/state.rb, line 5
def initialize(enum, state_machine, **opts)
  @enum = enum
  @state_machine = state_machine
  @initial = opts.delete(:initial)
  @transitions = []
  @options = opts.with_indifferent_access
  @index = opts.fetch(:index)

  ensure_index_not_occupied
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/nxt_state_machine/state.rb, line 44
def <=>(other)
  index <=> other.index
end
events() click to toggle source
# File lib/nxt_state_machine/state.rb, line 40
def events
  state_machine.events_for_state(enum)
end
first?() click to toggle source
# File lib/nxt_state_machine/state.rb, line 36
def first?
  sorted_states.first.index == index
end
last?() click to toggle source
# File lib/nxt_state_machine/state.rb, line 32
def last?
  sorted_states.last.index == index
end
next() click to toggle source
# File lib/nxt_state_machine/state.rb, line 27
def next
  current_index = sorted_states.index { |state| state.index == index }
  sorted_states[(current_index + 1) % sorted_states.size]
end
previous() click to toggle source
# File lib/nxt_state_machine/state.rb, line 22
def previous
  current_index = sorted_states.index { |state| state.index == index }
  sorted_states[(current_index - 1) % sorted_states.size]
end
to_s() click to toggle source
# File lib/nxt_state_machine/state.rb, line 18
def to_s
  enum.to_s
end

Private Instance Methods

ensure_index_not_occupied() click to toggle source
# File lib/nxt_state_machine/state.rb, line 54
def ensure_index_not_occupied
  state_with_same_index = state_machine.states.values.find { |state| state.index == index }
  return unless state_with_same_index

  raise StateWithSameIndexAlreadyRegistered, "The index #{index} is already occupied by state: #{state_with_same_index.enum}"
end
sorted_states() click to toggle source
# File lib/nxt_state_machine/state.rb, line 50
def sorted_states
  state_machine.states.values.sort_by(&:index)
end