Module: EverydayCliUtils::Ask

Defined in:
lib/everyday-cli-utils/ask.rb

Class Method Summary collapse

Class Method Details

.ask(question, options, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/everyday-cli-utils/ask.rb', line 13

def self::ask(question, options, &block)
  val = '-1'
  while true
    print "#{question}\n\n#{setup_options(options)}\n\n"
    val = Readline.readline("Please enter your choice (1 - #{options.count}): ", true)
    if !(val =~ /^\d+$/).nil? && (1..options.count).include?(val.to_i)
      break
    end
    print "\n\nYou must enter an integer between 1 and #{options.count}. Please try again.\n\n"
  end
  block.call(options[val.to_i - 1])
end

.ask_yn(question, &block) ⇒ Object



26
27
28
29
# File 'lib/everyday-cli-utils/ask.rb', line 26

def self::ask_yn(question, &block)
  resp = Readline.readline("#{question} (yes/no) ", true)
  block.call(resp.downcase == 'yes' || resp.downcase == 'y')
end

.hash_to_options(hash, extra = []) ⇒ Object



31
32
33
# File 'lib/everyday-cli-utils/ask.rb', line 31

def self::hash_to_options(hash, extra = [])
  hash.keys + extra
end

.setup_options(options) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/everyday-cli-utils/ask.rb', line 5

def self::setup_options(options)
  mapped = []
  options.each_with_index { |v, k|
    mapped << "#{k+1}) #{v.to_s}"
  }
  mapped.join("\n")
end