questions 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41e385aa0a2fa87149846bfa99223235a5910887
4
- data.tar.gz: 5aa6e4a359aad4dc0bfa7232b79daabc4a746934
3
+ metadata.gz: 5d41564d3b29b1c64399a5f86e375254943bdc87
4
+ data.tar.gz: c0cd3511a6de5d9e36969a7e68093344672aa63e
5
5
  SHA512:
6
- metadata.gz: c1056f0f6f023509511d95659796f301be00924d129e2c2a38cdffed01df1899b2adf275367c1e662c5dfc959bd2c874a323fce5ce66a2dae997ee013b145d08
7
- data.tar.gz: c74a6eca07e5d9a80327b2df544bae4bd9adfe042479a37eb9e1d109266d1b781a2d10022842d990a69866a7e6674bd2db36a9be8f96b2d11cbc3eea60ddddfa
6
+ metadata.gz: cb923ed4048e62eb3e7ec943033d5a4fbe2d3b3004d76ed07c3e62c2acec75fb6b04e430b0d12c2bbb109be70b2954abc0897fd3f9363b3c3edb1ba29daef970
7
+ data.tar.gz: 82f7f2b6ddb1543148030a9dd4ebcbf933f364775cb39a56f5be6bc44b70799c1e8abf877b72bcdb89273703b47a49395c20f38f3ff2ff3da1b916f63ff4190c
data/.rspec CHANGED
@@ -1,2 +1 @@
1
1
  --color
2
- --format documentation
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.0.2
@@ -1,6 +1,14 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Questions
4
+ # Represents an answer.
5
+ #
6
+ # An answer has an indicator which will be used to detect which answer is selected by user. Indicators are the first
7
+ # few letters of its instruction. User can identify an
8
+ # indicator by its surrounded square brackets. After that the rest of the instruction will be displayed.
9
+ #
10
+ # Answers can be active or inactive. Only active ansers will be displayed, inactive ones will be hidden.
11
+ # Answers can be special, then the indicator is in uppercase letters set.
4
12
  class Answer
5
13
  SPECIAL_ENDINGS = [:all]
6
14
 
@@ -121,6 +129,7 @@ module Questions
121
129
  # Answer.new(overwrite: false).to_s #=> ""
122
130
  def to_s
123
131
  return nil if inactive?
132
+ indicator = indicator()
124
133
  instruction_without_indicator = instruction.to_s[indicator.length..-1]
125
134
  humanized = instruction_without_indicator.gsub("_", " ")
126
135
  "[#{indicator}]#{humanized}"
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Questions
4
+ # Answers manages many answers. Each item is a Answer object in this collection.
4
5
  class Answers < Array
5
6
  # Instantiates a new Answers object.
6
7
  #
@@ -78,7 +79,7 @@ module Questions
78
79
  when Array
79
80
  arg.each { |arg| self << arg } # call as Answer or Symbol
80
81
  when Hash
81
- self << convert_hash_to_answers(select_symbols(arg)) # call as Array
82
+ self << AnswersHelper.convert_hash_to_answers(AnswersHelper.select_symbols(arg)) # call as Array
82
83
  end
83
84
  self
84
85
  end
@@ -108,9 +109,9 @@ module Questions
108
109
  def update_indicators_for_uniqueness!
109
110
  return if has_unique_indicators?
110
111
 
111
- each_with_index do |answer, i|
112
- other_indicators = first(i).map(&:indicator)
113
- answer.indicator = free_indicator_of(answer, used_indicators: other_indicators)
112
+ each_with_index do |answer, num|
113
+ other_indicators = first(num).map(&:indicator)
114
+ answer.indicator = AnswersHelper.free_indicator_of(answer, used_indicators: other_indicators)
114
115
  end
115
116
  end
116
117
 
@@ -135,6 +136,7 @@ module Questions
135
136
  # answers.indicators #=> [:a, :A, :ab]
136
137
  # answers.has_unique_indicators? #=> true
137
138
  def has_unique_indicators?
139
+ indicators = indicators()
138
140
  indicators == indicators.uniq
139
141
  end
140
142
 
@@ -151,46 +153,5 @@ module Questions
151
153
  update_indicators_for_uniqueness!
152
154
  map(&:to_s).compact.join(", ")
153
155
  end
