Class: Test
- Inherits:
-
Object
- Object
- Test
- Defined in:
- lib/jungi/classes.rb
Overview
Basic Test Class
Direct Known Subclasses
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
-
#answer(*args) ⇒ Object
Alias set_answer.
-
#answer=(args) ⇒ Object
Alias set_answer.
-
#answers ⇒ Object
Get a list of the answers so far.
-
#done? ⇒ Boolean
Alias finished?.
-
#finished? ⇒ Boolean
Done supplying answers to questions?.
-
#get_question(index) ⇒ Object
Get question via index.
-
#initialize ⇒ Test
constructor
A new instance of Test.
-
#method_missing(name, *args) ⇒ Object
Enable the use of self.Q<number> for easy implementation of tests.
-
#out_of_index?(index, truism = false) ⇒ Boolean
Check for out of index.
-
#question(*args) ⇒ Object
Alias get_question.
-
#result ⇒ Object
Stub method for result of test.
-
#set_answer(index, value) ⇒ Object
Set question answer to value.
Constructor Details
#initialize ⇒ Test
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 |
#answers ⇒ Object
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?
170 171 172 |
# File 'lib/jungi/classes.rb', line 170 def done? self.finished? end |
#finished? ⇒ Boolean
Done supplying answers to questions?
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
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 |
#result ⇒ Object
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 |