quna 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/quna.rb ADDED
@@ -0,0 +1,129 @@
1
+ require 'readline'
2
+
3
+ # Questions and answers module.
4
+ #
5
+ # Example:
6
+ #
7
+ # quna = Quna::Query.new( :yes )
8
+ # ans = quna.ask( "Ready to scan" )
9
+ # do_scan if ans
10
+ #
11
+ module Quna
12
+
13
+ class QunaInterrupt < RuntimeError; end
14
+ class QunaNoAnswer < RuntimeError; end
15
+
16
+
17
+ # Basic user query.
18
+ class Query
19
+
20
+ def initialize( default_answer = nil )
21
+ @question = nil
22
+ @default_answer = default_answer
23
+ @answer = nil
24
+
25
+ @opts = {
26
+ no_answer_limit: nil,
27
+ }
28
+ end
29
+
30
+
31
+ # Store asked question.
32
+ def set_question( question )
33
+ @question = question
34
+ end
35
+
36
+
37
+ # Ask y/n question until answer is received.
38
+ def ask_yn( question )
39
+ set_question question
40
+
41
+ result = :no
42
+
43
+ no_answer_count = 0
44
+
45
+ loop do
46
+ answer = Readline.readline( prompt_yn )
47
+ result = case answer
48
+
49
+ when nil
50
+ raise( QunaInterrupt, @question )
51
+
52
+ when ""
53
+ if @default_answer
54
+ @default_answer
55
+ else
56
+ no_answer_count += 1
57
+ if @opts[ :no_answer_limit ] && no_answer_count >= @opts[ :no_answer_limit ]
58
+ raise( QunaNoAnswer, @question )
59
+ end
60
+ nil
61
+ end
62
+
63
+ else
64
+ lowcase = answer.downcase
65
+ case lowcase
66
+ when 'y', 'yes'; :yes
67
+ when 'n', 'no'; :no
68
+ else nil
69
+ end
70
+ end
71
+
72
+ break if result
73
+ end
74
+
75
+ result
76
+ end
77
+
78
+
79
+ # Ask y/n question and return boolean result, i.e. TRUE for
80
+ # yes and FALSE for no.
81
+ def ask_yn_bool( question )
82
+ result = ask_yn( question )
83
+ if result == :yes
84
+ true
85
+ else
86
+ false
87
+ end
88
+ end
89
+
90
+
91
+ # Ask generic question.
92
+ def ask( question )
93
+ set_question question
94
+ answer = Readline.readline( prompt )
95
+ raise( QunaInterrupt, prompt ) unless answer
96
+ answer
97
+ end
98
+
99
+
100
+ # Ask generic question without any requirements for answer.
101
+ def ask_relaxed( question )
102
+ set_question question
103
+ answer = Readline.readline( prompt )
104
+ answer
105
+ end
106
+
107
+
108
+ # Create prompt string for y/n question.
109
+ def prompt_yn
110
+ options = nil
111
+ if @default_answer == :yes
112
+ options = "[Y/n]"
113
+ elsif @default_answer == :no
114
+ options = "[y/N]"
115
+ else
116
+ options = "[y/n]"
117
+ end
118
+ "#{@question} #{options}? "
119
+ end
120
+
121
+
122
+ # Create prompt string for generic question.
123
+ def prompt
124
+ "#{@question}? "
125
+ end
126
+
127
+ end
128
+
129
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,6 @@
1
+ class Quna
2
+ VERSION = "0.0.2"
3
+ def Quna.version
4
+ Quna::VERSION
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quna
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Tero Isannainen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ Quna is a library for making it simple to query
15
+ interactively answers from users.
16
+ email: tero.isannainen@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files:
20
+ - README.rdoc
21
+ - CHANGELOG.rdoc
22
+ files:
23
+ - CHANGELOG.rdoc
24
+ - LICENSE
25
+ - README.rdoc
26
+ - doc/Quna.html
27
+ - doc/Quna/Query.html
28
+ - doc/Quna/QunaInterrupt.html
29
+ - doc/Quna/QunaNoAnswer.html
30
+ - doc/Rubu.html
31
+ - doc/_index.html
32
+ - doc/class_list.html
33
+ - doc/css/common.css
34
+ - doc/css/full_list.css
35
+ - doc/css/style.css
36
+ - doc/file.CHANGELOG.html
37
+ - doc/file.README.html
38
+ - doc/file_list.html
39
+ - doc/frames.html
40
+ - doc/index.html
41
+ - doc/js/app.js
42
+ - doc/js/full_list.js
43
+ - doc/js/jquery.js
44
+ - doc/method_list.html
45
+ - doc/top-level-namespace.html
46
+ - lib/quna.rb
47
+ - lib/version.rb
48
+ homepage:
49
+ licenses:
50
+ - Ruby
51
+ metadata: {}
52
+ post_install_message: Check README...
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 1.9.3
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.2.5
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Quna is Questions and Answers utility.
71
+ test_files: []