class Quna::Query

Basic user query.

Public Class Methods

new( default_answer = nil ) click to toggle source
# File lib/quna.rb, line 20
def initialize( default_answer = nil )
    @question = nil
    @default_answer = default_answer
    @answer = nil

    @opts = {
        no_answer_limit: nil,
    }
end

Public Instance Methods

ask( question ) click to toggle source

Ask generic question.

# File lib/quna.rb, line 92
def ask( question )
    set_question question
    answer = Readline.readline( prompt )
    raise( QunaInterrupt, prompt ) unless answer
    answer
end
ask_relaxed( question ) click to toggle source

Ask generic question without any requirements for answer.

# File lib/quna.rb, line 101
def ask_relaxed( question )
    set_question question
    answer = Readline.readline( prompt )
    answer
end
ask_yn( question ) click to toggle source

Ask y/n question until answer is received.

# File lib/quna.rb, line 38
def ask_yn( question )
    set_question question

    result = :no

    no_answer_count = 0

    loop do
        answer = Readline.readline( prompt_yn )
        result = case answer

                 when nil
                     raise( QunaInterrupt, @question )

                 when ""
                     if @default_answer
                         @default_answer
                     else
                         no_answer_count += 1
                         if @opts[ :no_answer_limit ] && no_answer_count >= @opts[ :no_answer_limit ]
                             raise( QunaNoAnswer, @question )
                         end
                         nil
                     end

                 else
                     lowcase = answer.downcase
                     case lowcase
                     when 'y', 'yes'; :yes
                     when 'n', 'no'; :no
                     else nil
                     end
                 end

        break if result
    end

    result
end
ask_yn_bool( question ) click to toggle source

Ask y/n question and return boolean result, i.e. TRUE for yes and FALSE for no.

# File lib/quna.rb, line 81
def ask_yn_bool( question )
    result = ask_yn( question )
    if result == :yes
        true
    else
        false
    end
end
prompt() click to toggle source

Create prompt string for generic question.

# File lib/quna.rb, line 123
def prompt
    "#{@question}? "
end
prompt_yn() click to toggle source

Create prompt string for y/n question.

# File lib/quna.rb, line 109
def prompt_yn
    options = nil
    if @default_answer == :yes
        options = "[Y/n]"
    elsif @default_answer == :no
        options = "[y/N]"
    else
        options = "[y/n]"
    end
    "#{@question} #{options}? "
end
set_question( question ) click to toggle source

Store asked question.

# File lib/quna.rb, line 32
def set_question( question )
    @question = question
end