Class: OpeningHoursConverter::RegexHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/opening_hours_converter/regex_handler.rb

Instance Method Summary collapse

Instance Method Details

#commaObject



248
249
250
# File 'lib/opening_hours_converter/regex_handler.rb', line 248

def comma
  ',' + space + '?'
end

#commentObject



302
303
304
# File 'lib/opening_hours_converter/regex_handler.rb', line 302

def comment
  '\"[^\"]*\"'
end

#comment_regexObject



201
202
203
# File 'lib/opening_hours_converter/regex_handler.rb', line 201

def comment_regex
  compile(line(comment))
end

#compile(string) ⇒ Object



318
319
320
321
# File 'lib/opening_hours_converter/regex_handler.rb', line 318

def compile(string)
  @compile ||= {}
  @compile[string] ||= Regexp.compile(string, Regexp::IGNORECASE)
end

#day_regexObject



114
115
116
117
118
119
120
# File 'lib/opening_hours_converter/regex_handler.rb', line 114

def day_regex
  compile(
    line(
      potential_range(month_day)
    )
  )
end

#end_of_lineObject



310
311
312
# File 'lib/opening_hours_converter/regex_handler.rb', line 310

def end_of_line
  '$'
end

#escape(string) ⇒ Object



323
324
325
326
# File 'lib/opening_hours_converter/regex_handler.rb', line 323

def escape(string)
  @escape ||= {}
  @escape[string] ||= Regexp.escape(string)
end

#full_timeObject



217
218
219
# File 'lib/opening_hours_converter/regex_handler.rb', line 217

def full_time
  '24/7'
end

#group(*args) ⇒ Object



209
210
211
# File 'lib/opening_hours_converter/regex_handler.rb', line 209

def group(*args)
  "(#{args.join})"
end

#holiday_regexObject



58
59
60
# File 'lib/opening_hours_converter/regex_handler.rb', line 58

def holiday_regex
  compile(line(ph))
end

#int_range(max) ⇒ Object

Raises:

  • (ArgumentError)


221
222
223
224
225
226
227
228
229
230
# File 'lib/opening_hours_converter/regex_handler.rb', line 221

def int_range(max)
  raise ArgumentError, 'too high' if max > 99

  base = max / 10

  unit_max = max - base * 10
  base_ten = "[0-#{base - 1}]?"
  base_unit = '[0-9]'
  group(group("#{base_ten}#{base_unit}") + '|' + group("#{base}[0-#{unit_max}]"))
end

#line(pattern) ⇒ Object



314
315
316
# File 'lib/opening_hours_converter/regex_handler.rb', line 314

def line(pattern)
  start_of_line + pattern + end_of_line
end

#modifierObject



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

def modifier
  'off'
end

#monthObject



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

def month
  group('Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec')
end

#month_dayObject



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

def month_day
  group('[012]?[0-9]|3[01]')
end

#month_day_regexObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/opening_hours_converter/regex_handler.rb', line 43

def month_day_regex
  compile(
    line(
      potential_range(
        month + space + month_day,
        potential(month + space) + month_day
      )
    )
  )
end

#month_regexObject



54
55
56
# File 'lib/opening_hours_converter/regex_handler.rb', line 54

def month_regex
  compile(line(potential_range(month)))
end

#multi_month_regexObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/opening_hours_converter/regex_handler.rb', line 180

def multi_month_regex
  compile(
    line(
      potential_list(
        potential_range(
          month +
          potential(
            group(
              space, potential_range(month_day)
            )
          )
        )
      )
    )
  )
end

#phObject



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

def ph
  'PH'
end

#potential(pattern) ⇒ Object



232
233
234
# File 'lib/opening_hours_converter/regex_handler.rb', line 232

def potential(pattern)
  group(pattern) + '?'
end

#potential_commaObject



252
253
254
# File 'lib/opening_hours_converter/regex_handler.rb', line 252

def potential_comma
  ',? ?'
end

#potential_list(pattern, optional_pattern = pattern) ⇒ Object



240
241
242
# File 'lib/opening_hours_converter/regex_handler.rb', line 240

def potential_list(pattern, optional_pattern = pattern)
  group(pattern, group(group(comma, optional_pattern), '?'), '*')
end

#potential_range(pattern, optional_pattern = pattern) ⇒ Object



236
237
238
# File 'lib/opening_hours_converter/regex_handler.rb', line 236

def potential_range(pattern, optional_pattern = pattern)
  group(pattern, group('-', optional_pattern), '?')
end

#rule_modifier_regexObject



5
6
7
# File 'lib/opening_hours_converter/regex_handler.rb', line 5

def rule_modifier_regex
  /^(open|closed|off)$/i
end

#spaceObject



256
257
258
# File 'lib/opening_hours_converter/regex_handler.rb', line 256

def space
  ' '
end

#start_of_lineObject



306
307
308
# File 'lib/opening_hours_converter/regex_handler.rb', line 306

def start_of_line
  '^'
end

#timeObject



213
214
215
# File 'lib/opening_hours_converter/regex_handler.rb', line 213

def time
  group(int_range(24)) + ':' + group(int_range(59))
end

#time_regexObject



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/opening_hours_converter/regex_handler.rb', line 62

def time_regex
  compile(
    line(
      potential_list(
        group(
          potential_range(
            group(time)
          ) + '|' + group(full_time)
        )
      )
    )
  )
