Class: OpeningHoursConverter::OpeningHoursRule

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/opening_hours_converter/opening_hours_rule.rb

Constant Summary

Constants included from Constants

Constants::DAYS, Constants::DAYS_MAX, Constants::EASTER_WEEKDAY, Constants::IRL_DAYS, Constants::IRL_MONTHS, Constants::MINUTES_MAX, Constants::MONTH_END_DAY, Constants::OSM_DAYS, Constants::OSM_DAYS_ABBREVIATIONS, Constants::OSM_MONTHS, Constants::PH_WEEKDAY, Constants::YEAR_DAYS_MAX

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOpeningHoursRule

Returns a new instance of OpeningHoursRule.



8
9
10
11
12
13
14
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 8

def initialize
  @date = []
  @time = []
  @is_defined_off = false
  @is_defined_unknown = false
  @comment = ''
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



6
7
8
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 6

def comment
  @comment
end

#dateObject

Returns the value of attribute date.



6
7
8
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 6

def date
  @date
end

#is_defined_offObject

Returns the value of attribute is_defined_off.



6
7
8
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 6

def is_defined_off
  @is_defined_off
end

#is_defined_unknownObject

Returns the value of attribute is_defined_unknown.



6
7
8
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 6

def is_defined_unknown
  @is_defined_unknown
end

#timeObject

Returns the value of attribute time.



6
7
8
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 6

def time
  @time
end

Instance Method Details

#add_comment(comment) ⇒ Object



405
406
407
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 405

def add_comment(comment)
  @comment = comment
end

#add_date(date) ⇒ Object



380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 380

def add_date(date)
  if date.nil? || !date.instance_of?(OpeningHoursConverter::OpeningHoursDate)
    raise ArgumentError
  end

  if @date.empty? || date.same_weekdays?(@date.first.weekdays)
    @date << date
  else
    if !@date.first.same_weekdays?(date.weekdays)
      raise ArgumentError, "This date #{@date.inspect} can't be added to this rule #{inspect}"
    end
  end
end

#add_easter_weekdayObject



370
371
372
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 370

def add_easter_weekday
  @date.each(&:add_easter_weekday)
end

#add_overwritten_weekday(weekday) ⇒ Object



374
375
376
377
378
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 374

def add_overwritten_weekday(weekday)
  @date.each do |d|
    d.add_overwritten_weekday(weekday)
  end
end

#add_ph_weekdayObject



366
367
368
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 366

def add_ph_weekday
  @date.each(&:add_ph_weekday)
end

#add_time(time) ⇒ Object



401
402
403
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 401

def add_time(time)
  @time << time if (@time.empty? || @time[0].get != 'off') && !include_time?(time)
end

#add_weekday(weekday) ⇒ Object



360
361
362
363
364
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 360

def add_weekday(weekday)
  @date.each do |d|
    d.add_weekday(weekday)
  end
end

#ends_month?(r) ⇒ Boolean

Returns:

  • (Boolean)


286
287
288
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 286

def ends_month?(r)
  r[:end][:day] == MONTH_END_DAY[r[:end][:month]] - 1
end

#ends_year?(r) ⇒ Boolean

Returns:

  • (Boolean)


294
295
296
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 294

def ends_year?(r)
  r[:end][:month] == 11 && ends_month?(r)
end

#equals(o) ⇒ Object



347
348
349
350
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 347

def equals(o)
  return false unless o.instance_of?(OpeningHoursConverter::OpeningHoursRule)
  (same_time?(o) && same_date?(o))
end

#getObject



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
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 16

def get
  result = ''

  result += get_wide_selector if !@date.empty?

  if !@date.empty?
    wd = @date[0].get_weekdays
    result += " #{wd}" if !wd.empty?
  end
  if @is_defined_unknown
    result += ' unknown'
  elsif @is_defined_off
    result += ' off'
  elsif !@time.empty?
    result += ' '
    @time.uniq.each_with_index do |t, i|
      result += ',' if i > 0
      result += t.get
    end
  else
    result += ' off'
  end

  rgx_day = /(Mo|Tu|We|Th|Fr|Sa|Su)/

  if result.strip == '00:00-24:00' || (!(result =~ /00:00-24:00/).nil? && (result =~ rgx_day).nil?)
    result.gsub!('00:00-24:00', '24/7')
  end

  result += " #{comment}" if !comment.nil? && !comment.empty?

  result.strip
