Class: Tracker::Cli::View::Select

Inherits:
Object
  • Object
show all
Defined in:
lib/tracker/cli/view/select.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prompt, options) ⇒ Select

Returns a new instance of Select.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tracker/cli/view/select.rb', line 7

def initialize(prompt, options)
  options.each_with_index do |option, index|
    $stderr.print "(#{index + 1}) #{block_given? ? yield(option) : option}\n"
  end
  
  loop do
    $stderr.print "\n#{prompt} "
    user_input = $stdin.gets.chomp
    index = user_input.to_i
    selection = options[index - 1]
    
    if index.to_s != user_input
      $stderr.print "Please make a selection.\n"
    elsif selection.nil?
      $stderr.print "Please select one of the options above.\n"
    else
      @selection = selection
      break
    end
  end
  
  $stderr.print "\n"
end

Instance Attribute Details

#selectionObject (readonly)

Returns the value of attribute selection.



5
6
7
# File 'lib/tracker/cli/view/select.rb', line 5

def selection
  @selection
end