Class: Formulas::Salary

Inherits:
Object
  • Object
show all
Includes:
FrequencyConversions
Defined in:
lib/formulas/salary.rb

Overview

Calculate basic salary in frequency salary = Formulas::Salary.new(pay: 52_000, Salary::ANNUAL)

salary.pay(:monthly)

Provide a way to calculate salary in different frequencies It provide summaries of salary breakdown after deductions such as withholding tax, super, health care and methods to get the take home pay

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FrequencyConversions

#convert_annually_to_annually, #convert_annually_to_fortnightly, #convert_annually_to_monthly, #convert_annually_to_weekly, #convert_fortnightly_to_annually, #convert_fortnightly_to_monthly, #convert_fortnightly_to_weekly, #convert_monthly_to_annually, #convert_monthly_to_fortnightly, #convert_monthly_to_weekly, #convert_weekly_to_annually, #convert_weekly_to_fortnightly, #convert_weekly_to_monthly

Constructor Details

#initialize(pay:, frequency:) ⇒ Salary

Returns a new instance of Salary.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
# File 'lib/formulas/salary.rb', line 18

def initialize(pay:, frequency:)
  @pay = pay
  @frequency = frequency.to_sym

  invalid_frequency unless FREQUENCIES.include?(@frequency)
  raise ArgumentError, 'Gross pay must be numeric' unless Numeric === pay
end

Instance Attribute Details

#frequencyObject (readonly)

Returns the value of attribute frequency.



16
17
18
# File 'lib/formulas/salary.rb', line 16

def frequency
  @frequency
end

Instance Method Details

#pay(request_frequency: @frequency) ⇒ Object



26
27
28
29
30
31
# File 'lib/formulas/salary.rb', line 26

def pay(request_frequency: @frequency)
  invalid_frequency unless FREQUENCIES.include?(frequency)
  return @pay if @frequency == request_frequency

  convert_pay_to(request_frequency).round(2)
end