class WizardsCastle::Prompter

Public Instance Methods

ask(allowed_array,prompt_hash) click to toggle source
# File lib/wizards-castle/prompter.rb, line 4
def ask(allowed_array,prompt_hash)
  # prompt_hash is {:prompt,:success,:error}
  loop do
    emit prompt_hash[:prompt]
    input = gets.to_s.strip.upcase

    rv = nil
    if allowed_array.include?("DR") && input[0..1]=="DR"
      rv="DR"
    else
      rv=input[0] if allowed_array.include?(input[0])
    end

    if rv
      emit_with_arg(prompt_hash[:success],rv)
      return rv
    end

    emit prompt_hash[:error] 
  end
end
ask_for_anything(prompt_hash) click to toggle source
# File lib/wizards-castle/prompter.rb, line 47
def ask_for_anything(prompt_hash)
  emit prompt_hash[:prompt]
  rv = gets.to_s.strip.upcase
  emit_with_arg(prompt_hash[:success],rv)
  rv
end
ask_integer(min,max,prompt_hash) click to toggle source
# File lib/wizards-castle/prompter.rb, line 27
def ask_integer(min,max,prompt_hash)
  # prompt_hash is {:prompt,:success,:error,:out_of_range}
  # Negative ints are treated as errors.
  loop do
    emit prompt_hash[:prompt]
    input = gets.to_s.strip

    if input.match(/^\d+$/)
      i = input.to_i
      if i>=min && i<=max
        emit prompt_hash[:success]
        return i
      end
      emit prompt_hash[:out_of_range]
    else
      emit prompt_hash[:error]
    end
  end
end
confirm(target,prompt_hash) click to toggle source
# File lib/wizards-castle/prompter.rb, line 55
def confirm(target,prompt_hash)
  emit prompt_hash[:prompt]
  input = gets.to_s.strip.upcase
  if input.start_with?(target)
    emit prompt_hash[:confirmed]
    return true
  else
    emit prompt_hash[:denied]
    return false
  end
end

Private Instance Methods

emit(x) click to toggle source
# File lib/wizards-castle/prompter.rb, line 68
def emit x
  output = x.is_a?(Proc) ? x.call : x
  print output
end
emit_with_arg(x, arg) click to toggle source
# File lib/wizards-castle/prompter.rb, line 73
def emit_with_arg x, arg
  output = x.is_a?(Proc) ? x.call(arg) : x
  print output
end