forked from ice-cube-ruby/ice_cube
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhour_of_day.rb
More file actions
78 lines (58 loc) · 1.75 KB
/
Copy pathhour_of_day.rb
File metadata and controls
78 lines (58 loc) · 1.75 KB
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
module IceCube
module Validations::HourOfDay
# Add hour of day validations
def hour_of_day(*hours)
hours.flatten.each do |hour|
unless hour.is_a?(Integer)
raise ArgumentError, "expecting Integer value for hour, got #{hour.inspect}"
end
verify_alignment(hour, :hour, :hour_of_day) { |error| raise error }
validations_for(:hour_of_day) << Validation.new(hour)
end
clobber_base_validations(:hour)
self
end
def realign(opening_time, start_time)
return super unless validations[:hour_of_day]
freq = base_interval_validation.interval
first_hour = Array(validations[:hour_of_day]).min_by(&:value)
time = TimeUtil::TimeWrapper.new(start_time, false)
if freq > 1
offset = first_hour.validate(opening_time, start_time)
time.add(:hour, offset - freq)
else
time.hour = first_hour.value
end
super opening_time, time.to_time
end
class Validation < Validations::FixedValue
attr_reader :hour
alias :value :hour
def initialize(hour)
@hour = hour
end
def key
:hour_of_day
end
def type
:hour
end
def dst_adjust?
true
end
def build_s(builder)
builder.piece(:hour_of_day) << StringBuilder.nice_number(hour)
end
def build_hash(builder)
builder.validations_array(:hour_of_day) << hour
end
def build_ical(builder)
builder['BYHOUR'] << hour
end
StringBuilder.register_formatter(:hour_of_day) do |segments|
str = StringBuilder.sentence(segments)
IceCube::I18n.t('ice_cube.at_hours_of_the_day', count: segments.size, segments: str)
end
end
end
end