class Nabokov::Informator

This class is heavily based on the Interviewer class from the Danger gem The original link is github.com/danger/danger/blob/master/lib/danger/commands/init_helpers/interviewer.rb

Attributes

no_waiting[RW]
ui[RW]

Public Class Methods

new(cork_board) click to toggle source
# File lib/nabokov/helpers/informator.rb, line 9
def initialize(cork_board)
  @ui = cork_board
end

Public Instance Methods

ask_with_answers(question, possible_answers) click to toggle source
# File lib/nabokov/helpers/informator.rb, line 46
def ask_with_answers(question, possible_answers)
  ui.print("\n#{question}? [")
  print_possible_answers(possible_answers)
  answer = ""
  loop do
    show_prompt
    answer = read_answer(possible_answers)

    break if possible_answers.map(&:downcase).include? answer

    ui.print "\nPossible answers are ["
    print_possible_answers(possible_answers)
  end
  answer
end
error(message) click to toggle source
# File lib/nabokov/helpers/informator.rb, line 36
def error(message)
  ui.puts(message.red)
end
important(message) click to toggle source
# File lib/nabokov/helpers/informator.rb, line 25
def important(message)
  i = message.length + 8
  inform("-" * i)
  inform("--- " + message + " ---")
  inform("-" * i)
end
inform(message) click to toggle source
# File lib/nabokov/helpers/informator.rb, line 21
def inform(message)
  ui.puts(message.green)
end
say(message) click to toggle source
# File lib/nabokov/helpers/informator.rb, line 17
def say(message)
  ui.puts(message)
end
show_prompt() click to toggle source
# File lib/nabokov/helpers/informator.rb, line 13
def show_prompt
  ui.print("> ".bold.green)
end
wait_for_return() click to toggle source
# File lib/nabokov/helpers/informator.rb, line 40
def wait_for_return
  STDOUT.flush
  STDIN.gets unless @no_waiting
  ui.puts
end
warn(message) click to toggle source
# File lib/nabokov/helpers/informator.rb, line 32
def warn(message)
  ui.puts(message.yellow)
end

Private Instance Methods

print_possible_answers(possible_answers) click to toggle source
read_answer(possible_answers) click to toggle source
# File lib/nabokov/helpers/informator.rb, line 64
def read_answer(possible_answers)
  answer = @no_waiting ? possible_answers[0].downcase : STDIN.gets.downcase.chomp
  answer = "yes" if answer == "y"
  answer = "no" if answer == "n"

  # default to first answer
  if answer == ""
    answer = possible_answers[0].downcase
    ui.puts "Using: " + answer.yellow
  end
  answer
end