/
pto
executable file
·77 lines (66 loc) · 1.73 KB
/
pto
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
76
77
#!/usr/bin/env ruby
require 'optparse'
require 'date'
def convert(str)
case str
when /(.*)\/(.*)/
return Float($1) / Float($2)
when /(.*)\.(.*)/
return Float(str)
else
return Integer(str)
end
end
def days_until_cap(accrual:, cap:, rate:)
days = 0
return days if accrual >= cap
daily_rate = rate / 365.0
while accrual < cap
accrual += daily_rate
days += 1
end
days
end
def main(options = {})
accrual = convert(options[:accrual])
cap = convert(options[:cap])
rate = convert(options[:rate])
days = days_until_cap(accrual: accrual, cap: cap, rate: rate)
if days == 0
puts "Already over cap :-( Please take some PTO ASAP!"
else
cap_date = Date.today + days
puts "Cap is hit in #{days} days - on #{cap_date}. Please take some PTO before then :-)"
end
return true
end
options = {}
optparse = OptionParser.new do |opts|
opts.on("-aACCRUAL", "--accrual=ACCRUAL", "Set current accrual in number of days. Defaults to zero.") do |accrual|
options[:accrual] = accrual
end
opts.on("-cCAP", "--cap=CAP", "Set cap in number of days. Required argument.") do |cap|
options[:cap] = cap
end
opts.on("-rRATE", "--rate=RATE", "Set accrual rate in number of days per year. Required argument.") do |rate|
options[:rate] = rate
end
opts.on("-h", "--help", "Prints this help") do
puts opts
exit
end
end
begin
optparse.parse!
mandatory = [:cap, :rate]
missing = mandatory.select{ |param| options[param].nil? }
unless missing.empty?
raise OptionParser::MissingArgument.new(missing.join(', '))
end
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
puts $!.to_s
puts optparse
exit
end
options[:accrual] = 0.0 if options[:accrual].nil?
main(options)