Skip to content

Commit

Permalink
Convert StatutorySickPayCalculator into ActiveModel model
Browse files Browse the repository at this point in the history
This will allow us to instantiate the calculator at the beginning of the flow
and set its attributes as we go along c.f. `PartYearProfitTaxCreditsCalculator`.

This commit extracts logic from the constructor into separate methods which are
only called on demand. This should make it easier to unit test these methods,
because we'll only need to supply the attributes that are actually needed for
the method under test; previously we had to supply many/all of the attributes to
avoid an error in the constructor.
  • Loading branch information
floehopper committed Nov 12, 2015
1 parent c98c60d commit f78671a
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions lib/smart_answer/calculators/statutory_sick_pay_calculator.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
module SmartAnswer::Calculators
class StatutorySickPayCalculator
attr_reader :waiting_days, :normal_workdays, :pattern_days
include ActiveModel::Model

def initialize(prev_sick_days:, sick_start_date:, sick_end_date:, days_of_the_week_worked:)
@prev_sick_days = prev_sick_days
@waiting_days = (@prev_sick_days >= 3 ? 0 : 3 - @prev_sick_days)
@sick_start_date = sick_start_date
@sick_end_date = sick_end_date
@pattern_days = days_of_the_week_worked.length
@normal_workdays_missed = init_normal_workdays_missed(days_of_the_week_worked)
@normal_workdays = @normal_workdays_missed.length
@payable_days = init_payable_days
attr_writer :prev_sick_days, :sick_start_date, :sick_end_date, :days_of_the_week_worked

def waiting_days
@prev_sick_days >= 3 ? 0 : 3 - @prev_sick_days
end

def pattern_days
@days_of_the_week_worked.length
end

def normal_workdays_missed
@normal_workdays_missed ||= init_normal_workdays_missed(@days_of_the_week_worked)
end

def normal_workdays
normal_workdays_missed.length
end

def payable_days
@payable_days ||= init_payable_days
end

# define as static so we don't have to instantiate the calculator too early in the flow
Expand Down Expand Up @@ -98,11 +109,11 @@ def weekly_rate_on(date)
end

def max_days_that_can_be_paid
(28 * @pattern_days).round(10)
(28 * pattern_days).round(10)
end

def days_to_pay
@payable_days.length
payable_days.length
end

def sick_pay_weekly_dates
Expand All @@ -123,7 +134,7 @@ def weekly_payments
def weekly_payment(week_start_date)
pay = 0.0
((week_start_date - 6)..week_start_date).each do |date|
pay += daily_rate_from_weekly(weekly_rate_on(date), @pattern_days) if @payable_days.include?(date)
pay += daily_rate_from_weekly(weekly_rate_on(date), pattern_days) if payable_days.include?(date)
end
BigDecimal.new(pay.round(10).to_s).round(2, BigDecimal::ROUND_UP).to_f
end
Expand All @@ -146,9 +157,9 @@ def init_normal_workdays_missed(days_of_the_week_worked)

def init_payable_days
# copy not to modify the instance variable we need to keep
payable_days_temp = @normal_workdays_missed.dup
payable_days_temp = normal_workdays_missed.dup
## 1. remove up to 3 first dates from the array if there are waiting days in this period
payable_days_temp.shift(@waiting_days)
payable_days_temp.shift(waiting_days)
## 2. return only the first days_that_can_be_paid_for_this_period
payable_days_temp.shift(days_that_can_be_paid_for_this_period)
end
Expand Down

0 comments on commit f78671a

Please sign in to comment.