class Question

Attributes

answers[RW]
name[RW]
points[RW]
question_comment[RW]
question_tags[RW]
question_text[RW]
randomize[RW]
raw[RW]

Public Class Methods

from_JSON(hash_str) click to toggle source

factory method to return correct type of question

# 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
new(*args) click to toggle source
# File lib/ruql/question.rb, line 4
def initialize(*args)
  options = if args[-1].kind_of?(Hash) then args[-1] else {} end
  @answers = options[:answers] || []
  @points = [options[:points].to_i, 1].max
  @raw = options[:raw]
  @name = options[:name]
  @question_tags = []
  @question_comment = ''
end

Public Instance Methods

answer(text, opts={}) click to toggle source
# File lib/ruql/question.rb, line 22
def answer(text, opts={})
  @answers << Answer.new(text, correct=true, opts[:explanation])
end
answer_helper(obj) click to toggle source
# 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 = '') click to toggle source
# File lib/ruql/question.rb, line 39
def comment(str = '')
  @question_comment = str.to_s
end
correct_answer() click to toggle source
# File lib/ruql/question.rb, line 43
def correct_answer ;  @answers.detect(&:correct?)  ;  end
correct_answers() click to toggle source
# File lib/ruql/question.rb, line 45
def correct_answers ;  @answers.collect(&:correct?) ; end
distractor(text, opts={}) click to toggle source
# File lib/ruql/question.rb, line 26
def distractor(text, opts={})
  @answers << Answer.new(text, correct=false, opts[:explanation])
end
explanation(text) click to toggle source
# File lib/ruql/question.rb, line 18
def explanation(text)
  @answers.each { |answer| answer.explanation ||= text }
end
raw?() click to toggle source
# File lib/ruql/question.rb, line 14
def raw? ; !!@raw ; end
tags(*args) click to toggle source

these are ignored but legal for now:

# File lib/ruql/question.rb, line 31
def tags(*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) click to toggle source
# File lib/ruql/question.rb, line 16
def text(s) ; @question_text = s ; end
to_JSON() click to toggle source

creates a JSON hash of the object with its object name. we should convert this to a mixin for answer and question. aaron

# 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