Class: NxtStateMachine::State

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/nxt_state_machine/state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enum, state_machine, **opts) ⇒ State

Returns a new instance of State.



5
6
7
8
9
10
11
12
13
14
# 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

Instance Attribute Details

#enumObject

Returns the value of attribute enum.



16
17
18
# File 'lib/nxt_state_machine/state.rb', line 16

def enum
  @enum
end

#indexObject

Returns the value of attribute index.



16
17
18
# File 'lib/nxt_state_machine/state.rb', line 16

def index
  @index
end

#initialObject

Returns the value of attribute initial.



16
17
18
# File 'lib/nxt_state_machine/state.rb', line 16

def initial
  @initial
end

#optionsObject

Returns the value of attribute options.



16
17
18
# File 'lib/nxt_state_machine/state.rb', line 16

def options
  @options
end

#state_machineObject

Returns the value of attribute state_machine.



16
17
18
# File 'lib/nxt_state_machine/state.rb', line 16

def state_machine
  @state_machine
end

#transitionsObject

Returns the value of attribute transitions.



16
17
18
# File 'lib/nxt_state_machine/state.rb', line 16

def transitions
  @transitions
end

Instance Method Details

#<=>(other) ⇒ Object



44
45
46
# File 'lib/nxt_state_machine/state.rb', line 44

def <=>(other)
  index <=> other.index
end

#eventsObject



40
41
42
# File 'lib/nxt_state_machine/state.rb', line 40

def events
  state_machine.events_for_state(enum)
end

#first?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/nxt_state_machine/state.rb', line 36

def first?
  sorted_states.first.index == index
end

#last?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/nxt_state_machine/state.rb', line 32

def last?
  sorted_states.last.index == index
end

#nextObject



27
28
29
30
# 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

#previousObject



22
23
24
25
# 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_sObject



18
19
20
# File 'lib/nxt_state_machine/state.rb', line 18

def to_s
  enum.to_s
end