Class: Ruql::Json
- Inherits:
-
Object
- Object
- Ruql::Json
- Defined in:
- lib/ruql/json/json.rb,
lib/ruql/json/version.rb
Constant Summary collapse
- VERSION =
"1.0.0"
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Class Method Summary collapse
Instance Method Summary collapse
- #answers_to_json_array(answers) ⇒ Object
-
#initialize(quiz, options = {}) ⇒ Json
constructor
A new instance of Json.
- #render_fill_in(q) ⇒ Object
- #render_multiple_choice(question) ⇒ Object
- #render_quiz ⇒ Object
Constructor Details
#initialize(quiz, options = {}) ⇒ Json
Returns a new instance of Json.
7 8 9 10 11 |
# File 'lib/ruql/json/json.rb', line 7 def initialize(quiz,={}) @output = '' @json_array = [] @quiz = quiz end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
5 6 7 |
# File 'lib/ruql/json/json.rb', line 5 def output @output end |
Class Method Details
.allowed_options ⇒ Object
13 14 15 |
# File 'lib/ruql/json/json.rb', line 13 def self. return ['',[]] end |
Instance Method Details
#answers_to_json_array(answers) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ruql/json/json.rb', line 37 def answers_to_json_array(answers) answers_array = [] answers.each do |answer| answer_json = { "text" => answer.answer_text, "explanation" => answer.explanation, "correct" => answer.correct } answers_array.push(answer_json) end answers_array end |
#render_fill_in(q) ⇒ Object
50 51 52 |
# File 'lib/ruql/json/json.rb', line 50 def render_fill_in(q) # fill-in-the-blank questions not currently supported end |
#render_multiple_choice(question) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/ruql/json/json.rb', line 29 def render_multiple_choice(question) question_hash = { "text" => question.question_text, "answers" => answers_to_json_array(question.answers) } @json_array.push(question_hash) end |
#render_quiz ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ruql/json/json.rb', line 17 def render_quiz @quiz.questions.each do |question| case question when MultipleChoice, SelectMultiple, TrueFalse then render_multiple_choice(question) when FillIn then render_fill_in(question) # not currently supported else raise "Unknown question type: #{question}" end end @output = ::JSON.pretty_generate(@json_array) end |