154
-
155
- private
156
-
157
- # {"abc" => true, abort: true, overwrite: false} => {abort: true}
158
- def select_symbols(args)
159
- args.select { |inst, value| inst.is_a?(Symbol) }
160
- end
161
-
162
- # {abort: true} => [Answer.new(:abort => true)]
163
- def convert_hash_to_answers(answers)
164
- answers.reduce([]) { |array, (inst, value)| array << Answer.new({inst => value}) }
165
- end
166
-
167
- # :symbol => Answer.new(:symbol)
168
- # Answer.new(:symbol) => Answer.new(:symbol)
169
- # "string" => raise ArgumentError
170
- def convert_symbol_to_answer_or_send_through(arg)
171
- case arg
172
- when Answer then arg
173
- when Symbol then Answer.new(arg)
174
- else
175
- raise ArgumentError
176
- end
177
- end
178
-
179
- # Gets first free indicator for `answer` which isn't used by `used_indicators`.
180
- def free_indicator_of answer, opts={}
181
- opts = {:used_indicators => []}.merge(opts)
182
- used_indicators = opts[:used_indicators]
183
-
184
- i = 1
185
- loop do
186
- indicator = answer.indicator(i)
187
- if used_indicators.include? indicator
188
- i+=1
189
- redo # next try
190
- else
191
- return indicator
192
- end
193
- end
194
- end
195
156
  end
196
157
  end
@@ -65,8 +65,9 @@ module Questions
65
65
  #
66
66
  # @return [Symbol] selected answer
67
67
  def ask
68
+ answers = answers()
68
69
  raise "You have to set answers" if answers.empty?
69
- answer = gets "#{@question} #{answers}"
70
+ answer = UserInput.get "#{@question} #{answers}"
70
71
  answers[answer.to_sym].instruction || ask
71
72
  end
72
73
 
@@ -91,14 +92,5 @@ module Questions
91
92
  question.answers = answers
92
93
  question.ask
93
94
  end
94
-
95
- private
96
-
97
- # Prints `msg` and reads user input
98
- def gets msg
99
- STDOUT.print("#{msg} ")
100
- STDOUT.flush
101
- STDIN.gets.chomp
102
- end
103
95
  end
104
96
  end
data/lib/questions.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require "util/user_input"
2
+ require "util/answers_helper"
3
+
1
4
  require "questions/version"
2
5
  require "questions/answer"
3
6
  require "questions/answers"
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ module AnswersHelper
4
+ # {"abc" => true, abort: true, overwrite: false} => {abort: true}
5
+ def self.select_symbols(args)
6
+ args.select { |inst, value| inst.is_a?(Symbol) }
7
+ end
8
+
9
+ # {abort: true} => [Answer.new(:abort => true)]
10
+ def self.convert_hash_to_answers(answers)
11
+ answers.reduce([]) { |array, (inst, value)| array << Answer.new({inst => value}) }
12
+ end
13
+
14
+ # :symbol => Answer.new(:symbol)
15
+ # Answer.new(:symbol) => Answer.new(:symbol)
16
+ # "string" => raise ArgumentError
17
+ def self.convert_symbol_to_answer_or_send_through(arg)
18
+ case arg
19
+ when Answer then arg
20
+ when Symbol then Answer.new(arg)
21
+ else
22
+ raise ArgumentError
23
+ end
24
+ end
25
+
26
+ # Gets first free indicator for `answer` which isn't used by `used_indicators`.
27
+ def self.free_indicator_of answer, opts={}
28
+ opts = {:used_indicators => []}.merge(opts)
29
+ used_indicators = opts[:used_indicators]
30
+
31
+ first_chars = 1
32
+ loop do
33
+ indicator = answer.indicator(first_chars)
34
+ if used_indicators.include? indicator
35
+ first_chars += 1
36
+ redo # next try
37
+ else
38
+ return indicator
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ module UserInput
4
+ # Prints `msg` and reads user input
5
+ def self.get msg
6
+ STDOUT.print("#{msg} ")
7
+ STDOUT.flush
8
+ STDIN.gets.chomp
9
+ end
10
+ end
@@ -184,72 +184,4 @@ describe Answers do
184
184
  its(:to_s) { should == "[a]bort, [ab]c" }
185
185
  end
186
186
  end
