Pickaxe 0.6.3 → 0.7.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.
@@ -18,7 +18,7 @@ end
18
18
  $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
19
19
 
20
20
  module Pickaxe
21
- VERSION = "0.6.3"
21
+ VERSION = "0.7.0"
22
22
 
23
23
  class PickaxeError < StandardError; end
24
24
 
@@ -26,6 +26,10 @@ END_OF_BANNER
26
26
  Pickaxe::Main.options[:select] = Integer(v)
27
27
  end
28
28
 
29
+ opts.on("--single", "Generated test will have only 1 correct answer") do |v|
30
+ Pickaxe::Main.options[:one_choice] = true
31
+ end
32
+
29
33
  opts.on("--full-test", "Checks test after all questions are answered") do |v|
30
34
  Pickaxe::Main.options[:full_test] = true
31
35
  end
@@ -82,6 +86,12 @@ END_OF_MESSAGE
82
86
  $stderr.puts(("! --full-test disables the --repeat-incorrect option" ).color(:yellow))
83
87
  Pickaxe::Main.options[:repeat_incorrect] = false
84
88
  end
89
+
90
+ if Pickaxe::Main.options[:one_choice] and Pickaxe::Main.options[:repeat_incorrect]
91
+ $stderr.puts(("! --one-choice disables the --repeat-incorrect option" ).color(:yellow))
92
+ Pickaxe::Main.options[:repeat_incorrect] = false
93
+ end
94
+
85
95
  Pickaxe::Main.new(ARGV)
86
96
  rescue Pickaxe::PickaxeError, OptionParser::InvalidOption => e
87
97
  $stderr.puts(("! " + e.to_s).color(:red))
@@ -49,12 +49,5 @@ module Pickaxe
49
49
  BadQuestion.message(question.content.first, "has no correct answers"))
50
50
  end
51
51
  end
52
-
53
- class NotUniqueAnswerIndices < TestSyntaxError
54
- def initialize(question)
55
- super(question.content.first.index,
56
- BadQuestion.message(question.content.first, "answer indices are not unique"))
57
- end
58
- end
59
52
  end
60
53
  end
@@ -1,3 +1,5 @@
1
+ # -*- coding: UTF-8 -*-
2
+
1
3
  module Pickaxe
2
4
  class TestLine < String
3
5
  attr_accessor :index
@@ -11,7 +13,7 @@ module Pickaxe
11
13
  end
12
14
  end
13
15
 
14
- class Test
16
+ class Test
15
17
  include Pickaxe::Errors
16
18
 
17
19
  attr_reader :questions
@@ -106,6 +108,11 @@ module Pickaxe
106
108
 
107
109
  RE = /^\s*(\d+)\.?\s*(.+)$/u
108
110
 
111
+ def initialize(*args)
112
+ super(*args)
113
+ reindex_answers(self.answers)
114
+ end
115
+
109
116
  def self.parse(file, answers)
110
117
  content = []
111
118
  until answers.first.nil? or Answer::RE.match(answers.first)
@@ -128,8 +135,7 @@ module Pickaxe
128
135
 
129
136
  answers = answers.collect {|answer| Answer.parse(answer) }
130
137
  Question.new(file, m[1], content, answers).tap do |q|
131
- raise NoCorrectAnswer.new(q) if q.correct_answers.blank?
132
- raise NotUniqueAnswerIndices.new(q) unless q.answer_indices.uniq!.nil?
138
+ raise NoCorrectAnswer.new(q) if q.correct_answers.blank?
133
139
  q.content = q.content.join(" ").gsub("\\n", "\n")
134
140
  end
135
141
  end
@@ -142,17 +148,57 @@ module Pickaxe
142
148
  if @shuffled_answers.nil?
143
149
  unless Main.options[:sorted_answers]
144
150
  @shuffled_answers = self.answers.shuffle
145
- answer_indices.sort.each_with_index do |index, order|
146
- @shuffled_answers[order].index = index
147
- end
151
+ reindex_answers(@shuffled_answers)
148
152
  else
149
153
  @shuffled_answers = self.answers
150
154
  end
155
+
156
+ if Main.options[:one_choice]
157
+ # NOTE:
158
+ # This hack line removes possible answers that states that non of
159
+ # other answers are correct (in Polish), because this invalidates
160
+ # the algorithm fot generating fourth answer from given 3
161
+ #
162
+ # NOTE: Other languages will remain untouched.
163
+ #
164
+ @shuffled_answers.reject! { |a| a.content =~ /(\s+|^)żadne(\s+|$)/u }
165
+ reindex_answers(@shuffled_answers)
166
+ # END OF HACK
167
+
168
+ @shuffled_answers = generate_fourth_answer(@shuffled_answers[0...3])
169
+ end
151
170
  end
152
171
 
153
172
  @shuffled_answers
154
173
  end
155
174
 
175
+ def reindex_answers(answers)
176
+ letters = ('a'...'z').to_a
177
+ answers.each_with_index do |index, order|
178
+ answers[order].index = letters[order]
179
+ end
180
+ end
181
+
182
+ def generate_fourth_answer(answers)
183
+ correct = correct_answers(answers)
184
+ answers + case correct.length
185
+ when 0 then
186
+ [Answer.new(Answer::EMPTY, "d", true)]
187
+ when 1
188
+ left = answer_indices(answers) - correct
189
+ fourth = (0...([3, left.length].min)).collect {|i| left.combination(i).to_a.flatten }.shuffle.first
190
+ fourth = if fourth.empty?
191
+ Answer::EMPTY
192
+ else
193
+ Answer::CORRECT_ARE % fourth.map(&:upcase).join(", ")
194
+ end
195
+ [Answer.new(fourth, "d", false)]
196
+ else
197
+ answers.each {|a| a.correctness = false }
198
+ [Answer.new(Answer::CORRECT_ARE % correct.map(&:upcase).join(", "), "d", true)]
199
+ end
200
+ end
201
+
156
202
  def answered(indices)
157
203
  content = self.content.word_wrap(:indent => index.to_s.length+2)
158
204
  content + "\n\n" + self.shuffled_answers.collect do |answer|
@@ -176,12 +222,12 @@ module Pickaxe
176
222
  given.sort == correct_answers
177
223
  end
178
224
 
179
- def correct_answers
180
- answers.select(&:correctness).collect(&:index).sort
225
+ def correct_answers(a = shuffled_answers)
226
+ a.select(&:correctness).collect(&:index).sort
181
227
  end
182
228
 
183
- def answer_indices
184
- shuffled_answers.collect(&:index)
229
+ def answer_indices(a = shuffled_answers)
230
+ a.collect(&:index)
185
231
  end
186
232
 
187
233
  def check?(given)
@@ -206,6 +252,9 @@ module Pickaxe
206
252
  end
207
253
 
208
254
  class Answer < Struct.new(:content, :index, :correctness)
255
+ EMPTY = "None of answers above is correct"
256
+ CORRECT_ARE = "Correct answers: %s"
257
+
209
258
  RE = /^\s*(>+)?\s*(\?+)?\s*\(?(\w)\)\s*(.+)$/u
210
259
  LINE_RE = /^\s*\\?\s*([[:alpha:]]|\w+)/u
211
260
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Pickaxe
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 3
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 6
9
- - 3
10
- version: 0.6.3
8
+ - 7
9
+ - 0
10
+ version: 0.7.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dawid Fatyga
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-12 00:00:00 +01:00
18
+ date: 2010-12-13 00:00:00 +01:00
19
19
  default_executable: bin/pickaxe
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency