class Thor::Shell::Basic

Public Instance Methods

ask(statement, *args) click to toggle source
# File lib/mb/thor_ext.rb, line 6
def ask(statement, *args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  color = args.first

  if options[:limited_to]
    ask_filtered(statement, color, options)
  else
    ask_simply(statement, color, options)
  end
end

Protected Instance Methods

ask_filtered(statement, color, options) click to toggle source
# File lib/mb/thor_ext.rb, line 36
def ask_filtered(statement, color, options)
  answer_set = options[:limited_to]
  correct_answer = nil
  until correct_answer
    answer = ask_simply("#{statement} #{answer_set.inspect}", color, options)
    correct_answer = answer_set.include?(answer) ? answer : nil
    answers = answer_set.map(&:inspect).join(", ")
    say("Your response must be one of: [#{answers}]. Please try again.") unless correct_answer
  end
  correct_answer
end
ask_simply(statement, color, options) click to toggle source
# File lib/mb/thor_ext.rb, line 19
def ask_simply(statement, color, options)
  default = options[:default]
  message = [statement, ("(#{default.inspect})" if default), nil].uniq.join(" ")
  say(message, color)
  result = stdin.gets

  return unless result

  result.strip!

  if default && result == ""
    default
  else
    result
  end
end