end

#weekObject



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

def week
  'week'
end

#week_dayObject



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

def week_day
  group('Mo|Tu|We|Th|Fr|Sa|Su')
end

#week_day_modifierObject



244
245
246
# File 'lib/opening_hours_converter/regex_handler.rb', line 244

def week_day_modifier
  '\\[' + group('[1-5]|\\-1') + '\\]'
end

#week_day_or_holiday_regexObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/opening_hours_converter/regex_handler.rb', line 76

def week_day_or_holiday_regex
  compile(
    line(
      potential_list(
        group(
          potential_range(
            group(week_day)
          ) + '|' + group(ph)
        )
      )
    )
  )
end

#week_day_regexObject



90
91
92
93
94
95
96
# File 'lib/opening_hours_converter/regex_handler.rb', line 90

def week_day_regex
  compile(
    line(
      potential_range(week_day)
    )
  )
end

#week_day_with_modifier_regexObject



98
99
100
101
102
103
104
# File 'lib/opening_hours_converter/regex_handler.rb', line 98

def week_day_with_modifier_regex
  compile(
    line(
      week_day + week_day_modifier
    )
  )
end

#week_key_regexObject



9
10
11
# File 'lib/opening_hours_converter/regex_handler.rb', line 9

def week_key_regex
  compile(line(week))
end

#week_modifierObject



264
265
266
# File 'lib/opening_hours_converter/regex_handler.rb', line 264

def week_modifier
  '\/[1-9]'
end

#week_numberObject



260
261
262
# File 'lib/opening_hours_converter/regex_handler.rb', line 260

def week_number
  group('[01234]?[0-9]|5[0123]')
end

#week_regexObject



13
14
15
16
17
18
19
20
21
# File 'lib/opening_hours_converter/regex_handler.rb', line 13

def week_regex
  compile(
    line(
      week + space + potential_list(
        potential_range(int_range(53))
      )
    )
  )
end

#week_value_regexObject



197
198
199
# File 'lib/opening_hours_converter/regex_handler.rb', line 197

def week_value_regex
  compile(line(potential_list(potential_range(int_range(53)))))
end

#week_value_with_modifier_regexObject



33
34
35
36
37
38
39
40
41
# File 'lib/opening_hours_converter/regex_handler.rb', line 33

def week_value_with_modifier_regex
  compile(
    line(
      potential_list(
        potential_range(int_range(53), group(int_range(53) + potential(week_modifier)))
      )
    )
  )
end

#week_with_modifierObject



268
269
270
271
272
# File 'lib/opening_hours_converter/regex_handler.rb', line 268

def week_with_modifier
  group(
    potential_range(week_number, week_number + potential(week_modifier))
  )
end

#week_with_modifier_regexObject



23
24
25
26
27
28
29
30
31
# File 'lib/opening_hours_converter/regex_handler.rb', line 23

def week_with_modifier_regex
  compile(
    line(
      week + space + potential_list(
        potential_range(int_range(53), group(int_range(53) + potential(week_modifier)))
      )
    )
  )
end

#yearObject



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

def year
  '\\d{4}'
end

#year_month_day_regexObject



150
151
152
153
154
155
156
157
158
159
# File 'lib/opening_hours_converter/regex_handler.rb', line 150

def year_month_day_regex
  compile(
    line(
      potential_range(
        year + space + month + space + month_day,
        potential(year + space) + potential(month + space) + month_day
      )
    )
  )
end

#year_month_regexObject



205
206
207
# File 'lib/opening_hours_converter/regex_handler.rb', line 205

def year_month_regex
  compile(line(potential_range(year + space + month, potential(year + space) + month)))
end

#year_multi_month_day_regexObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/opening_hours_converter/regex_handler.rb', line 161

def year_multi_month_day_regex
  compile(
    line(
      year + space +
      potential_list(
        potential(year + space) +
        month +
        group(
          space, potential_range(month_day)
        )
      )
    )
  )
end

#year_multi_month_regexObject



176
177
178
# File 'lib/opening_hours_converter/regex_handler.rb', line 176

def year_multi_month_regex
  compile(line(year + space + group(potential_range(month), potential_comma) + '*'))
end

#year_ph_regexObject



122
123
124
125
126
127
128
# File 'lib/opening_hours_converter/regex_handler.rb', line 122

def year_ph_regex
  compile(
    line(
      year + group(space + ph + '|' + '-' + year + space + ph)
    )
  )
end

#year_regexObject



106
107
108
109
110
111
112
# File 'lib/opening_hours_converter/regex_handler.rb', line 106

def year_regex
  compile(
    line(
      potential_range(year)
    )
  )
end

#year_week_regexObject



130
131
132
133
134
135
136
137
138
139
# File 'lib/opening_hours_converter/regex_handler.rb', line 130

def year_week_regex
  compile(
    line(
      potential_range(year) + space + week + space +
      group(
        potential_list(potential_range(week_number))
      )
    )
  )
end

#year_week_with_modifier_regexObject



141
142
143
144
145
146
147
148
# File 'lib/opening_hours_converter/regex_handler.rb', line 141

def year_week_with_modifier_regex
  compile(
    line(
      potential_range(year) + space + week + space +
      potential_list(week_with_modifier)
    )
  )
end