end

#get_wide_selectorObject



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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 50

def get_wide_selector
  if @date.length == 1 && @date[0].wide_interval.type == 'holiday'
    holiday = @date[0].wide_interval.start[:holiday]
    if @date[0].wide_interval.start[:year].nil?
      return holiday
    else
      if @date[0].wide_interval.end && @date[0].wide_interval.end[:year]
        return "#{@date[0].wide_interval.start[:year]}-#{@date[0].wide_interval.end[:year]} #{holiday}"
      else
        return "#{@date[0].wide_interval.start[:year]} #{holiday}"
      end
    end
  end
  # A year range with a step ("2020-2030/2") leaves the years it skips out
  # of the day array, which would come back as the list of years they are
  # rather than as the range they were written as.
  return @date[0].wide_interval.get_time_selector if stepped_year?

  years = OpeningHoursConverter::Year.build_day_array_from_dates(@date)
  year_start = -1
  month_start = -1
  day_start = -1

  return result_to_string(years) if is_only_week_interval?(years)

  result = {}

  if !years['always'].nil?
    always = years.delete('always')

    always.each_with_index do |month_array, month|
      month_array.each_with_index do |day_bool, day|
        if day_bool && month_start < 0
          month_start = month
          day_start = day
        end
        if day_bool && month_start >= 0 && month == 11 && day == 30
          result['always'] ||= []
          result['always'] << { start: { day: day_start, month: month_start }, end: { day: 30, month: 11 } }
          month_start = -1
          day_start = -1
        elsif !day_bool && month_start >= 0
          result['always'] ||= []
          end_res = day == 0 ?
            month == 0 ?
              { day: 30, month: 11 } : { day: MONTH_END_DAY[month - 1] - 1, month: month - 1 } :
            { day: day - 1, month: month }
          result['always'] << { start: { day: day_start, month: month_start }, end: end_res }
          month_start = -1
          day_start = -1
        end
      end
    end
  end

  years.each do |year, months|
    months.each_with_index do |month_array, month|
      month_array.each_with_index do |day_bool, day|
        if day_bool && year_start < 0
          year_start = year
          month_start = month
          day_start = day
        end
        if day_bool && year_start >= 0 && month == 11 && day == 30 && years[year + 1].nil?
          if year_start == year
            result[year] ||= []
            result[year] << { start: { day: day_start, month: month_start }, end: { day: 30, month: 11 } }
          else
            result['multi_year'] ||= {}
            result['multi_year']["#{year_start}-#{year}"] ||= []
            result['multi_year']["#{year_start}-#{year}"] << { start: { day: day_start, month: month_start, year: year_start },
                                                               end: { day: 30, month: 11, year: year } }
          end
          year_start = -1
          month_start = -1
          day_start = -1
        elsif !day_bool && year_start >= 0
          end_res = {}

          end_res = if day == 0
                      if month == 0
                        { day: 30, month: 11 }
                      else
                        { day: MONTH_END_DAY[month - 1] - 1, month: month - 1 }
                      end
                    else
                      { day: day - 1, month: month }
                    end

          if year_start == year
            result[year] ||= []
            result[year] << { start: { day: day_start, month: month_start }, end: end_res }
          else
            result['multi_year'] ||= {}
            result['multi_year']["#{year_start}-#{year}"] ||= []
            end_res[:year] = year
            result['multi_year']["#{year_start}-#{year}"] << { start: { day: day_start, month: month_start, year: year_start },
                                                               end: end_res }
          end
          year_start = -1
          month_start = -1
          day_start = -1
        end
      end
    end
  end

  selector = result_to_string(result)
  selector += '+' if open_ended_year?
  selector
end

#has_overwritten_weekday?Boolean

Returns:

  • (Boolean)


356
357
358
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 356

def has_overwritten_weekday?
  !@date.empty? && !@date[0].weekdays_over.empty?
end

#has_weekday_index?Boolean

Returns:

  • (Boolean)


298
299
300
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 298

def has_weekday_index?
  @date.any? { |d| !d.weekday_index.nil? }
end

#include_time?(time) ⇒ Boolean

Returns:

  • (Boolean)


394
395
396
397
398
399
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 394

def include_time?(time)
  @time.each do |t|
    return true if t.start == time.start && t.end == time.end
  end
  false
end

