Class: WorkCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/work_calculator.rb,
lib/work_calculator/version.rb

Constant Summary collapse

VALID_START_DATE =
Date.parse('01/01/1901')
VALID_FINISH_DATE =
Date.parse('31/12/2999')
VERSION =
"1.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start, finish) ⇒ WorkCalculator

Returns a new instance of WorkCalculator.



26
27
28
29
# File 'lib/work_calculator.rb', line 26

def initialize(start, finish)
  self.start = start
  self.finish = finish
end

Instance Attribute Details

#startObject

Returns the value of attribute start.



5
6
7
# File 'lib/work_calculator.rb', line 5

def start
  @start
end

Instance Method Details

#finish=(date) ⇒ Object



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

def finish=(date)
  begin
    @finish = check_date(date)
  rescue Exception => message
    puts message
  end
end

#get_elapsed_daysNumber

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

Returns:

  • (Number)

    the resulting number of days worked



50
51
52
# File 'lib/work_calculator.rb', line 50

def get_elapsed_days
  (@finish - @start).to_i.abs - 1
end

Prints the number of full days worked in human friendly message

Returns:

  • (String)

    the resulting message



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/work_calculator.rb', line 33

def print_elapsed_days_worked
  elapsed_days = get_elapsed_days()

  case elapsed_days
  when 0
    puts "No day's worked"
  when 1
    puts "#{elapsed_days} full day worked."
  else
    puts "#{elapsed_days} full days worked."
  end
end