187
-
188
- describe "Private:" do
189
- describe "#select_symbols" do
190
- let(:hash) { {"abc" => true, abort: true, overwrite: false} }
191
- it "should return {abort: true, overwrite: false}" do
192
- subject.send(:select_symbols, hash).should == { abort: true, overwrite: false }
193
- end
194
- end
195
-
196
- describe "#convert_hash_to_answers" do
197
- let(:hash) { {abort: true, overwrite: false} }
198
- subject do
199
- Answers.new.send(:convert_hash_to_answers, hash)
200
- end
201
- it "each entry should be kind of Answer" do
202
- subject.all? { |answer| answer.kind_of? Answer }
203
- end
204
- its("first.instruction") { should == :abort }
205
- its("first.true?") { should be_true }
206
- its("last.instruction") { should == :overwrite }
207
- its("last.true?") { should be_false }
208
- end
209
-
210
- describe "#convert_symbol_to_answer_or_send_through" do
211
- subject do
212
- Answers.new.send(:convert_symbol_to_answer_or_send_through, parameter)
213
- end
214
- context "when parameter is a Symbol" do
215
- let(:parameter) { :abort }
216
- it { should be_kind_of Answer }
217
- its(:instruction) { should == :abort }
218
- end
219
- context "when parameter is an answer" do
220
- let(:parameter) { abort }
221
- it { should == abort }
222
- end
223
- context "when parameter is an other object" do
224
- let(:parameter) { "string" }
225
- it "should raise an ArgumentError" do
226
- expect { subject }.to raise_error ArgumentError
227
- end
228
- end
229
- end
230
-
231
- describe "#free_indicator_of" do
232
- context ":abc" do
233
- it "should return :a if it isn't already used" do
234
- subject.send(:free_indicator_of, abort, :used_indicators => [:b]).should == :a
235
- end
236
-
237
- it "should return :a if :used_indicators is empty" do
238
- subject.send(:free_indicator_of, abort, :used_indicators => []).should == :a
239
- end
240
-
241
- it "should return :a if no :used_indicators is set" do
242
- subject.send(:free_indicator_of, abort).should == :a
243
- end
244
-
245
- it "should reuturn :ab if :a is used" do
246
- subject.send(:free_indicator_of, abort, :used_indicators => [:a]).should == :ab
247
- end
248
-
249
- it "should return :ab if :a and :A is used" do
250
- subject.send(:free_indicator_of, abort, :used_indicators => [:a, :A]).should == :ab
251
- end
252
- end
253
- end
254
- end
255
187
  end
@@ -29,7 +29,7 @@ describe Question do
29
29
  context "with answers" do
30
30
  before { subject.answers = answers_array }
31
31
  before do
32
- subject.should_receive(:gets).with("#{question_msg} [o]verwrite, [a]bort").and_return("o")
32
+ UserInput.should_receive(:get).with("#{question_msg} [o]verwrite, [a]bort").and_return("o")
33
33
  end
34
34
  its(:ask) { should == :overwrite }
35
35
  end
@@ -39,34 +39,23 @@ describe Question do
39
39
  context "with answers as array" do
40
40
  subject { Question.ask(question_msg, [:all, :nothing]) }
41
41
  before do
42
- Question.any_instance.should_receive(:gets).with("#{question_msg} [A]ll, [n]othing").and_return("A")
42
+ UserInput.should_receive(:get).with("#{question_msg} [A]ll, [n]othing").and_return("A")
43
43
  end
44
44
  it { should == :all }
45
45
  end
46
46
  context "with answers as hash" do
47
47
  subject { Question.ask(question_msg, all: true, nothing: true) }
48
48
  before do
49
- Question.any_instance.should_receive(:gets).with("#{question_msg} [A]ll, [n]othing").and_return("A")
49
+ UserInput.should_receive(:get).with("#{question_msg} [A]ll, [n]othing").and_return("A")
50
50
  end
51
51
  it { should == :all }
52
52
  end
53
53
  context "with answers as hash with false value" do
54
54
  subject { Question.ask(question_msg, all: true, not: false, nothing: true) }
55
55
  before do
56
- Question.any_instance.should_receive(:gets).with("#{question_msg} [A]ll, [n]othing").and_return("A")
56
+ UserInput.should_receive(:get).with("#{question_msg} [A]ll, [n]othing").and_return("A")
57
57
  end
58
58
  it { should == :all }
59
59
  end
60
60
  end
61
-
62
- describe "Privates:" do
63
- describe "#gets" do
64
- before do
65
- STDOUT.should_receive(:print).with(question_msg + " ")
66
- STDOUT.should_receive(:flush)
67
- STDIN.should_receive(:gets).and_return("y\n")
68
- end
69
- it { subject.send(:gets, question_msg).should == "y" }
70
- end
71
- end
72
61
  end
