class WorkCalculator

Constants

VALID_FINISH_DATE
VALID_START_DATE
VERSION

Attributes

start[RW]

Public Class Methods

new(start, finish) click to toggle source
# File lib/work_calculator.rb, line 26
def initialize(start, finish)
  self.start = start
  self.finish = finish
end

Public Instance Methods

finish=(date) click to toggle source
# File lib/work_calculator.rb, line 18
def finish=(date)
  begin
    @finish = check_date(date)
  rescue Exception => message
    puts message
  end
end
get_elapsed_days() click to toggle source

Calculates the total number of worked days by subtracting the end date to the start date Subtracting 1 by total days in required because the last day work is not considered a full days work It is possible the start date is before end date, thus we convert any negative days work to positive @return [Number] the resulting number of days worked

# File lib/work_calculator.rb, line 50
def get_elapsed_days
  (@finish - @start).to_i.abs - 1
end
print_elapsed_days_worked() click to toggle source

Prints the number of full days worked in human friendly message @return [String] the resulting message

start=(date) click to toggle source
# File lib/work_calculator.rb, line 10
def start=(date)
  begin
    @start = check_date(date)
  rescue Exception => message
    puts message
  end
end

Private Instance Methods

check_date(date) click to toggle source

Validates start/finish date @return [Date] the resulting date

# File lib/work_calculator.rb, line 58
def check_date(date)
  date = Date.parse(date)
  if date == nil
    raise "Date calculator needs a #{date.key} date"
  elsif !is_within_valid_dates(date)
    raise "#{date.key} date is not valid, check the date is between #{VALID_START_DATE} to #{VALID_FINISH_DATE}"
  else
    date
  end
end
is_within_valid_dates(date) click to toggle source

Checks if the date is not before the valid start date and end start date threshold @param name [Date] the date to validate against @return [Bool] the resulting validation

# File lib/work_calculator.rb, line 72
def is_within_valid_dates date
  date.between?(VALID_START_DATE, VALID_FINISH_DATE)
end