Class: OpeningHoursConverter::OpeningHoursDate
- Inherits:
-
Object
- Object
- OpeningHoursConverter::OpeningHoursDate
show all
- Includes:
- Constants
- Defined in:
- lib/opening_hours_converter/opening_hours_date.rb
Constant Summary
Constants included
from Constants
Constants::DAYS, Constants::DAYS_MAX, Constants::IRL_DAYS, Constants::IRL_MONTHS, Constants::MINUTES_MAX, Constants::MONTH_END_DAY, Constants::OSM_DAYS, Constants::OSM_MONTHS, Constants::PH_WEEKDAY, Constants::YEAR_DAYS_MAX
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(wide, wide_type, weekdays) ⇒ OpeningHoursDate
Returns a new instance of OpeningHoursDate.
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 9
def initialize(wide, wide_type, weekdays)
if wide.nil? || wide_type.nil? || weekdays.nil?
raise ArgumentError
end
@wide = wide
@wide_type = wide_type
@weekdays = weekdays.sort
@weekdays_over = []
end
|
Instance Attribute Details
#weekdays ⇒ Object
Returns the value of attribute weekdays.
6
7
8
|
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 6
def weekdays
@weekdays
end
|
#weekdays_over ⇒ Object
Returns the value of attribute weekdays_over.
6
7
8
|
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 6
def weekdays_over
@weekdays_over
end
|
#wide ⇒ Object
Returns the value of attribute wide.
7
8
9
|
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 7
def wide
@wide
end
|
#wide_type ⇒ Object
Returns the value of attribute wide_type.
7
8
9
|
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 7
def wide_type
@wide_type
end
|
Instance Method Details
#add_overwritten_weekday(weekday) ⇒ Object
117
118
119
120
121
122
|
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 117
def add_overwritten_weekday(weekday)
unless @weekdays_over.include?(weekday) && @weekdays_over.include?(weekday)
@weekdays_over << weekday
@weekdays_over.sort!
end
end
|
#add_ph_weekday ⇒ Object
113
114
115
|
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 113
def add_ph_weekday
add_weekday(-2)
end
|
#add_weekday(weekday) ⇒ Object
106
107
108
109
110
111
|
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 106
def add_weekday(weekday)
if !@weekdays.include?(weekday) && !@weekdays_over.include?(weekday)
@weekdays << weekday
@weekdays.sort!
end
end
|
#equals(o) ⇒ Object
132
133
134
|
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 132
def equals(o)
o.instance_of?(OpeningHoursConverter::OpeningHoursDate) && @wide_type == o.wide_type && @wide == o.wide && o.same_weekdays?(@weekdays)
end
|
#get_weekdays ⇒ Object
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
102
103
104
|
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 20
def get_weekdays
result = ""
wd = @weekdays.concat(@weekdays_over).sort.uniq
if wd.length > 0 && wd[0] == -2
result = "PH"
wd.shift
end
if wd.length > 0 && wd.include?(6) && wd.include?(0) && (wd.include?(5) || wd.include?(1))
start_we = 6
i = wd.length - 2
stop_looking = false
while !stop_looking && i >= 0
if wd[i] == wd[i+1] - 1
start_we = wd[i]
i -= 1
else
stop_looking = true
end
end
i = 1
stop_looking = false
end_we = 0
while !stop_looking && i < wd.length
if wd[i-1] == wd[i] - 1
end_we = wd[i]
i += 1
else
stop_looking = true
end
end
length = 7 - start_we + end_we + 1
if length >= 3 && start_we > end_we
if result.length > 0
result += ","
end
result += "#{OSM_DAYS[start_we]}-#{OSM_DAYS[end_we]}"
j=0
while j < wd.length
if wd[j] <= end_we || wd[j] >= start_we
wd.slice!(j, 1)
else
j+=1
end
end
end
end
if wd.length > 1 || (wd.length == 1 && wd[0] != -1)
result += result.length > 0 ? ",#{OSM_DAYS[wd[0]]}" : OSM_DAYS[wd[0]]
first_in_row = wd[0]
for i in 1...wd.length
if wd[i-1] != wd[i] - 1
if first_in_row != wd[i-1]
if wd[i-1] - first_in_row == 1
result += ",#{OSM_DAYS[wd[i-1]]}"
else
result += "-#{OSM_DAYS[wd[i-1]]}"
end
end
result += ",#{OSM_DAYS[wd[i]]}"
first_in_row = wd[i]
elsif i == wd.length - 1
if wd[i] - first_in_row == 1
result += ",#{OSM_DAYS[wd[i]]}"
else
result += "-#{OSM_DAYS[wd[i]]}"
end
end
end
end
if result == "Mo-Su"
result = ""
end
return result
end
|
#same_kind_as?(date) ⇒ Boolean
124
125
126
|
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 124
def same_kind_as?(date)
@wide_type == date.wide_type && date.same_weekdays?(@weekdays)
end
|
#same_weekdays?(weekdays) ⇒ Boolean
128
129
130
|
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 128
def same_weekdays?(weekdays)
weekdays == @weekdays
end
|