opening_hours_converter 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/opening_hours_converter/constants.rb +0 -2
- data/lib/opening_hours_converter/date_range.rb +7 -2
- data/lib/opening_hours_converter/opening_hours_builder.rb +6 -11
- data/lib/opening_hours_converter/opening_hours_parser.rb +26 -21
- data/lib/opening_hours_converter/opening_hours_rule.rb +11 -305
- data/lib/opening_hours_converter/wide_interval.rb +18 -0
- data/lib/opening_hours_converter.rb +3 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc8f56307535202e3d7fed87c5c4ec147906e7de
|
4
|
+
data.tar.gz: 9a271684ea3fa41b7969408d19754efd7f571151
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb354f6b4afc01071ec3e26e3f0d5f201b7c0135af63c507134bb4f6bb191e3b89b591c78518b97c90a352cc685da78bc86478ce44b4cf57888a343801f2897b
|
7
|
+
data.tar.gz: 040b9fedf42908fb206b5bae2a3d7ab082f08c364246d79a0d3650ab36a8929501c63207d92bc65cdabe41f6efc72a16170fd50d977bddb8d6f64aca862945d2
|
@@ -1,10 +1,11 @@
|
|
1
1
|
module OpeningHoursConverter
|
2
2
|
class DateRange
|
3
|
-
attr_accessor :wide_interval, :typical
|
3
|
+
attr_accessor :wide_interval, :typical, :comment
|
4
4
|
|
5
5
|
def initialize(w=nil)
|
6
6
|
@wide_interval = nil
|
7
7
|
@typical = nil
|
8
|
+
@comment = ""
|
8
9
|
update_range(w)
|
9
10
|
end
|
10
11
|
|
@@ -33,12 +34,16 @@ module OpeningHoursConverter
|
|
33
34
|
end
|
34
35
|
end
|
35
36
|
|
37
|
+
def add_comment(comment="")
|
38
|
+
@comment += comment
|
39
|
+
end
|
40
|
+
|
36
41
|
def has_same_typical?(date_range)
|
37
42
|
defines_typical_day? == date_range.defines_typical_day? && @typical.same_as?(date_range.typical)
|
38
43
|
end
|
39
44
|
|
40
45
|
def is_general_for?(date_range)
|
41
|
-
defines_typical_day? == date_range.defines_typical_day? && @wide_interval.contains?(date_range.wide_interval)
|
46
|
+
defines_typical_day? == date_range.defines_typical_day? && @wide_interval.contains?(date_range.wide_interval) && @comment == date_range.comment
|
42
47
|
end
|
43
48
|
end
|
44
49
|
end
|
@@ -16,6 +16,7 @@ module OpeningHoursConverter
|
|
16
16
|
range_general = nil
|
17
17
|
range_general_for = nil
|
18
18
|
range_general_id = date_range_index - 1
|
19
|
+
|
19
20
|
while range_general_id >= 0 && range_general.nil?
|
20
21
|
if !date_range.nil?
|
21
22
|
general_for = date_ranges[range_general_id].is_general_for?(date_range)
|
@@ -27,7 +28,6 @@ module OpeningHoursConverter
|
|
27
28
|
end
|
28
29
|
range_general_id -= 1
|
29
30
|
end
|
30
|
-
# binding.pry
|
31
31
|
if date_range_index == 0 || range_general.nil?
|
32
32
|
if date_range.defines_typical_week?
|
33
33
|
if !range_general_for.nil?
|
@@ -39,17 +39,16 @@ module OpeningHoursConverter
|
|
39
39
|
oh_rules = build_day(date_range)
|
40
40
|
end
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
# while oh_rule_index < oh_rules.length
|
42
|
+
oh_rules.each_with_index do |rule, i|
|
43
|
+
oh_rules[i].add_comment(date_range.comment)
|
44
|
+
end
|
46
45
|
|
47
|
-
|
46
|
+
oh_rules.map do |oh_rule|
|
48
47
|
oh_rule_added = false
|
49
48
|
rule_index = 0
|
50
49
|
|
51
50
|
while !oh_rule_added && rule_index < rules.length
|
52
|
-
if rules[rule_index].same_time?(oh_rule) && !rules[rule_index].equals(oh_rule)
|
51
|
+
if rules[rule_index].same_time?(oh_rule) && !rules[rule_index].equals(oh_rule) && rules[rule_index].comment == oh_rule.comment
|
53
52
|
begin
|
54
53
|
for date_id in 0...oh_rule.date.length
|
55
54
|
rules[rule_index].add_date(oh_rule.date[date_id])
|
@@ -82,10 +81,8 @@ module OpeningHoursConverter
|
|
82
81
|
end
|
83
82
|
end
|
84
83
|
end
|
85
|
-
|
86
84
|
# binding.pry
|
87
85
|
|
88
|
-
|
89
86
|
result = ""
|
90
87
|
rules.each_with_index do |rule, rule_index|
|
91
88
|
if rule_index > 0
|
@@ -114,7 +111,6 @@ module OpeningHoursConverter
|
|
114
111
|
end
|
115
112
|
|
116
113
|
def build_week(date_range)
|
117
|
-
# binding.pry
|
118
114
|
result = []
|
119
115
|
intervals = date_range.typical.get_intervals(true)
|
120
116
|
time_intervals = create_time_intervals(date_range.wide_interval, date_range.wide_interval.type, intervals)
|
@@ -289,7 +285,6 @@ module OpeningHoursConverter
|
|
289
285
|
end
|
290
286
|
|
291
287
|
def create_time_intervals(wide_interval, type, intervals)
|
292
|
-
# binding.pry
|
293
288
|
monday0 = -1
|
294
289
|
sunday24 = -1
|
295
290
|
|
@@ -17,12 +17,14 @@ module OpeningHoursConverter
|
|
17
17
|
@RGX_YEAR = /^(\d{4})(\-(\d{4}))?$/
|
18
18
|
@RGX_YEAR_MONTH_DAY = /^(\d{4}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([012]?[0-9]|3[01])(\-((\d{4}) )?((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) )?([012]?[0-9]|3[01]))?\:?$/
|
19
19
|
@RGX_YEAR_MONTH = /^(\d{4}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(\-((\d{4}) )?((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)))?\:?$/
|
20
|
+
@RGX_COMMENT = /^\"[^\"]*\"$/
|
20
21
|
end
|
21
22
|
|
22
23
|
def parse(oh)
|
23
24
|
result = []
|
24
25
|
blocks = oh.split(';')
|
25
26
|
|
27
|
+
comment = nil
|
26
28
|
rule_modifier = nil
|
27
29
|
time_selector = nil
|
28
30
|
weekday_selector = nil
|
@@ -47,6 +49,13 @@ module OpeningHoursConverter
|
|
47
49
|
tokens = tokenize(block)
|
48
50
|
current_token = tokens.length - 1
|
49
51
|
|
52
|
+
# get comment
|
53
|
+
if current_token >= 0 && is_comment?(tokens[current_token])
|
54
|
+
comment = tokens[current_token]
|
55
|
+
current_token -= 1
|
56
|
+
end
|
57
|
+
|
58
|
+
|
50
59
|
# get state
|
51
60
|
if current_token >= 0 && is_rule_modifier?(tokens[current_token])
|
52
61
|
rule_modifier = tokens[current_token].downcase
|
@@ -70,24 +79,6 @@ module OpeningHoursConverter
|
|
70
79
|
current_token -= 1
|
71
80
|
end
|
72
81
|
|
73
|
-
# years = []
|
74
|
-
# if current_token >= 0 && is_year?(tokens[current_token])
|
75
|
-
# year_selector = tokens[current_token]
|
76
|
-
# year_selector = year_selector.split(',')
|
77
|
-
# year_selector.each do |y|
|
78
|
-
# single_year = y.gsub(/\:$/, '').split('-')
|
79
|
-
# year_from = single_year[0]
|
80
|
-
# if single_year.length > 1
|
81
|
-
# year_to = single_year[1]
|
82
|
-
# else
|
83
|
-
# year_to = year_from
|
84
|
-
# end
|
85
|
-
|
86
|
-
# years << {from: year_from, to: year_to}
|
87
|
-
# end
|
88
|
-
# current_token -= 1
|
89
|
-
# end
|
90
|
-
|
91
82
|
months = []
|
92
83
|
years = []
|
93
84
|
if current_token >= 0
|
@@ -183,13 +174,12 @@ module OpeningHoursConverter
|
|
183
174
|
times << {from: 0, to: 24*60}
|
184
175
|
end
|
185
176
|
|
186
|
-
# pasur
|
187
177
|
date_ranges.each do |dr|
|
188
178
|
found_date_range = false
|
189
179
|
res_dr_id = 0
|
190
180
|
|
191
181
|
while res_dr_id < result.length && !found_date_range
|
192
|
-
if result[res_dr_id].wide_interval.equals(dr)
|
182
|
+
if result[res_dr_id].wide_interval.equals(dr) && result[res_dr_id].comment == comment
|
193
183
|
found_date_range = true
|
194
184
|
else
|
195
185
|
res_dr_id += 1
|
@@ -200,6 +190,9 @@ module OpeningHoursConverter
|
|
200
190
|
dr_obj = result[res_dr_id]
|
201
191
|
else
|
202
192
|
dr_obj = OpeningHoursConverter::DateRange.new(dr)
|
193
|
+
if !comment.nil?
|
194
|
+
dr_obj.add_comment(comment)
|
195
|
+
end
|
203
196
|
|
204
197
|
general = -1
|
205
198
|
for res_dr_id in 0...result.length
|
@@ -246,9 +239,12 @@ module OpeningHoursConverter
|
|
246
239
|
add_interval(dr_obj.typical, weekdays[wd_id], times[t_id])
|
247
240
|
end
|
248
241
|
end
|
242
|
+
|
243
|
+
|
249
244
|
end
|
250
245
|
end
|
251
246
|
end
|
247
|
+
|
252
248
|
return result
|
253
249
|
end
|
254
250
|
|
@@ -483,7 +479,13 @@ module OpeningHoursConverter
|
|
483
479
|
end
|
484
480
|
|
485
481
|
def tokenize(block)
|
486
|
-
block.split('
|
482
|
+
if block.split('"').length > 1
|
483
|
+
comment = block.split('"')[1]
|
484
|
+
tokens = block.split('"')[0].split(' ')
|
485
|
+
tokens << "\"#{comment}\""
|
486
|
+
else
|
487
|
+
block.split(' ')
|
488
|
+
end
|
487
489
|
end
|
488
490
|
|
489
491
|
def as_minutes(time)
|
@@ -491,6 +493,9 @@ module OpeningHoursConverter
|
|
491
493
|
values[0].to_i * 60 + values[1].to_i
|
492
494
|
end
|
493
495
|
|
496
|
+
def is_comment?(token)
|
497
|
+
!(@RGX_COMMENT =~ token).nil?
|
498
|
+
end
|
494
499
|
def is_rule_modifier?(token)
|
495
500
|
!(@RGX_RULE_MODIFIER =~ token).nil?
|
496
501
|
end
|
@@ -3,15 +3,15 @@ require 'opening_hours_converter/constants'
|
|
3
3
|
module OpeningHoursConverter
|
4
4
|
class OpeningHoursRule
|
5
5
|
include Constants
|
6
|
-
attr_accessor :date, :time
|
6
|
+
attr_accessor :date, :time, :comment
|
7
7
|
|
8
8
|
def initialize
|
9
9
|
@date = []
|
10
10
|
@time = []
|
11
|
+
@comment = ""
|
11
12
|
end
|
12
13
|
|
13
14
|
def get
|
14
|
-
get_wide_selector if @date.length > 1
|
15
15
|
result = ""
|
16
16
|
if @date.length > 0
|
17
17
|
result += get_wide_selector
|
@@ -42,14 +42,15 @@ module OpeningHoursConverter
|
|
42
42
|
result.gsub!("00:00-24:00", "24/7")
|
43
43
|
end
|
44
44
|
|
45
|
+
if !comment.nil? && comment.length != 0
|
46
|
+
result += " #{comment}"
|
47
|
+
end
|
45
48
|
|
46
|
-
result = clean(result)
|
47
49
|
result.strip
|
48
50
|
end
|
49
51
|
|
50
52
|
def get_wide_selector
|
51
|
-
|
52
|
-
years = build_day_array
|
53
|
+
years = OpeningHoursConverter::Year.build_day_array_from_dates(@date)
|
53
54
|
|
54
55
|
year_start = -1
|
55
56
|
month_start = -1
|
@@ -58,7 +59,6 @@ module OpeningHoursConverter
|
|
58
59
|
result = {}
|
59
60
|
|
60
61
|
if !years["always"].nil?
|
61
|
-
|
62
62
|
always = years.delete("always")
|
63
63
|
|
64
64
|
always.each_with_index do |month_array, month|
|
@@ -85,15 +85,6 @@ module OpeningHoursConverter
|
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
|
-
# years {
|
89
|
-
# 2017: [
|
90
|
-
# {start: {}, end: {}}
|
91
|
-
# ],
|
92
|
-
# multi_year: [
|
93
|
-
# {start: {}, {}}
|
94
|
-
# ]
|
95
|
-
# }
|
96
|
-
|
97
88
|
years.each do |year, months|
|
98
89
|
months.each_with_index do |month_array, month|
|
99
90
|
month_array.each_with_index do |day_bool, day|
|
@@ -137,11 +128,6 @@ module OpeningHoursConverter
|
|
137
128
|
result["multi_year"]["#{year_start}-#{year}"] << { start: { day: day_start, month: month_start, year: year_start },
|
138
129
|
end: end_res }
|
139
130
|
end
|
140
|
-
# end_res = day == 0 ?
|
141
|
-
# month == 0 ?
|
142
|
-
# { day: 30, month: 11, year: year } : { day: MONTH_END_DAY[month-1]-1, month: month-1, year: year } :
|
143
|
-
# { day: day-1, month: month, year: year }
|
144
|
-
# result << {start: {day: day_start, month: month_start, year: year_start}, end: end_res}
|
145
131
|
year_start = -1
|
146
132
|
month_start = -1
|
147
133
|
day_start = -1
|
@@ -149,10 +135,7 @@ module OpeningHoursConverter
|
|
149
135
|
end
|
150
136
|
end
|
151
137
|
end
|
152
|
-
|
153
|
-
|
154
138
|
result_to_string(result)
|
155
|
-
|
156
139
|
end
|
157
140
|
|
158
141
|
def result_to_string(result)
|
@@ -241,287 +224,6 @@ module OpeningHoursConverter
|
|
241
224
|
r[:end][:month] == 11 && ends_month?(r)
|
242
225
|
end
|
243
226
|
|
244
|
-
def build_day_array
|
245
|
-
years = {}
|
246
|
-
@date.each do |date|
|
247
|
-
if !date.wide.start.nil? && !date.wide.start[:year].nil?
|
248
|
-
if date.wide.end.nil? || date.wide.end[:year].nil? || date.wide.start[:year] == date.wide.end[:year]
|
249
|
-
if !years[date.wide.start[:year]].nil?
|
250
|
-
years = process_single_year(date, years)
|
251
|
-
else
|
252
|
-
years[date.wide.start[:year]] = Array.new(OSM_MONTHS.length) { |i| Array.new(MONTH_END_DAY[i]) { false } }
|
253
|
-
years = process_single_year(date, years)
|
254
|
-
end
|
255
|
-
else
|
256
|
-
for year in date.wide.start[:year]..date.wide.end[:year]
|
257
|
-
years[year] ||= Array.new(OSM_MONTHS.length) { |i| Array.new(MONTH_END_DAY[i]) { false } }
|
258
|
-
end
|
259
|
-
process_multiple_years(date, years)
|
260
|
-
end
|
261
|
-
else
|
262
|
-
years["always"] ||= Array.new(OSM_MONTHS.length) { |i| Array.new(MONTH_END_DAY[i]) { false } }
|
263
|
-
years = process_always(date, years)
|
264
|
-
end
|
265
|
-
end
|
266
|
-
years
|
267
|
-
end
|
268
|
-
|
269
|
-
def process_always(date, years, get_iterator=false)
|
270
|
-
if !get_iterator
|
271
|
-
if date.wide.start.nil?
|
272
|
-
years["always"] = Array.new(OSM_MONTHS.length) { |i| Array.new(MONTH_END_DAY[i]) { true } }
|
273
|
-
elsif !date.wide.start[:day].nil?
|
274
|
-
if date.wide.end.nil? || (date.wide.end[:month].nil? && date.wide.end[:day].nil?) ||
|
275
|
-
(date.wide.start[:month] == date.wide.end[:month] && date.wide.start[:day] == date.wide.end[:day])
|
276
|
-
years["always"][date.wide.start[:month]-1][date.wide.start[:day]-1] = true
|
277
|
-
elsif date.wide.start[:month] == date.wide.end[:month]
|
278
|
-
for day in date.wide.start[:day]-1..date.wide.end[:day]-1
|
279
|
-
years["always"][date.wide.start[:month]-1][day] = true
|
280
|
-
end
|
281
|
-
elsif date.wide.start[:month] != date.wide.end[:month]
|
282
|
-
for month in date.wide.start[:month]-1..date.wide.end[:month]-1
|
283
|
-
if month == date.wide.start[:month]-1
|
284
|
-
for day in date.wide.start[:day]-1...MONTH_END_DAY[month]
|
285
|
-
years["always"][month][day] = true
|
286
|
-
end
|
287
|
-
elsif month == date.wide.end[:month]-1
|
288
|
-
for day in 0..date.wide.end[:day]-1
|
289
|
-
years["always"][month][day] = true
|
290
|
-
end
|
291
|
-
else
|
292
|
-
for day in 0...MONTH_END_DAY[month]
|
293
|
-
years["always"][month][day] = true
|
294
|
-
end
|
295
|
-
end
|
296
|
-
end
|
297
|
-
end
|
298
|
-
elsif !date.wide.start[:month].nil?
|
299
|
-
if date.wide.end.nil? || date.wide.end[:month].nil? || date.wide.start[:month] == date.wide.end[:month]
|
300
|
-
years["always"][date.wide.start[:month]-1].each_with_index do |month, i|
|
301
|
-
years["always"][date.wide.start[:month]-1][i] = true
|
302
|
-
end
|
303
|
-
else
|
304
|
-
for month in date.wide.start[:month]-1..date.wide.end[:month]-1
|
305
|
-
years["always"][month].each_with_index do |day, i|
|
306
|
-
years["always"][month][i] = true
|
307
|
-
end
|
308
|
-
end
|
309
|
-
end
|
310
|
-
end
|
311
|
-
else
|
312
|
-
for year in YEAR_MIN..YEAR_MAX
|
313
|
-
if years[year].nil?
|
314
|
-
years[year] = Array.new(OSM_MONTHS.length) { |i| Array.new(MONTH_END_DAY[i]) { false } }
|
315
|
-
end
|
316
|
-
if !date.wide.start[:day].nil?
|
317
|
-
if date.wide.end.nil? || (date.wide.end[:month].nil? && date.wide.end[:day].nil?) ||
|
318
|
-
(date.wide.start[:month] == date.wide.end[:month] && date.wide.start[:day] == date.wide.end[:day])
|
319
|
-
years[year][date.wide.start[:month]-1][date.wide.start[:day]-1] = true
|
320
|
-
elsif date.wide.start[:month] == date.wide.end[:month]
|
321
|
-
for day in date.wide.start[:day]-1..date.wide.end[:day]-1
|
322
|
-
years[year][date.wide.start[:month]-1][day] = true
|
323
|
-
end
|
324
|
-
elsif date.wide.start[:month] != date.wide.end[:month]
|
325
|
-
for month in date.wide.start[:month]-1..date.wide.end[:month]-1
|
326
|
-
if month == date.wide.start[:month]-1
|
327
|
-
for day in date.wide.start[:day]-1...MONTH_END_DAY[month]
|
328
|
-
years[year][month][day] = true
|
329
|
-
end
|
330
|
-
elsif month == date.wide.end[:month]-1
|
331
|
-
for day in 0..date.wide.end[:day]-1
|
332
|
-
years[year][month][day] = true
|
333
|
-
end
|
334
|
-
else
|
335
|
-
for day in 0...MONTH_END_DAY[month]
|
336
|
-
years[year][month][day] = true
|
337
|
-
end
|
338
|
-
end
|
339
|
-
end
|
340
|
-
end
|
341
|
-
elsif !date.wide.start[:month].nil?
|
342
|
-
if date.wide.end.nil? || date.wide.end[:month].nil? || date.wide.start[:month] == date.wide.end[:month]
|
343
|
-
years[year][date.wide.start[:month]-1].each_with_index do |month, i|
|
344
|
-
years[year][date.wide.start[:month]-1][i] = true
|
345
|
-
end
|
346
|
-
else
|
347
|
-
for month in date.wide.start[:month]-1..date.wide.end[:month]-1
|
348
|
-
years[year][month].each_with_index do |day, i|
|
349
|
-
years[year][month][i] = true
|
350
|
-
end
|
351
|
-
end
|
352
|
-
end
|
353
|
-
end
|
354
|
-
end
|
355
|
-
end
|
356
|
-
return years
|
357
|
-
end
|
358
|
-
|
359
|
-
def process_multiple_years(date, years)
|
360
|
-
if date.wide.type == "year"
|
361
|
-
for year in date.wide.start[:year]..date.wide.end[:year]
|
362
|
-
years[year].each_with_index do |month,i|
|
363
|
-
month.each_with_index do |day,j|
|
364
|
-
years[year][i][j] = true
|
365
|
-
end
|
366
|
-
end
|
367
|
-
end
|
368
|
-
elsif date.wide.type == "month"
|
369
|
-
for year in date.wide.start[:year]..date.wide.end[:year]
|
370
|
-
if year == date.wide.start[:year]
|
371
|
-
for month in date.wide.start[:month]-1..11
|
372
|
-
years[year][month].each_with_index do |day, i|
|
373
|
-
years[year][month][i] = true
|
374
|
-
end
|
375
|
-
end
|
376
|
-
elsif year == date.wide.end[:year]
|
377
|
-
for month in 0..date.wide.end[:month]-1
|
378
|
-
years[year][month].each_with_index do |day, i|
|
379
|
-
years[year][month][i] = true
|
380
|
-
end
|
381
|
-
end
|
382
|
-
else
|
383
|
-
for month in 0..11
|
384
|
-
years[year][month].each_with_index do |day, i|
|
385
|
-
years[year][month][i] = true
|
386
|
-
end
|
387
|
-
end
|
388
|
-
end
|
389
|
-
end
|
390
|
-
elsif date.wide.type == "day"
|
391
|
-
for year in date.wide.start[:year]..date.wide.end[:year]
|
392
|
-
if year == date.wide.start[:year]
|
393
|
-
for month in date.wide.start[:month]-1..11
|
394
|
-
if month == date.wide.start[:month]-1
|
395
|
-
for day in date.wide.start[:day]-1...MONTH_END_DAY[month]
|
396
|
-
years[year][month][day] = true
|
397
|
-
end
|
398
|
-
else
|
399
|
-
for day in 0...MONTH_END_DAY[month]
|
400
|
-
years[year][month][day] = true
|
401
|
-
end
|
402
|
-
end
|
403
|
-
end
|
404
|
-
elsif year == date.wide.end[:year]
|
405
|
-
for month in 0..date.wide.end[:month]-1
|
406
|
-
if month == date.wide.end[:month]-1
|
407
|
-
for day in 0..date.wide.end[:day]-1
|
408
|
-
years[year][month][day] = true
|
409
|
-
end
|
410
|
-
else
|
411
|
-
for day in 0...MONTH_END_DAY[month]
|
412
|
-
years[year][month][day] = true
|
413
|
-
end
|
414
|
-
end
|
415
|
-
end
|
416
|
-
else
|
417
|
-
for month in 0..11
|
418
|
-
for day in 0...MONTH_END_DAY[month]
|
419
|
-
years[year][month][day] = true
|
420
|
-
end
|
421
|
-
end
|
422
|
-
end
|
423
|
-
end
|
424
|
-
end
|
425
|
-
return years
|
426
|
-
end
|
427
|
-
|
428
|
-
def process_single_year(date, years, always=false)
|
429
|
-
if date.wide.type == "year"
|
430
|
-
years[date.wide.start[:year]].each_with_index do |month,i|
|
431
|
-
month.each_with_index do |day,j|
|
432
|
-
years[date.wide.start[:year]][i][j] = true
|
433
|
-
end
|
434
|
-
end
|
435
|
-
elsif date.wide.type == "month"
|
436
|
-
if date.wide.end.nil? || date.wide.end[:month].nil? || date.wide.start[:month] == date.wide.end[:month]
|
437
|
-
years[date.wide.start[:year]][date.wide.start[:month]-1].each_with_index do |month, i|
|
438
|
-
years[date.wide.start[:year]][date.wide.start[:month]-1][i] = true
|
439
|
-
end
|
440
|
-
else
|
441
|
-
for month in date.wide.start[:month]-1..date.wide.end[:month]-1
|
442
|
-
years[date.wide.start[:year]][month].each_with_index do |day, i|
|
443
|
-
years[date.wide.start[:year]][month][i] = true
|
444
|
-
end
|
445
|
-
end
|
446
|
-
end
|
447
|
-
elsif date.wide.type == "day"
|
448
|
-
if date.wide.start[:month] == date.wide.end[:month] || date.wide.end[:month].nil?
|
449
|
-
if date.wide.start[:day] == date.wide.end[:day]
|
450
|
-
years[date.wide.start[:year]][date.wide.start[:month]-1][date.wide.start[:day]-1] = true
|
451
|
-
else
|
452
|
-
for day in date.wide.start[:day]-1..date.wide.end[:day]-1
|
453
|
-
years[date.wide.start[:year]][date.wide.start[:month]-1][day] = true
|
454
|
-
end
|
455
|
-
end
|
456
|
-
else
|
457
|
-
for month in date.wide.start[:month]-1..date.wide.end[:month]-1
|
458
|
-
if month == date.wide.start[:month]-1
|
459
|
-
for day in date.wide.start[:day]-1...MONTH_END_DAY[month]
|
460
|
-
years[date.wide.start[:year]][month][day] = true
|
461
|
-
end
|
462
|
-
elsif month == date.wide.end[:month]-1
|
463
|
-
for day in 0..date.wide.end[:day]-1
|
464
|
-
years[date.wide.start[:year]][month][day] = true
|
465
|
-
end
|
466
|
-
else
|
467
|
-
for day in 0...MONTH_END_DAY[month]
|
468
|
-
years[date.wide.start[:year]][month][day] = true
|
469
|
-
end
|
470
|
-
end
|
471
|
-
end
|
472
|
-
end
|
473
|
-
end
|
474
|
-
return years
|
475
|
-
end
|
476
|
-
|
477
|
-
def clean(result)
|
478
|
-
result = remove_duplicate_year(result)
|
479
|
-
result = remove_bad_spaces(result)
|
480
|
-
end
|
481
|
-
|
482
|
-
def remove_bad_spaces(result)
|
483
|
-
return result if (result =~ /\,\s/).nil?
|
484
|
-
return result.split(', ').join(',')
|
485
|
-
end
|
486
|
-
|
487
|
-
def remove_duplicate_year(result)
|
488
|
-
return result if (result =~ /(\d{4})[^;]+(\1)/).nil? && (result =~ /(\d{4}\-\d{4})[^;]+(\1)/).nil?
|
489
|
-
result_parts = result.split(';')
|
490
|
-
sanitized_parts = []
|
491
|
-
result_parts.each do |rp|
|
492
|
-
|
493
|
-
# string with year range repeating separated by a comma
|
494
|
-
if !(rp =~ /(\d{4}\-\d{4})\,(\1)/).nil?
|
495
|
-
first_occurence = (rp =~ /(\d{4}\-\d{4})/)
|
496
|
-
years = result[first_occurence...first_occurence + 9]
|
497
|
-
|
498
|
-
sanitized_parts << years + rp[first_occurence + 10, rp.length].split(year).join('')
|
499
|
-
|
500
|
-
# string with year repeating separrated by a comma
|
501
|
-
elsif !(rp =~ /(\d{4})\,(\1)/).nil?
|
502
|
-
first_occurence = (rp =~ /(\d{4})/)
|
503
|
-
year = result[first_occurence...first_occurence + 4]
|
504
|
-
|
505
|
-
sanitized_parts << year + rp[first_occurence + 5, rp.length].split(year).join('')
|
506
|
-
|
507
|
-
# string with year range repeating
|
508
|
-
elsif !(rp =~ /(\d{4}\-\d{4})[^;]+(\1)/).nil?
|
509
|
-
first_occurence = (rp =~ /(\d{4}\-\d{4})/)
|
510
|
-
years = result[first_occurence...first_occurence + 9]
|
511
|
-
|
512
|
-
sanitized_parts << years + rp[first_occurence + 9, rp.length].split(year).join('')
|
513
|
-
|
514
|
-
# string with year repeating
|
515
|
-
elsif !(rp =~ /(\d{4})[^;]+(\1)/).nil?
|
516
|
-
first_occurence = (rp =~ /(\d{4})/)
|
517
|
-
year = result[first_occurence...first_occurence + 4]
|
518
|
-
|
519
|
-
sanitized_parts << year + rp[first_occurence + 4, rp.length].split(year).join('')
|
520
|
-
end
|
521
|
-
end
|
522
|
-
sanitized_parts.join('')
|
523
|
-
end
|
524
|
-
|
525
227
|
def same_time?(o)
|
526
228
|
if o.nil? || o.time.length != @time.length
|
527
229
|
return false
|
@@ -577,7 +279,7 @@ module OpeningHoursConverter
|
|
577
279
|
if @date.length == 0 || date.same_weekdays?(@date.first.weekdays)
|
578
280
|
@date << date
|
579
281
|
else
|
580
|
-
if
|
282
|
+
if !@date.first.same_weekdays?(date.weekdays)
|
581
283
|
raise ArgumentError, "This date #{@date.inspect} can't be added to this rule #{self.inspect}"
|
582
284
|
end
|
583
285
|
end
|
@@ -590,5 +292,9 @@ module OpeningHoursConverter
|
|
590
292
|
raise ArgumentError, "This time can't be added to this rule"
|
591
293
|
end
|
592
294
|
end
|
295
|
+
|
296
|
+
def add_comment(comment)
|
297
|
+
@comment = comment
|
298
|
+
end
|
593
299
|
end
|
594
300
|
end
|
@@ -80,6 +80,24 @@ module OpeningHoursConverter
|
|
80
80
|
self
|
81
81
|
end
|
82
82
|
|
83
|
+
def date_time(start_date, end_date=nil)
|
84
|
+
if start_date.nil?
|
85
|
+
raise(ArgumentError, "start_date is required")
|
86
|
+
end
|
87
|
+
if !start_date.instance_of?(DateTime)
|
88
|
+
raise(ArgumentError, "start_date is not a DateTime")
|
89
|
+
end
|
90
|
+
if !end_date.instance_of?(DateTime)
|
91
|
+
raise(ArgumentError, "end_date is not a DateTime")
|
92
|
+
end
|
93
|
+
@start = { day: start_date.day, month: start_date.month, year: start_date.year }
|
94
|
+
if !end_date.nil? && end_date != start_date
|
95
|
+
@end = { day: end_date.day, month: end_date.month, year: end_date.year }
|
96
|
+
end
|
97
|
+
@type = "day"
|
98
|
+
self
|
99
|
+
end
|
100
|
+
|
83
101
|
def month(start_month, start_year=nil, end_month=nil, end_year=nil)
|
84
102
|
if start_month.nil?
|
85
103
|
raise(ArgumentError, "start_month is required")
|
@@ -2,12 +2,14 @@ module OpeningHoursConverter
|
|
2
2
|
require 'date'
|
3
3
|
require_relative './opening_hours_converter/date_range'
|
4
4
|
require_relative './opening_hours_converter/day'
|
5
|
+
require_relative './opening_hours_converter/week'
|
6
|
+
require_relative './opening_hours_converter/year'
|
5
7
|
require_relative './opening_hours_converter/interval'
|
8
|
+
require_relative './opening_hours_converter/iterator'
|
6
9
|
require_relative './opening_hours_converter/opening_hours_builder'
|
7
10
|
require_relative './opening_hours_converter/opening_hours_date'
|
8
11
|
require_relative './opening_hours_converter/opening_hours_parser'
|
9
12
|
require_relative './opening_hours_converter/opening_hours_rule'
|
10
13
|
require_relative './opening_hours_converter/opening_hours_time'
|
11
|
-
require_relative './opening_hours_converter/week'
|
12
14
|
require_relative './opening_hours_converter/wide_interval'
|
13
15
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opening_hours_converter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ziserman Martin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Datetime range to openinghours, openinghours to datetime range. Very
|
14
14
|
strongly inspired by yohours.
|