Class: Word2Quiz::Question
- Inherits:
-
Object
- Object
- Word2Quiz::Question
- Defined in:
- lib/word_2_quiz/question.rb
Overview
Question contains all data for a single question. A question has text, and answers.
Constant Summary collapse
- POINTS_POSSIBLE =
5
Instance Attribute Summary collapse
-
#answers ⇒ Object
Returns the value of attribute answers.
-
#text ⇒ Object
Returns the value of attribute text.
Class Method Summary collapse
-
.from_paragraphs(paragraphs, solution) ⇒ Object
Creates a question from an array of strings paragraphs: an array of nokogiri nodes containing each line for the question text and answers solution: a string containing which answer(a,b,c,d) is correct.
Instance Method Summary collapse
-
#initialize(text = "", answers = []) ⇒ Question
constructor
A new instance of Question.
- #to_canvas ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(text = "", answers = []) ⇒ Question
Returns a new instance of Question.
14 15 16 17 |
# File 'lib/word_2_quiz/question.rb', line 14 def initialize(text = "", answers = []) @answers = answers @text = text end |
Instance Attribute Details
#answers ⇒ Object
Returns the value of attribute answers.
10 11 12 |
# File 'lib/word_2_quiz/question.rb', line 10 def answers @answers end |
#text ⇒ Object
Returns the value of attribute text.
10 11 12 |
# File 'lib/word_2_quiz/question.rb', line 10 def text @text end |
Class Method Details
.from_paragraphs(paragraphs, solution) ⇒ Object
Creates a question from an array of strings paragraphs: an array of nokogiri nodes containing each line for the question text and answers solution: a string containing which answer(a,b,c,d) is correct
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/word_2_quiz/question.rb', line 25 def self.from_paragraphs(paragraphs, solution) if solution.nil? || solution.empty? raise InvalidAnswerKey.new( "Question #{Helpers.get_question_number(paragraphs)} does not have" + " an answer.", ) end answer_start_indexes = paragraphs.each_index.select do |i| # an answer starts with a letter then a dot paragraphs[i].text.match(/^[a-z]\./) end all_answer_paragraphs = Helpers.map_to_boundaries( indexes: answer_start_indexes, paragraphs: paragraphs, ) question_paragraphs = paragraphs.take(answer_start_indexes.first) question_paragraphs = Helpers.strip_blanks(question_paragraphs) question_text = question_paragraphs.map do |paragraph| paragraph.to_html.sub(/(>)\d+\.\s?/, '\1') # Remove question number. end.join("\n") answers = [] all_answer_paragraphs.each do |answer_paragraphs| answer = Answer.from_paragraphs(answer_paragraphs, solution) answers.push(answer) end Question.new(question_text, answers) end |
Instance Method Details
#to_canvas ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/word_2_quiz/question.rb', line 66 def to_canvas { question_type: "multiple_choice_question", question_text: @text, points_possible: POINTS_POSSIBLE, answers: @answers.map(&:to_canvas), } end |
#to_h ⇒ Object
59 60 61 62 63 64 |
# File 'lib/word_2_quiz/question.rb', line 59 def to_h { text: @text, answers: @answers.map(&:to_h), } end |