Class: Pickaxe::Question
- Inherits:
-
Struct
- Object
- Struct
- Pickaxe::Question
- Includes:
- Errors
- Defined in:
- lib/pickaxe/test.rb
Constant Summary collapse
- RE =
/^\s*(\d+)\.?\s*(.+)$/u
Instance Attribute Summary collapse
-
#answers ⇒ Object
Returns the value of attribute answers.
-
#content ⇒ Object
Returns the value of attribute content.
-
#file ⇒ Object
Returns the value of attribute file.
-
#index ⇒ Object
Returns the value of attribute index.
Class Method Summary collapse
Instance Method Summary collapse
- #answer_indices(a = shuffled_answers) ⇒ Object
- #answered(indices) ⇒ Object
- #check?(given) ⇒ Boolean
- #correct?(given) ⇒ Boolean
- #correct_answers(a = shuffled_answers) ⇒ Object
- #generate_fourth_answer(answers) ⇒ Object
-
#initialize(*args) ⇒ Question
constructor
A new instance of Question.
- #reindex_answers(answers) ⇒ Object
- #reset! ⇒ Object
- #shuffled_answers ⇒ Object
Constructor Details
#initialize(*args) ⇒ Question
Returns a new instance of Question.
111 112 113 114 |
# File 'lib/pickaxe/test.rb', line 111 def initialize(*args) super(*args) reindex_answers(self.answers) end |
Instance Attribute Details
#answers ⇒ Object
Returns the value of attribute answers
106 107 108 |
# File 'lib/pickaxe/test.rb', line 106 def answers @answers end |
#content ⇒ Object
Returns the value of attribute content
106 107 108 |
# File 'lib/pickaxe/test.rb', line 106 def content @content end |
#file ⇒ Object
Returns the value of attribute file
106 107 108 |
# File 'lib/pickaxe/test.rb', line 106 def file @file end |
#index ⇒ Object
Returns the value of attribute index
106 107 108 |
# File 'lib/pickaxe/test.rb', line 106 def index @index end |
Class Method Details
.parse(file, answers) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/pickaxe/test.rb', line 116 def self.parse(file, answers) content = [] until answers.first.nil? or Answer::RE.match(answers.first) content << answers.shift end raise MissingContent.new(answers.first.index) if content.blank? raise BadQuestion.new(content.first) unless m = RE.match(content.first) raise MissingAnswers.new(content.first) if answers.blank? answers = answers.inject([]) do |joined, line| if Answer::RE.match(line) joined << [line] else raise BadAnswer.new(line) unless Answer::LINE_RE.match(line) joined.last << line end joined end answers = answers.collect {|answer| Answer.parse(answer) } Question.new(file, m[1], content, answers).tap do |q| raise NoCorrectAnswer.new(q) if q.correct_answers.blank? q.content = q.content.join(" ").gsub("\\n", "\n") end end |
Instance Method Details
#answer_indices(a = shuffled_answers) ⇒ Object
233 234 235 |
# File 'lib/pickaxe/test.rb', line 233 def answer_indices(a = shuffled_answers) a.collect(&:index) end |
#answered(indices) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/pickaxe/test.rb', line 206 def answered(indices) content = self.content.word_wrap(:indent => index.to_s.length+2) content + "\n\n" + self.shuffled_answers.collect do |answer| selected = indices.include?(answer.index) line = (selected ? ">> " : " ") + answer.to_s if(Main.[:force_show_answers] or (not indices.blank? and not Main.[:full_test])) then if selected and answer.correctness line.color(:green) elsif not selected and answer.correctness line.color(:yellow) elsif selected and not answer.correctness line.color(:red) end end || line end.join("\n") + "\n\n" end |
#check?(given) ⇒ Boolean
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/pickaxe/test.rb', line 237 def check?(given) if correct?(given) "Correct!".color(:green) else missed = (correct_answers - given) missed = unless missed.empty? "Missed: #{missed.join}".color(:yellow) else "" end incorrect = (given - correct_answers) incorrect = unless incorrect.empty? "Wrong: #{incorrect.join}".color(:red) else "" end "Incorrect, should be:".color(:red) + " #{correct_answers.join.color(:green)}! #{[missed, incorrect].join(" ")}" end end |
#correct?(given) ⇒ Boolean
225 226 227 |
# File 'lib/pickaxe/test.rb', line 225 def correct?(given) given.sort == correct_answers end |
#correct_answers(a = shuffled_answers) ⇒ Object
229 230 231 |
# File 'lib/pickaxe/test.rb', line 229 def correct_answers(a = shuffled_answers) a.select(&:correctness).collect(&:index).sort end |
#generate_fourth_answer(answers) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/pickaxe/test.rb', line 182 def generate_fourth_answer(answers) correct = correct_answers(answers) answers << case correct.length when 0 then Answer.new(Answer::EMPTY, "d", true) when 1 indices = answer_indices(answers) fourth = [[]] fourth.push(*indices.combination(2).to_a) fourth.push(*indices.combination(3).to_a) fourth = fourth.shuffle.first fourth = if fourth.empty? Answer::EMPTY else Answer::CORRECT_ARE % fourth.map(&:upcase).join(", ") end Answer.new(fourth, "d", false) else answers.each {|a| a.correctness = false } Answer.new(Answer::CORRECT_ARE % correct.map(&:upcase).join(", "), "d", true) end end |
#reindex_answers(answers) ⇒ Object
175 176 177 178 179 180 |
# File 'lib/pickaxe/test.rb', line 175 def reindex_answers(answers) letters = ('a'...'z').to_a answers.each_with_index do |index, order| answers[order].index = letters[order] end end |
#reset! ⇒ Object
143 144 145 |
# File 'lib/pickaxe/test.rb', line 143 def reset! @shuffled_answers = nil end |
#shuffled_answers ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/pickaxe/test.rb', line 147 def shuffled_answers if @shuffled_answers.nil? unless Main.[:sorted_answers] @shuffled_answers = self.answers.shuffle reindex_answers(@shuffled_answers) else @shuffled_answers = self.answers end if Main.[:one_choice] # NOTE: # This hack line removes possible answers that states that non of # other answers are correct (in Polish), because this invalidates # the algorithm fot generating fourth answer from given 3 # # NOTE: Other languages will remain untouched. # @shuffled_answers.reject! { |a| a.content =~ /(\s+|^)(ż|Ż)ad(na|ne|en)(\s+|$)/ui } reindex_answers(@shuffled_answers) # END OF HACK @shuffled_answers = generate_fourth_answer(@shuffled_answers[0...3]) end end @shuffled_answers end |