Class: StateMachine::State

Inherits:
Object
  • Object
show all
Defined in:
app/state_machine/state.rb

Overview

TODO: Rename to Select

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(selections, selected_index) ⇒ State

Returns a new instance of State.



6
7
8
9
10
11
# File 'app/state_machine/state.rb', line 6

def initialize(selections, selected_index)
  raise 'no selections' if selections.nil? || selections.empty?

  @selections = selections
  select(selected_index)
end

Instance Attribute Details

#selected_indexObject (readonly)

Returns the value of attribute selected_index.



4
5
6
# File 'app/state_machine/state.rb', line 4

def selected_index
  @selected_index
end

Instance Method Details

#action(window) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/state_machine/state.rb', line 13

def action(window)
  key = Key.new(window.getch)

  begin
    @next_state =
      case key.button
      when :up
        self.class.new(@selections, selected_index-1)
      when :down
        self.class.new(@selections, selected_index+1)
      when :enter
        selected_index == 0 ?
          NewSessionState.new(selected_index) :
          Existing.new(selected_index, @selections[selected_index])
      when :quit
        Quit.new(selected_index)
      else
        self
      end
  rescue => exception
    logger.info(exception)
    @next_state = self
  end
end

#next_stateObject



38
39
40
# File 'app/state_machine/state.rb', line 38

def next_state
  @next_state
end