Class: Strobe::CLI::Input::Menu
- Inherits:
-
Object
- Object
- Strobe::CLI::Input::Menu
- Defined in:
- lib/strobe/cli/input.rb
Defined Under Namespace
Classes: NoSuchOptionError
Instance Attribute Summary collapse
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#choices ⇒ Object
readonly
Returns the value of attribute choices.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
Instance Method Summary collapse
- #ask ⇒ Object
- #choice(name, &block) ⇒ Object
- #display ⇒ Object
-
#initialize(input) ⇒ Menu
constructor
A new instance of Menu.
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
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
7 8 9 |
# File 'lib/strobe/cli/input.rb', line 7 def actions @actions end |
#choices ⇒ Object (readonly)
Returns the value of attribute choices.
7 8 9 |
# File 'lib/strobe/cli/input.rb', line 7 def choices @choices end |
#input ⇒ Object (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
#ask ⇒ Object
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 |
#display ⇒ Object
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 |