Class: Question

Inherits:
Object
  • Object
show all
Defined in:
lib/ruql/question.rb

Direct Known Subclasses

Dropdown, FillIn, MultipleChoice, TrueFalse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Question

Returns a new instance of Question.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ruql/question.rb', line 13

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_image = options[:image]
  @question_tags = []
  @question_uid = (options.delete(:uid) || SecureRandom.uuid).to_s
  @question_comment = ''
end

Instance Attribute Details

#answersObject

Returns the value of attribute answers.



2
3
4
# File 'lib/ruql/question.rb', line 2

def answers
  @answers
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/ruql/question.rb', line 2

def name
  @name
end

#pointsObject

Returns the value of attribute points.



2
3
4
# File 'lib/ruql/question.rb', line 2

def points
  @points
end

#question_commentObject

Returns the value of attribute question_comment.



2
3
4
# File 'lib/ruql/question.rb', line 2

def question_comment
  @question_comment
end

#question_imageObject

Returns the value of attribute question_image.



2
3
4
# File 'lib/ruql/question.rb', line 2

def question_image
  @question_image
end

#question_tagsObject

Returns the value of attribute question_tags.



2
3
4
# File 'lib/ruql/question.rb', line 2

def question_tags
  @question_tags
end

#question_textObject

Returns the value of attribute question_text.



2
3
4
# File 'lib/ruql/question.rb', line 2

def question_text
  @question_text
end

#question_uidObject

Returns the value of attribute question_uid.



2
3
4
# File 'lib/ruql/question.rb', line 2

def question_uid
  @question_uid
end

#randomizeObject

Returns the value of attribute randomize.



2
3
4
# File 'lib/ruql/question.rb', line 2

def randomize
  @randomize
end

#rawObject

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



78
79
80
81
82
83
84
85
86
87
# File 'lib/ruql/question.rb', line 78

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



38
39
40
# File 'lib/ruql/question.rb', line 38

def answer(text, opts={})
  @answers << Answer.new(text, correct=true, opts[:explanation])
end

#answer_helper(obj) ⇒ Object



63
64
65
66
67
68
# File 'lib/ruql/question.rb', line 63

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



55
56
57
# File 'lib/ruql/question.rb', line 55

def comment(str = '')
  @question_comment = str.to_s
end

#correct_answerObject



59
# File 'lib/ruql/question.rb', line 59

def correct_answer ;  @answers.detect(&:correct?)  ;  end

#correct_answersObject



61
# File 'lib/ruql/question.rb', line 61

def correct_answers ;  @answers.collect(&:correct?) ; end

#distractor(text, opts = {}) ⇒ Object



42
43
44
# File 'lib/ruql/question.rb', line 42

def distractor(text, opts={})
  @answers << Answer.new(text, correct=false, opts[:explanation])
end

#explanation(text) ⇒ Object



30
31
32
# File 'lib/ruql/question.rb', line 30

def explanation(text)
  @answers.each { |answer| answer.explanation ||= text }
end

#image(url) ⇒ Object



34
35
36
# File 'lib/ruql/question.rb', line 34

def image(url)
  @question_image = url
end

#raw?Boolean

Returns:

  • (Boolean)


24
# File 'lib/ruql/question.rb', line 24

def raw? ; !!@raw ; end

#tags(*args) ⇒ Object

these are ignored but legal for now:



47
48
49
50
51
52
53
# File 'lib/ruql/question.rb', line 47

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) ⇒ Object



28
# File 'lib/ruql/question.rb', line 28

def text(s) ; @question_text = s ; end

#to_JSONObject

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



71
72
73
74
75
# File 'lib/ruql/question.rb', line 71

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

#uid(u) ⇒ Object



26
# File 'lib/ruql/question.rb', line 26

def uid(u) ; @question_uid = u ; end