qanda 0.2.0
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.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +20 -0
- data/LICENSE.txt +21 -0
- data/README.md +8 -0
- data/lib/qanda.rb +9 -0
- data/lib/qanda/version.rb +3 -0
- data/lib/qanda_exceptions.rb +13 -0
- data/lib/question/boolean.rb +24 -0
- data/lib/question/multiple_choice.rb +70 -0
- data/lib/question/question.rb +31 -0
- data/lib/question/text.rb +13 -0
- data/lib/questionnaire.rb +33 -0
- data/qanda.gemspec +22 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a6b1fdeb78b7c55eafd6212ffa7ce2ac939b88e5
|
4
|
+
data.tar.gz: cac1aa19f4c1ca1fd44066b78a3c06086d9f3874
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9b5ba4e407117a11cc2a4a61786e9eb47618c0d251c5504d258dfd9eb098b2e25777b3907263b3475982eec60e05d0a9de19df28e00c1ffcadd5ee2cba42c03b
|
7
|
+
data.tar.gz: da1d64ccfafd5e3985a73f7625fe12f560b48462e3a44d427ac6f7ed694814ac1a0f70d6352e2da0127150205872b1a4f4b5766b3deb58811e79bec59830253d
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Wesley Boynton
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# QandA
|
2
|
+
|
3
|
+
QandA is a cute little gem for quickly gathering answers to questions that require user-submitted data. Useful for setup and install scripts, command line shells, and other fun stuff.
|
4
|
+
|
5
|
+
## License
|
6
|
+
|
7
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
8
|
+
|
data/lib/qanda.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module QandA
|
2
|
+
module Question
|
3
|
+
class Boolean < Question
|
4
|
+
TRUES = ['yes', 'y', 'true', 't']
|
5
|
+
FALSES = ['no', 'n', 'false', 'f']
|
6
|
+
|
7
|
+
def initialize(message = '', default = nil)
|
8
|
+
message = "#{message} (y/n)"
|
9
|
+
super(message: message, default: default)
|
10
|
+
@validation = Proc.new do |answer|
|
11
|
+
answer = answer.downcase.strip
|
12
|
+
TRUES.include?(answer) || FALSES.include?(answer)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def ask
|
17
|
+
super
|
18
|
+
@answer = answer.downcase.strip
|
19
|
+
@answer = true if TRUES.include?(@answer)
|
20
|
+
@answer = false if FALSES.include?(@answer)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module QandA
|
2
|
+
module Question
|
3
|
+
class MultipleChoice < Question
|
4
|
+
NOT_ENOUGH_OPTIONS_ERR = 'A multiple-choice question must have an array with at least two elements.'
|
5
|
+
DEFAULT_IS_INTEGER_ERR = 'A multiple-choice default must be a valid index for its array of choices.'
|
6
|
+
MINIMUM_NO_ZERO_ERR = 'A multiple-choice minimum cannot be zero, negative, or greater than the choice count.'
|
7
|
+
MAXIMUM_NO_ZERO_ERR = 'A multiple-choice maximum cannot be zero, negative, or greater than/equal to the choice count.'
|
8
|
+
MIN_GREATER_THAN_MAX = 'A multiple-choice minimum cannot be greater than the maximum.'
|
9
|
+
|
10
|
+
def initialize(choices, message = '', default = nil, minimum = 1, maximum = nil)
|
11
|
+
@maximum = maximum.nil? ? minimum : maximum
|
12
|
+
@minimum = minimum
|
13
|
+
@choices = choices
|
14
|
+
raise InvalidQuestionError.new(NOT_ENOUGH_OPTIONS_ERR) unless choices.is_a?(Array) && choices.size > 1
|
15
|
+
raise InvalidQuestionError.new(DEFAULT_IS_INTEGER_ERR) unless default.nil? || answer_string_is_valid?(default)
|
16
|
+
raise InvalidQuestionError.new(@MAXIMUM_NO_ZERO_ERR) unless @minimum > 0 && @maximum <= choices.size
|
17
|
+
raise InvalidQuestionError.new(@MINIMUM_NO_ZERO_ERR) unless @maximum > 0 && @minimum <= choices.size - 1
|
18
|
+
raise InvalidQuestionError.new(MIN_GREATER_THAN_MAX) unless @maximum >= @minimum
|
19
|
+
super(message: message, default: default)
|
20
|
+
@validation = Proc.new { |answer| answer_string_is_valid?(answer) }
|
21
|
+
end
|
22
|
+
|
23
|
+
def answer_string_is_valid?(answer)
|
24
|
+
answer = answer.to_s.split(',').map { |i| i.strip }
|
25
|
+
valid = true
|
26
|
+
valid = false if answer.length > @maximum
|
27
|
+
valid = false if answer.length < @minimum
|
28
|
+
if valid
|
29
|
+
answer.each do |answer|
|
30
|
+
unless answer.to_i.to_s == answer && @choices.size > answer.to_i - 1
|
31
|
+
valid = false
|
32
|
+
break
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
valid
|
37
|
+
end
|
38
|
+
|
39
|
+
def prompt
|
40
|
+
puts "\n"
|
41
|
+
if @maximum == 1
|
42
|
+
puts "Select only one answer:"
|
43
|
+
elsif @maximum == @minimum
|
44
|
+
puts "Select exactly #{@maximum} answers, separated by commas:"
|
45
|
+
else
|
46
|
+
puts "Select between #{@minimum} and #{@maximum} answers, separated by commas:"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def ask
|
51
|
+
super do
|
52
|
+
prompt
|
53
|
+
@choices.each_with_index do |c, i|
|
54
|
+
puts "[#{i+1}] #{c}"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
@answer = [*@answer.to_s.split(',').map { |i| i.strip.to_i - 1 }]
|
58
|
+
end
|
59
|
+
|
60
|
+
def answer
|
61
|
+
arr = []
|
62
|
+
@answer.each do |i|
|
63
|
+
arr << @choices[i].to_s
|
64
|
+
end
|
65
|
+
arr
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module QandA
|
2
|
+
module Question
|
3
|
+
class Question
|
4
|
+
def initialize(message: '', default: nil, &validation)
|
5
|
+
@msg = message
|
6
|
+
@default = default
|
7
|
+
@validation ||= validation ? validation : nil
|
8
|
+
@answer = nil
|
9
|
+
end
|
10
|
+
|
11
|
+
def ask(&block)
|
12
|
+
yield if block_given?
|
13
|
+
str = "#{@msg}"
|
14
|
+
str << " [#{@default}]" if @default
|
15
|
+
str << ': '
|
16
|
+
while @answer == nil do
|
17
|
+
print str
|
18
|
+
a = $stdin.gets.chomp
|
19
|
+
a = @default if @default && a.empty?
|
20
|
+
@answer = a unless @validation
|
21
|
+
@answer = a if @validation && instance_exec(a, &@validation)
|
22
|
+
puts "Invalid answer, try again" unless @answer
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def answer
|
27
|
+
@answer
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module QandA
|
2
|
+
class Questionnaire
|
3
|
+
TYPE_REGISTER = {
|
4
|
+
text: QandA::Question::Text,
|
5
|
+
multiple_choice: QandA::Question::MultipleChoice,
|
6
|
+
boolean: QandA::Question::Boolean,
|
7
|
+
|
8
|
+
}
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@questions = {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_question(key, type, *args)
|
15
|
+
type = type.to_sym
|
16
|
+
unless TYPE_REGISTER.has_key?(type)
|
17
|
+
raise NoSuchQuestionTypeError.new("No such question type '#{type}'")
|
18
|
+
end
|
19
|
+
@questions[key.to_sym] = TYPE_REGISTER[type].new(*args)
|
20
|
+
end
|
21
|
+
|
22
|
+
def run
|
23
|
+
@questions.each do |key, q|
|
24
|
+
q.ask
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def answers
|
29
|
+
Hash[@questions.map { |key, q| [key, q.answer] }]
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
data/qanda.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'qanda/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "qanda"
|
8
|
+
spec.version = Qanda::VERSION
|
9
|
+
spec.authors = ["Wesley Boynton"]
|
10
|
+
spec.email = ["wes@boynton.io"]
|
11
|
+
|
12
|
+
spec.summary = %q{A cute little library for quickly gathering answers to questions requiring user input.}
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: qanda
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wesley Boynton
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- wes@boynton.io
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- lib/qanda.rb
|
54
|
+
- lib/qanda/version.rb
|
55
|
+
- lib/qanda_exceptions.rb
|
56
|
+
- lib/question/boolean.rb
|
57
|
+
- lib/question/multiple_choice.rb
|
58
|
+
- lib/question/question.rb
|
59
|
+
- lib/question/text.rb
|
60
|
+
- lib/questionnaire.rb
|
61
|
+
- qanda.gemspec
|
62
|
+
homepage:
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.4.5.1
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: A cute little library for quickly gathering answers to questions requiring
|
86
|
+
user input.
|
87
|
+
test_files: []
|