Class: Quna::Query

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

Overview

Basic user query.

Instance Method Summary collapse

Constructor Details

#initialize(default_answer = nil) ⇒ Query

Returns a new instance of Query.



20
21
22
23
24
25
26
27
28
# 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

Instance Method Details

#ask(question) ⇒ Object

Ask generic question.

Raises:



92
93
94
95
96
97
# 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) ⇒ Object

Ask generic question without any requirements for answer.



101
102
103
104
105
# File 'lib/quna.rb', line 101

def ask_relaxed( question )
    set_question question
    answer = Readline.readline( prompt )
    answer
end

#ask_yn(question) ⇒ Object

Ask y/n question until answer is received.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object

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



81
82
83
84
85
86
87
88
# File 'lib/quna.rb', line 81

def ask_yn_bool( question )
    result = ask_yn( question )
    if result == :yes
        true
    else
        false
    end
end

#promptObject

Create prompt string for generic question.



123
124
125
# File 'lib/quna.rb', line 123

def prompt
    "#{@question}? "
end

#prompt_ynObject

Create prompt string for y/n question.



109
110
111
112
113
114
115
116
117
118
119
# 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) ⇒ Object

Store asked question.



32
33
34
# File 'lib/quna.rb', line 32

def set_question( question )
    @question = question
end