#is_full_month?(r) ⇒ Boolean

Returns:

  • (Boolean)


266
267
268
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 266

def is_full_month?(r)
  is_same_month?(r) && starts_month?(r) && ends_month?(r)
end

#is_full_year?(r) ⇒ Boolean

Returns:

  • (Boolean)


262
263
264
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 262

def is_full_year?(r)
  starts_year?(r) && ends_year?(r)
end

#is_off?Boolean

Returns:

  • (Boolean)


352
353
354
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 352

def is_off?
  @time.empty? || (@time.length == 1 && time[0].start.nil?)
end

#is_only_week_interval?(r) ⇒ Boolean

Returns:

  • (Boolean)


251
252
253
254
255
256
257
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 251

def is_only_week_interval?(r)
  r.all? do |year, intervals|
    intervals.all? do |interval|
      is_week_interval?(interval)
    end
  end
end

#is_same_day?(r) ⇒ Boolean

Returns:

  • (Boolean)


278
279
280
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 278

def is_same_day?(r)
  r[:start][:day] == r[:end][:day]
end

#is_same_month?(r) ⇒ Boolean

Returns:

  • (Boolean)


274
275
276
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 274

def is_same_month?(r)
  r[:start][:month] == r[:end][:month]
end

#is_same_year?(r) ⇒ Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 270

def is_same_year?(r)
  r[:start][:year] == r[:end][:year]
end

#is_week_interval?(interval) ⇒ Boolean

Returns:

  • (Boolean)


258
259
260
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 258

def is_week_interval?(interval)
  !interval.is_a?(Array) && interval.key?(:start) && interval[:start].key?(:week)
end

#mergeable?(o) ⇒ Boolean

Returns:

  • (Boolean)


325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 325

def mergeable?(o)
  return true if o.nil?

  if o.date.length != @date.length
    if @date.any? { |date| date.wide_interval.type == "week" }
      indexes = (@date + o.date).map do |date|
        date.wide_interval.indexes
      end.uniq

      return false if indexes.length > 1
    end

    true
  else
    @date.each_with_index do |d, i|
      return false if (d.wide_interval.start&.dig(:year) != o.date[i].wide_interval.start&.dig(:year)) && d.wide_interval.type != o.date[i].wide_interval.type || (d.wide_interval.type == "week" && o.date[i].wide_interval.type == "week" && d.wide_interval.indexes != o.date[i].wide_interval.indexes)
    end

    true
  end
end

#open_ended_year?Boolean

"2020+" (from that year on, forever) has no upper bound to encode in the year/month/day array result_to_string reads, so it's appended here.

Returns:

  • (Boolean)


169
170
171
172
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 169

def open_ended_year?
  @date.length == 1 && @date[0].wide_interval.type == 'year' &&
    @date[0].wide_interval.open_ended && @date[0].wide_interval.end.nil?
end

#result_to_string(result) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 174

