forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretirement.rb
101 lines (84 loc) · 3.02 KB
/
retirement.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
class Service
class DialogProperties
class Retirement
RETIREMENT_WARN_FIELD_NAMES = %w(warn_on warn_in_days warn_in_hours warn_offset_days warn_offset_hours).freeze
def initialize(options, user)
@attributes = {}
@options = options || {}
@user = user
end
def self.parse(options, user)
new(options, user).parse
end
def parse
@attributes.tap { parse_options }
end
private
def parse_options
if @options['dialog_service_retires_on'].present?
field_name = 'dialog_service_retires_on'
self.retire_on_date = time_parse(@options[field_name])
elsif @options['dialog_service_retires_in_hours'].present?
field_name = 'dialog_service_retires_in_hours'
retires_in_duration(@options[field_name], :hours)
elsif @options['dialog_service_retires_in_days'].present?
field_name = 'dialog_service_retires_in_days'
retires_in_duration(@options[field_name], :days)
end
rescue StandardError
$log.error("Error parsing dialog retirement property [#{field_name}] with value [#{@options[field_name].inspect}]. Error: #{$!}")
end
def retires_in_duration(value, modifier)
self.retire_on_date = time_now + offset_set(value, modifier)
end
def offset_set(value, modifier)
value.to_i.send(modifier).tap do |offset|
raise "Offset cannot be a zero or negative value" if offset.zero? || offset.negative?
end
end
def retire_on_date=(value)
@attributes[:retires_on] = value
retirement_warning
end
def retirement_warning
warn_value = parse_retirement_warn
@attributes[:retirement_warn] = warn_value if warn_value
end
def parse_retirement_warn
warn_key, value = retirement_warn_properties
case warn_key
when 'warn_on'
time_parse(value)
when 'warn_in_days'
time_now + offset_set(value, :days)
when 'warn_in_hours'
time_now + offset_set(value, :hours)
when 'warn_offset_days'
@attributes[:retires_on] - offset_set(value, :days)
when 'warn_offset_hours'
@attributes[:retires_on] - offset_set(value, :hours)
end
end
def retirement_warn_properties
warn_name = RETIREMENT_WARN_FIELD_NAMES.detect do |field_name|
@options["dialog_service_retirement_#{field_name}"].present?
end
return warn_name, @options["dialog_service_retirement_#{warn_name}"] if warn_name
end
def time_parse(value)
with_user_timezone do
Time.zone.parse(value).utc.tap do |time|
raise "Retirement date cannot be set in the past" if time < time_now
end
end
end
def time_now
with_user_timezone { Time.zone.now.utc }
end
def with_user_timezone
user = @user || User.current_user
user ? user.with_my_timezone { yield } : yield
end
end
end
end