Class: Barker::CandidateQuestion
- Inherits:
-
Object
- Object
- Barker::CandidateQuestion
show all
- Includes:
- Errors
- Defined in:
- lib/barker/candidate_question.rb
Constant Summary
Constants included
from Errors
Errors::AlreadyAnsweredError, Errors::AlreadyStartedError, Errors::AnswerNotFoundError, Errors::BarkerError, Errors::FileNotExistError, Errors::HasToRespondToIdError, Errors::HasToRespondToLocaleError, Errors::JokerAlreadyUsedError, Errors::NoCurrentStageError, Errors::NotStartedError, Errors::RecordNotFound, Errors::UnknownJokerError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(candidate, question, options = {}) ⇒ CandidateQuestion
Returns a new instance of CandidateQuestion.
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/barker/candidate_question.rb', line 9
def initialize(candidate, question, options = {})
@candidate = candidate
@question = question
@answers = question.answers.map { |answer| CandidateAnswer.new(candidate, answer) }
@given_answer = nil
@asked_at = nil
@answered_at = nil
@timer = options[:timer] || nil
end
|
Instance Attribute Details
#answered_at ⇒ Object
Returns the value of attribute answered_at.
7
8
9
|
# File 'lib/barker/candidate_question.rb', line 7
def answered_at
@answered_at
end
|
#answers ⇒ Object
Returns the value of attribute answers.
6
7
8
|
# File 'lib/barker/candidate_question.rb', line 6
def answers
@answers
end
|
#asked_at ⇒ Object
Returns the value of attribute asked_at.
7
8
9
|
# File 'lib/barker/candidate_question.rb', line 7
def asked_at
@asked_at
end
|
#candidate ⇒ Object
Returns the value of attribute candidate.
6
7
8
|
# File 'lib/barker/candidate_question.rb', line 6
def candidate
@candidate
end
|
#given_answer ⇒ Object
Returns the value of attribute given_answer.
6
7
8
|
# File 'lib/barker/candidate_question.rb', line 6
def given_answer
@given_answer
end
|
#question ⇒ Object
Returns the value of attribute question.
6
7
8
|
# File 'lib/barker/candidate_question.rb', line 6
def question
@question
end
|
#timer ⇒ Object
Returns the value of attribute timer.
7
8
9
|
# File 'lib/barker/candidate_question.rb', line 7
def timer
@timer
end
|
Instance Method Details
#answer(answer_id) ⇒ Object
27
28
29
30
31
32
|
# File 'lib/barker/candidate_question.rb', line 27
def answer(answer_id)
raise AlreadyAnsweredError if answered?
raise AnswerNotFoundError unless @given_answer = answers.find {|answer| answer.id == answer_id }
@answered_at = Time.now
given_answer
end
|
#answered? ⇒ Boolean
54
55
56
|
# File 'lib/barker/candidate_question.rb', line 54
def answered?
!!answered_at
end
|
#ask(options = {}) ⇒ Object
20
21
22
23
24
25
|
# File 'lib/barker/candidate_question.rb', line 20
def ask(options = {})
if !asked? || options[:reset_timer]
@asked_at = Time.now
end
self
end
|
#asked? ⇒ Boolean
50
51
52
|
# File 'lib/barker/candidate_question.rb', line 50
def asked?
!!asked_at
end
|
#candidate_id ⇒ Object
81
82
83
|
# File 'lib/barker/candidate_question.rb', line 81
def candidate_id
candidate.id
end
|
#correct? ⇒ Boolean
58
59
60
|
# File 'lib/barker/candidate_question.rb', line 58
def correct?
!!given_answer.try(:correct?)
end
|
#joker(joker) ⇒ Object
34
35
36
37
38
39
40
|
# File 'lib/barker/candidate_question.rb', line 34
def joker(joker)
raise JokerAlreadyUsedError if joker_played?(joker)
CandidateJoker.new(self, joker).tap do |candidate_joker|
candidate_joker.call
candidate.jokers << candidate_joker
end
end
|
#label ⇒ Object
85
86
87
|
# File 'lib/barker/candidate_question.rb', line 85
def label
question.label[candidate.locale] || question.label["en"]
end
|
#open? ⇒ Boolean
46
47
48
|
# File 'lib/barker/candidate_question.rb', line 46
def open?
!asked? && !answered?
end
|
#question_id ⇒ Object
Also known as:
id
76
77
78
|
# File 'lib/barker/candidate_question.rb', line 76
def question_id
question.id
end
|
#state ⇒ Object
66
67
68
69
70
71
72
73
74
|
# File 'lib/barker/candidate_question.rb', line 66
def state
if open?
:open
elsif answered?
correct?? :correct : :wrong
elsif asked?
:asked
end
end
|
#time_left ⇒ Object
93
94
95
96
|
# File 'lib/barker/candidate_question.rb', line 93
def time_left
left = timer.try(:-, delay)
(left.try(:>=, 0))? left : 0
end
|
#timeout? ⇒ Boolean
89
90
91
|
# File 'lib/barker/candidate_question.rb', line 89
def timeout?
timer.nil?? false : !(time_left.try(:>, 0))
end
|
#valid? ⇒ Boolean
42
43
44
|
# File 'lib/barker/candidate_question.rb', line 42
def valid?
question.valid? && timestamps? && asked_before_answered?
end
|
#wrong? ⇒ Boolean
62
63
64
|
# File 'lib/barker/candidate_question.rb', line 62
def wrong?
!correct?
end
|