@@ -0,0 +1,73 @@
1
+ # encoding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe AnswersHelper do
6
+ let(:abort) { Answer.new(:abort) }
7
+
8
+ describe "#select_symbols" do
9
+ let(:hash) { {"abc" => true, abort: true, overwrite: false} }
10
+ it "should return {abort: true, overwrite: false}" do
11
+ AnswersHelper.select_symbols(hash).should == { abort: true, overwrite: false }
12
+ end
13
+ end
14
+
15
+ describe "#convert_hash_to_answers" do
16
+ let(:hash) { {abort: true, overwrite: false} }
17
+ subject do
18
+ AnswersHelper.convert_hash_to_answers(hash)
19
+ end
20
+ it "each entry should be kind of Answer" do
21
+ subject.all? { |answer| answer.kind_of? Answer }
22
+ end
23
+ its("first.instruction") { should == :abort }
24
+ its("first.true?") { should be_true }
25
+ its("last.instruction") { should == :overwrite }
26
+ its("last.true?") { should be_false }
27
+ end
28
+
29
+ describe "#convert_symbol_to_answer_or_send_through" do
30
+ subject do
31
+ AnswersHelper.convert_symbol_to_answer_or_send_through(parameter)
32
+ end
33
+ context "when parameter is a Symbol" do
34
+ let(:parameter) { :abort }
35
+ it { should be_kind_of Answer }
36
+ its(:instruction) { should == :abort }
37
+ end
38
+ context "when parameter is an answer" do
39
+ let(:parameter) { abort }
40
+ it { should == abort }
41
+ end
42
+ context "when parameter is an other object" do
43
+ let(:parameter) { "string" }
44
+ it "should raise an ArgumentError" do
45
+ expect { subject }.to raise_error ArgumentError
46
+ end
47
+ end
48
+ end
49
+
50
+ describe "#free_indicator_of" do
51
+ context ":abc" do
52
+ it "should return :a if it isn't already used" do
53
+ AnswersHelper.free_indicator_of(abort, :used_indicators => [:b]).should == :a
54
+ end
55
+
56
+ it "should return :a if :used_indicators is empty" do
57
+ AnswersHelper.free_indicator_of(abort, :used_indicators => []).should == :a
58
+ end
59
+
60
+ it "should return :a if no :used_indicators is set" do
61
+ AnswersHelper.free_indicator_of(abort).should == :a
62
+ end
63
+
64
+ it "should reuturn :ab if :a is used" do
65
+ AnswersHelper.free_indicator_of(abort, :used_indicators => [:a]).should == :ab
66
+ end
67
+
68
+ it "should return :ab if :a and :A is used" do
69
+ AnswersHelper.free_indicator_of(abort, :used_indicators => [:a, :A]).should == :ab
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe UserInput do
6
+ describe "#get" do
7
+ let(:msg) { "What?" }
8
+ before do
9
+ STDOUT.should_receive(:print).with(msg + " ")
10
+ STDOUT.should_receive(:flush)
11
+ STDIN.should_receive(:gets).and_return("y\n")
12
+ end
13
+ it { subject.send(:get, msg).should == "y" }
14
+ end
15
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: questions
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - DSIW
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-17 00:00:00.000000000 Z
11
+ date: 2013-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -127,11 +127,15 @@ files:
127
127
  - lib/questions/answers.rb
128
128
  - lib/questions/question.rb
129
129
  - lib/questions/version.rb
130
+ - lib/util/answers_helper.rb
131
+ - lib/util/user_input.rb
130
132
  - questions.gemspec
131
133
  - spec/questions/answer_spec.rb
132
134
  - spec/questions/answers_spec.rb
133
135
  - spec/questions/question_spec.rb
134
136
  - spec/spec_helper.rb
137
+ - spec/util/answers_helper_spec.rb
138
+ - spec/util/user_input_spec.rb
135
139
  homepage: https://github.com/DSIW/questions
136
140
  licenses:
137
141
  - MIT
@@ -161,4 +165,6 @@ test_files:
161
165
  - spec/questions/answers_spec.rb
162
166
  - spec/questions/question_spec.rb
163
167
  - spec/spec_helper.rb
168
+ - spec/util/answers_helper_spec.rb
169
+ - spec/util/user_input_spec.rb
164
170
  has_rdoc: