Class: Fet::Score
- Inherits:
-
Object
- Object
- Fet::Score
- Defined in:
- lib/fet/score.rb
Overview
Holds the correct/incorrect answers to questions
Class Method Summary collapse
Instance Method Summary collapse
- #answer_correctly(*degree_indices) ⇒ Object
- #answer_incorrectly(*degree_indices) ⇒ Object
- #answered_correctly(degree_index = nil) ⇒ Object
- #answered_incorrectly(degree_index = nil) ⇒ Object
- #as_json(_options = {}) ⇒ Object
-
#initialize(score: nil) ⇒ Score
constructor
A new instance of Score.
- #merge(other) ⇒ Object
- #percentages ⇒ Object
- #questions_asked(degree_index = nil) ⇒ Object
- #to_json(*options) ⇒ Object
- #total_percentage ⇒ Object
Constructor Details
#initialize(score: nil) ⇒ Score
Returns a new instance of Score.
8 9 10 11 12 13 14 |
# File 'lib/fet/score.rb', line 8 def initialize(score: nil) score_hash = score || initialize_score score_hash = score_hash.map do |k, v| [k.to_i, v.transform_keys(&:to_sym)] end.to_h self.score = score_hash end |
Class Method Details
Instance Method Details
#answer_correctly(*degree_indices) ⇒ Object
20 21 22 |
# File 'lib/fet/score.rb', line 20 def answer_correctly(*degree_indices) degree_indices.each { |degree_index| score[degree_index][:correct] += 1 } end |
#answer_incorrectly(*degree_indices) ⇒ Object
24 25 26 |
# File 'lib/fet/score.rb', line 24 def answer_incorrectly(*degree_indices) degree_indices.each { |degree_index| score[degree_index][:incorrect] += 1 } end |
#answered_correctly(degree_index = nil) ⇒ Object
28 29 30 31 32 |
# File 'lib/fet/score.rb', line 28 def answered_correctly(degree_index = nil) return score.reduce(0) do |result, (score_degree_index, score_hash)| degree_index.nil? || score_degree_index == degree_index ? result + score_hash[:correct] : result end end |
#answered_incorrectly(degree_index = nil) ⇒ Object
34 35 36 37 38 |
# File 'lib/fet/score.rb', line 34 def answered_incorrectly(degree_index = nil) return score.reduce(0) do |result, (score_degree_index, score_hash)| degree_index.nil? || score_degree_index == degree_index ? result + score_hash[:incorrect] : result end end |
#as_json(_options = {}) ⇒ Object
44 45 46 |
# File 'lib/fet/score.rb', line 44 def as_json( = {}) score end |
#merge(other) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/fet/score.rb', line 52 def merge(other) score.each do |k, v| v[:correct] += other.answered_correctly(k) v[:incorrect] += other.answered_incorrectly(k) end end |
#percentages ⇒ Object
59 60 61 62 63 |
# File 'lib/fet/score.rb', line 59 def percentages score.map do |k, _| next([k, percentage(answered_correctly(k), questions_asked(k)).to_i]) end.to_h end |
#questions_asked(degree_index = nil) ⇒ Object
40 41 42 |
# File 'lib/fet/score.rb', line 40 def questions_asked(degree_index = nil) return answered_correctly(degree_index) + answered_incorrectly(degree_index) end |
#to_json(*options) ⇒ Object
48 49 50 |
# File 'lib/fet/score.rb', line 48 def to_json(*) as_json(*).to_json(*) end |
#total_percentage ⇒ Object
65 66 67 |
# File 'lib/fet/score.rb', line 65 def total_percentage return percentage(answered_correctly, questions_asked).to_i end |