def result_to_string(result)
  str_result = ''
  if is_only_week_interval?(result)
    local_str = result.keys == ['always'] ? 'week ' : result.keys.length == 1 ? result.keys.first.to_s + ' week ' : result.keys.min.to_s + '-' + result.keys.max.to_s + ' week '

    intervals = result[result.keys.first]
    intervals.each do |interval|
      local_str += "," if !(local_str[local_str.length - 1] =~ /^[0-9]$/).nil?
      local_str +=
        if interval[:modifier].nil?
          if interval[:start][:week] == interval[:end][:week]
            interval[:start][:week].to_s
          else
            "#{interval[:start][:week].to_s + '-' + interval[:end][:week].to_s}"
          end
        else
          "#{interval[:start][:week].to_s + '-' + interval[:end][:week].to_s}/#{interval[:modifier].to_s}"
        end
    end
    str_result += local_str
    local_str = ''
  else
    result.each do |selector, intervals|
      if selector == 'always'
        intervals.each do |interval|
          str_result += ',' if !str_result.empty?
          if is_full_year?(interval)
          elsif starts_year?(interval) && ends_year?(interval)
            str_result += "#{interval[:start][:year]}-#{interval[:end][:year]}"
          elsif is_full_month?(interval)
            str_result += (OSM_MONTHS[interval[:start][:month]]).to_s
          elsif is_same_month?(interval)
            if is_same_day?(interval)
              str_result += "#{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day] + 1 < 10 ? "0#{interval[:start][:day] + 1}" : interval[:start][:day] + 1}"
            else
              str_result += "#{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day] + 1 < 10 ? "0#{interval[:start][:day] + 1}" : interval[:start][:day] + 1}-#{interval[:end][:day] + 1 < 10 ? "0#{interval[:end][:day] + 1}" : interval[:end][:day] + 1}"
            end
          elsif starts_month?(interval) && ends_month?(interval)
            str_result += "#{OSM_MONTHS[interval[:start][:month]]}-#{OSM_MONTHS[interval[:end][:month]]}"
          else
            str_result += "#{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day] + 1 < 10 ? "0#{interval[:start][:day] + 1}" : interval[:start][:day] + 1}-#{OSM_MONTHS[interval[:end][:month]]} #{interval[:end][:day] + 1 < 10 ? "0#{interval[:end][:day] + 1}" : interval[:end][:day] + 1}"
          end
        end
      elsif selector == 'multi_year'
        intervals.each do |_years, intervals|
          intervals.each do |interval|
            str_result += ',' if !str_result.empty?
            if starts_year?(interval) && ends_year?(interval)
              str_result += "#{interval[:start][:year]}-#{interval[:end][:year]}"
            else
              str_result += "#{interval[:start][:year]} #{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day] + 1 < 10 ? "0#{interval[:start][:day] + 1}" : interval[:start][:day] + 1}-#{interval[:end][:year]} #{OSM_MONTHS[interval[:end][:month]]} #{interval[:end][:day] + 1 < 10 ? "0#{interval[:end][:day] + 1}" : interval[:end][:day] + 1}"
            end
          end
        end
      else
        local_str = "#{selector} "
        intervals.each do |interval|
          if is_full_year?(interval)
          elsif is_same_month?(interval)
            if is_same_day?(interval)
              local_str += "#{local_str.length > 5 ? ',' : ''}#{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day] + 1 < 10 ? "0#{interval[:start][:day] + 1}" : interval[:start][:day] + 1}"
            else
              local_str += "#{local_str.length > 5 ? ',' : ''}#{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day] + 1 < 10 ? "0#{interval[:start][:day] + 1}" : interval[:start][:day] + 1}-#{interval[:end][:day] + 1 < 10 ? "0#{interval[:end][:day] + 1}" : interval[:end][:day] + 1}"
            end
          else
            local_str += "#{local_str.length > 5 ? ',' : ''}#{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day] + 1 < 10 ? "0#{interval[:start][:day] + 1}" : interval[:start][:day] + 1}-#{selector} #{OSM_MONTHS[interval[:end][:month]]} #{interval[:end][:day] + 1 < 10 ? "0#{interval[:end][:day] + 1}" : interval[:end][:day] + 1}"
          end
          str_result += "#{!str_result.empty? ? ',' : ''}#{local_str}"
          local_str = ''
        end
      end
    end
  end

  str_result.strip
end

#same_date?(o) ⇒ Boolean

Returns:

  • (Boolean)


314
315
316
317
318
319
320
321
322
323
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 314

def same_date?(o)
  if o.nil? || o.date.length != @date.length
    false
  else
    @date.each_with_index do |d, i|
      return false if !d.wide_interval.equals(o.date[i].wide_interval)
    end
    true
  end
end

#same_time?(o) ⇒ Boolean

Returns:

  • (Boolean)


302
303
304
305
306
307
308
309
310
311
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 302

def same_time?(o)
  if o.nil? || o.time.length != @time.length || @is_defined_off != o.is_defined_off || @is_defined_unknown != o.is_defined_unknown || has_weekday_index? || o.has_weekday_index?
    false
  else
    @time.each_with_index do |t, i|
      return false if !t.equals(o.time[i])
    end
    true
  end
end

#starts_month?(r) ⇒ Boolean

Returns:

  • (Boolean)


282
283
284
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 282

def starts_month?(r)
  r[:start][:day] == 0
end

#starts_year?(r) ⇒ Boolean

Returns:

  • (Boolean)


290
291
292
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 290

def starts_year?(r)
  r[:start][:month] == 0 && starts_month?(r)
end

#stepped_year?Boolean

Returns:

  • (Boolean)


162
163
164
165
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 162

def stepped_year?
  @date.length == 1 && @date[0].wide_interval.type == 'year' &&
    !@date[0].wide_interval.step.nil?
end