Class: BetweenTheSheets::Ask

Inherits:
Object
  • Object
show all
Defined in:
lib/between_the_sheets/ask.rb

Class Method Summary collapse

Class Method Details



3
4
5
6
# File 'lib/between_the_sheets/ask.rb', line 3

def self.print(output = "", options = {})
  $stdout.print output
  prompt(options)
end

.prompt(options = {}) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/between_the_sheets/ask.rb', line 13

def self.prompt(options = {})
  options = { to_int: false, match: nil }.merge(options)
  match = options[:match]

  input = gets.chomp.downcase
  raise ApplicationExit if input.match(/^q|quit|^e|exit/)
  raise ApplicationHelp if input.match(/^h|help/)

  if match && input.match(match).to_s.empty?
    input = loop do
      try = self.print("Invalid response. Choices are: " + match.to_s.gsub("?-mix:", "") + " ")
      break try if try.match(match)
    end
  end

  if options[:to_int]
    input.gsub(/\D/, "").to_i
  else
    input
  end
end

.puts(output = "", options = {}) ⇒ Object



8
9
10
11
# File 'lib/between_the_sheets/ask.rb', line 8

def self.puts(output = "", options = {})
  $stdout.puts output
  prompt(options)
end