Class: Test

Inherits:
Object
  • Object
show all
Defined in:
lib/jungi/classes.rb

Overview

Basic Test Class

Direct Known Subclasses

ScaleTest

Constant Summary collapse

QUESTIONS =

The test’s questions plus the type of question they are

[
  ['This is a standard question.', :yn],
  ['This is another question.', :scale]
]

Instance Method Summary collapse

Constructor Details

#initializeTest

Returns a new instance of Test.



97
98
99
# File 'lib/jungi/classes.rb', line 97

def initialize
  @answers = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Enable the use of self.Q<number> for easy implementation of tests



102
103
104
105
106
107
108
109
# File 'lib/jungi/classes.rb', line 102

def method_missing(name, *args)
  section = name[1..(name.length - 1)]
  if name[0] == 'Q' && self.out_of_index?(section.to_i - 1)
    return @answers[section.to_i - 1]
  else
    super
  end
end

Instance Method Details

#answer(*args) ⇒ Object

Alias set_answer



146
147
148
# File 'lib/jungi/classes.rb', line 146

def answer(*args)
  set_answer(*args)
end

#answer=(args) ⇒ Object

Alias set_answer



151
152
153
# File 'lib/jungi/classes.rb', line 151

def answer=(args)
  set_answer(*args)
end

#answersObject

Get a list of the answers so far



156
157
158
# File 'lib/jungi/classes.rb', line 156

def answers
  Marshal.load(Marshal.dump(@answers))
end

#done?Boolean

Alias finished?

Returns:

  • (Boolean)


170
171
172
# File 'lib/jungi/classes.rb', line 170

def done?
  self.finished?
end

#finished?Boolean

Done supplying answers to questions?

Returns:

  • (Boolean)


161
162
163
164
165
166
167
# File 'lib/jungi/classes.rb', line 161

def finished?
  if @answers.length >= self.class.const_get(:QUESTIONS).length
    return true
  else
    return false
  end
end

#get_question(index) ⇒ Object

Get question via index



124
125
126
127
# File 'lib/jungi/classes.rb', line 124

def get_question(index)
  self.out_of_index? index
  self.class.const_get(:QUESTIONS)[index].dup
end

#out_of_index?(index, truism = false) ⇒ Boolean

Check for out of index

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
# File 'lib/jungi/classes.rb', line 112

def out_of_index?(index, truism = false)
  if (index) > (self.class.const_get(:QUESTIONS).length - 1)
    if truism
      false
    else
      fail "##{index} Out of Index"
    end
  end
  true
end

#question(*args) ⇒ Object

Alias get_question



130
131
132
# File 'lib/jungi/classes.rb', line 130

def question(*args)
  get_question(*args)
end

#resultObject

Stub method for result of test



175
176
177
178
# File 'lib/jungi/classes.rb', line 175

def result
  fail 'Not ready yet!' unless self.finished?
  @answers.to_s
end

#set_answer(index, value) ⇒ Object

Set question answer to value



135
136
137
138
139
140
141
142
143
# File 'lib/jungi/classes.rb', line 135

def set_answer(index, value)
  self.out_of_index? index
  type = self.class.const_get(:QUESTIONS)[index][1]

  unless Question::Answer.follows_type?(type, value)
    fail "Type doesn't match with question type!"
  end
  @answers[index] = value
end