Class: SalesforceCertificationCalculator::Exam
- Inherits:
-
Object
- Object
- SalesforceCertificationCalculator::Exam
- Defined in:
- lib/salesforce_certification_calculator/exam.rb
Constant Summary collapse
Instance Attribute Summary collapse
-
#file ⇒ String
Path to file containing exam data.
-
#sections ⇒ Array
readonly
Collection of Section objects for all sections of the exam.
-
#title ⇒ String
Name of the exam.
-
#total ⇒ Number
readonly
Cumulative score for the entire exam; if sections’ weights do not sum to 100, it will return an error message as a string.
Instance Method Summary collapse
-
#add_section(name, weight, score = 0) ⇒ Object
Adds a new Section object to the exam’s array of sections.
-
#calculate_total ⇒ Object
Determines cumulative score for the exam based on its sections’ weights and scores.
-
#initialize ⇒ Exam
constructor
Creates Exam object.
Constructor Details
#initialize ⇒ Exam
Creates Exam object
22 23 24 25 26 27 |
# File 'lib/salesforce_certification_calculator/exam.rb', line 22 def initialize @title = "" @file = "" @sections = [] @total = 0 end |
Instance Attribute Details
#file ⇒ String
Returns path to file containing exam data.
15 |
# File 'lib/salesforce_certification_calculator/exam.rb', line 15 attr_accessor :title, :file |
#sections ⇒ Array (readonly)
Returns collection of Section objects for all sections of the exam.
15 |
# File 'lib/salesforce_certification_calculator/exam.rb', line 15 attr_accessor :title, :file |
#title ⇒ String
Returns name of the exam.
15 16 17 |
# File 'lib/salesforce_certification_calculator/exam.rb', line 15 def title @title end |
#total ⇒ Number (readonly)
Returns cumulative score for the entire exam; if sections’ weights do not sum to 100, it will return an error message as a string.
15 |
# File 'lib/salesforce_certification_calculator/exam.rb', line 15 attr_accessor :title, :file |
Instance Method Details
#add_section(name, weight, score = 0) ⇒ Object
Adds a new Section object to the exam’s array of sections
38 39 40 41 |
# File 'lib/salesforce_certification_calculator/exam.rb', line 38 def add_section(name, weight, score = 0) section = SFC::Section.new(name, weight, score) @sections.push(section) end |
#calculate_total ⇒ Object
Determines cumulative score for the exam based on its sections’ weights and scores
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/salesforce_certification_calculator/exam.rb', line 48 def calculate_total summed_weights = 0 # Sum weighted scores of all sections @sections.each do |section| summed_weights += section.weight @total += section.weight * section.score / 100.0 end # Sets total to error message when sum of sections' weights is not 100; otherwise, rounds answer if summed_weights != 100 @total = "CANNOT CALCULATE" else @total = @total.round(2) end end |