Class: Strobe::CLI::Input::Menu

Inherits:
Object
  • Object
show all
Defined in:
lib/strobe/cli/input.rb

Defined Under Namespace

Classes: NoSuchOptionError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Menu

Returns a new instance of Menu.



9
10
11
12
13
# File 'lib/strobe/cli/input.rb', line 9

def initialize(input)
  @input = input
  @choices = []
  @actions = {}
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



7
8
9
# File 'lib/strobe/cli/input.rb', line 7

def actions
  @actions
end

#choicesObject (readonly)

Returns the value of attribute choices.



7
8
9
# File 'lib/strobe/cli/input.rb', line 7

def choices
  @choices
end

#inputObject (readonly)

Returns the value of attribute input.



7
8
9
# File 'lib/strobe/cli/input.rb', line 7

def input
  @input
end

Instance Method Details

#askObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/strobe/cli/input.rb', line 26

def ask
  display

  begin
    input.print "?  "

    option = input.gets.strip.to_i
    if option < 1 || option > choices.length
      input.puts "Please enter a number between 1 and #{choices.length}"
      raise NoSuchOptionError
    end

    actions[choices[option - 1]].call
  rescue NoSuchOptionError
    retry
  end
end

#choice(name, &block) ⇒ Object



15
16
17
18
# File 'lib/strobe/cli/input.rb', line 15

def choice(name, &block)
  choices << name
  actions[name] = block
end

#displayObject



20
21
22
23
24
# File 'lib/strobe/cli/input.rb', line 20

def display
  choices.each_with_index do |choice, i|
    input.puts "#{i+1}. #{choice}"
  end
end