Class: Questions::SingleSelect
- Inherits:
-
Object
- Object
- Questions::SingleSelect
- Defined in:
- lib/erik-single-select.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(index, question_json) ⇒ SingleSelect
constructor
A new instance of SingleSelect.
- #mark(answer) ⇒ Object
- #name ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(index, question_json) ⇒ SingleSelect
Returns a new instance of SingleSelect.
5 6 7 8 9 10 |
# File 'lib/erik-single-select.rb', line 5 def initialize(index, question_json) @index = index @text = question_json['question'] = question_json['options'] @answer = question_json['answer'] end |
Class Method Details
.schema ⇒ Object
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 58 59 60 |
# File 'lib/erik-single-select.rb', line 32 def self.schema { "properties": { "question": { "type": "string" }, "type": { "type": "string", "pattern": "^single-select$" }, "options": { "type": "array", "items": { "type": "string" } }, "answer": { "type": "integer", } }, "required": [ "question", "type", "options", "answer" ], "additionalProperties": false } end |
Instance Method Details
#mark(answer) ⇒ Object
16 17 18 19 |
# File 'lib/erik-single-select.rb', line 16 def mark(answer) correct = answer == @answer.to_s QuestionFeedback.new(correct, correct ? nil : %(The correct answer was "#{@options[@answer]}".)) end |
#name ⇒ Object
12 13 14 |
# File 'lib/erik-single-select.rb', line 12 def name @text end |
#to_html ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/erik-single-select.rb', line 21 def to_html = .collect.with_index do |option, index| id = "q-#{@index}-#{index}" %(<div> <input id="#{id}" type="radio" name="q-#{@index}" value="#{index}" /> <label for="#{id}">#{option}</label> </div>) end .join("\n") end |