Class: Question
- Inherits:
-
Object
- Object
- Question
- Defined in:
- lib/ruql/question.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#answers ⇒ Object
Returns the value of attribute answers.
-
#name ⇒ Object
Returns the value of attribute name.
-
#points ⇒ Object
Returns the value of attribute points.
-
#question_comment ⇒ Object
Returns the value of attribute question_comment.
-
#question_tags ⇒ Object
Returns the value of attribute question_tags.
-
#question_text ⇒ Object
Returns the value of attribute question_text.
-
#randomize ⇒ Object
Returns the value of attribute randomize.
-
#raw ⇒ Object
Returns the value of attribute raw.
Class Method Summary collapse
-
.from_JSON(hash_str) ⇒ Object
factory method to return correct type of question.
Instance Method Summary collapse
- #answer(text, opts = {}) ⇒ Object
- #answer_helper(obj) ⇒ Object
- #comment(str = '') ⇒ Object
- #correct_answer ⇒ Object
- #correct_answers ⇒ Object
- #distractor(text, opts = {}) ⇒ Object
- #explanation(text) ⇒ Object
-
#initialize(*args) ⇒ Question
constructor
A new instance of Question.
- #raw? ⇒ Boolean
-
#tags(*args) ⇒ Object
these are ignored but legal for now:.
- #text(s) ⇒ Object
-
#to_JSON ⇒ Object
creates a JSON hash of the object with its object name.
Constructor Details
#initialize(*args) ⇒ Question
Returns a new instance of Question.
4 5 6 7 8 9 10 11 12 |
# File 'lib/ruql/question.rb', line 4 def initialize(*args) = if args[-1].kind_of?(Hash) then args[-1] else {} end @answers = [:answers] || [] @points = [[:points].to_i, 1].max @raw = [:raw] @name = [:name] @question_tags = [] @question_comment = '' end |
Instance Attribute Details
#answers ⇒ Object
Returns the value of attribute answers.
2 3 4 |
# File 'lib/ruql/question.rb', line 2 def answers @answers end |
#name ⇒ Object
Returns the value of attribute name.
2 3 4 |
# File 'lib/ruql/question.rb', line 2 def name @name end |
#points ⇒ Object
Returns the value of attribute points.
2 3 4 |
# File 'lib/ruql/question.rb', line 2 def points @points end |
#question_comment ⇒ Object
Returns the value of attribute question_comment.
2 3 4 |
# File 'lib/ruql/question.rb', line 2 def question_comment @question_comment end |
#question_tags ⇒ Object
Returns the value of attribute question_tags.
2 3 4 |
# File 'lib/ruql/question.rb', line 2 def @question_tags end |
#question_text ⇒ Object
Returns the value of attribute question_text.
2 3 4 |
# File 'lib/ruql/question.rb', line 2 def question_text @question_text end |
#randomize ⇒ Object
Returns the value of attribute randomize.
2 3 4 |
# File 'lib/ruql/question.rb', line 2 def randomize @randomize end |
#raw ⇒ Object
Returns the value of attribute raw.
2 3 4 |
# File 'lib/ruql/question.rb', line 2 def raw @raw end |
Class Method Details
.from_JSON(hash_str) ⇒ Object
factory method to return correct type of question
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ruql/question.rb', line 62 def self.from_JSON(hash_str) hash = JSON.parse(hash_str) #create the appropriate class of the object from the hash's class name question = Object.const_get(hash.fetch('question_type')).new() hash.reject{|key| key == 'answers' or key == 'question_type'}.each do |key, value| question.send((key + '=').to_sym, value) end question.answers = hash['answers'].map{|answer_hash| Answer.from_JSON(answer_hash)} question end |
Instance Method Details
#answer(text, opts = {}) ⇒ Object
22 23 24 |
# File 'lib/ruql/question.rb', line 22 def answer(text, opts={}) @answers << Answer.new(text, correct=true, opts[:explanation]) end |
#answer_helper(obj) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/ruql/question.rb', line 47 def answer_helper(obj) if obj.is_a? Array and obj.size and obj[0].is_a? Answer return obj.map {|answer| answer.to_JSON} end obj end |
#comment(str = '') ⇒ Object
39 40 41 |
# File 'lib/ruql/question.rb', line 39 def comment(str = '') @question_comment = str.to_s end |
#correct_answer ⇒ Object
43 |
# File 'lib/ruql/question.rb', line 43 def correct_answer ; @answers.detect(&:correct?) ; end |
#correct_answers ⇒ Object
45 |
# File 'lib/ruql/question.rb', line 45 def correct_answers ; @answers.collect(&:correct?) ; end |
#distractor(text, opts = {}) ⇒ Object
26 27 28 |
# File 'lib/ruql/question.rb', line 26 def distractor(text, opts={}) @answers << Answer.new(text, correct=false, opts[:explanation]) end |
#explanation(text) ⇒ Object
18 19 20 |
# File 'lib/ruql/question.rb', line 18 def explanation(text) @answers.each { |answer| answer.explanation ||= text } end |
#raw? ⇒ Boolean
14 |
# File 'lib/ruql/question.rb', line 14 def raw? ; !!@raw ; end |
#tags(*args) ⇒ Object
these are ignored but legal for now:
31 32 33 34 35 36 37 |
# File 'lib/ruql/question.rb', line 31 def (*args) # string or array of strings if args.length > 1 @question_tags += args.map(&:to_s) else @question_tags << args.first.to_s end end |
#text(s) ⇒ Object
16 |
# File 'lib/ruql/question.rb', line 16 def text(s) ; @question_text = s ; end |
#to_JSON ⇒ Object
creates a JSON hash of the object with its object name. we should convert this to a mixin for answer and question. aaron
55 56 57 58 59 |
# File 'lib/ruql/question.rb', line 55 def to_JSON h = Hash[instance_variables.collect { |var| [var.to_s.delete('@'), answer_helper(instance_variable_get(var))] }] h['question_type'] = self.class.to_s return h end |