Class: SalesforceCertificationCalculator::Exam

Inherits:
Object
  • Object
show all
Defined in:
lib/salesforce_certification_calculator/exam.rb

Constant Summary collapse

SFC =
SalesforceCertificationCalculator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExam

Creates Exam object

Examples:

Create Exam Object

>> my_exam = Exam.new


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

#fileString

Returns path to file containing exam data.

Returns:

  • (String)

    path to file containing exam data



15
# File 'lib/salesforce_certification_calculator/exam.rb', line 15

attr_accessor :title, :file

#sectionsArray (readonly)

Returns collection of Section objects for all sections of the exam.

Returns:

  • (Array)

    collection of Section objects for all sections of the exam



15
# File 'lib/salesforce_certification_calculator/exam.rb', line 15

attr_accessor :title, :file

#titleString

Returns name of the exam.

Returns:

  • (String)

    name of the exam



15
16
17
# File 'lib/salesforce_certification_calculator/exam.rb', line 15

def title
  @title
end

#totalNumber (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.

Returns:

  • (Number)

    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

Examples:

Add New Section

>> my_exam = Exam.new
>> my_exam.add_section('Database', 24)

Parameters:

  • name (String)

    title of the section

  • weight (Number)

    percentage weight of the section, expressed as an integer

  • score (Number) (defaults to: 0)

    individual’s percentage score on that section of the exam, expressed as an integer; uses 0 as default value



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_totalObject

Determines cumulative score for the exam based on its sections’ weights and scores

Examples:

Calculate Total

>> my_exam = Exam.new
>> my_exam.calculate_total


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