-
Notifications
You must be signed in to change notification settings - Fork 0
/
due_date.rb
75 lines (56 loc) · 1.29 KB
/
due_date.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/env ruby
# encoding: utf-8
require 'core_ext/time'
class DueDate
END_OF_SHIFT = 17
WORKDAY_LENGTH = 8
WORKWEEK_LENGTH = 5
attr_reader :time
def initialize(submit_date)
@time = submit_date
end
def add_time!(turnaround_time)
hours = turnaround_time.split(':')[0].to_i
mins = turnaround_time.split(':')[1].to_i
add_minutes(mins)
add_hours(hours)
self
end
private
def add_minutes(work_min)
@time += work_min.minute
check_overflow!
end
def add_hours(work_hour)
if work_hour > WORKDAY_LENGTH
days = work_hour / WORKDAY_LENGTH
add_days(days)
work_hour -= days * WORKDAY_LENGTH
end
@time += work_hour.hour
check_overflow!
end
def add_days(work_day)
if work_day >= WORKWEEK_LENGTH
week = work_day / WORKWEEK_LENGTH
add_weeks(week)
work_day -= week * WORKWEEK_LENGTH
end
@time += work_day.day
end
def add_weeks(work_week)
@time += work_week.week
end
def overflow_a_day
HOURS_IN_DAY = 24
@time += (HOURS_IN_DAY - WORKDAY_LENGTH).hour
end
def overflow_a_weekend
DAYS_IN_WEEK = 7
@time += (DAYS_IN_WEEK - WORKWEEK_LENGTH).day
end
def check_overflow!
overflow_a_day if @time.hour >= END_OF_SHIFT
overflow_a_weekend if @time.saturday?
end
end