opening_hours_converter 1.8.0 → 1.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,53 +9,45 @@ module OpeningHoursConverter
9
9
  @date = []
10
10
  @time = []
11
11
  @is_defined_off = false
12
- @comment = ""
12
+ @comment = ''
13
13
  end
14
14
 
15
15
  def get
16
- result = ""
16
+ result = ''
17
17
 
18
- if @date.length > 0
19
- result += get_wide_selector
20
- end
18
+ result += get_wide_selector if !@date.empty?
21
19
 
22
- if @date.length > 0
20
+ if !@date.empty?
23
21
  wd = @date[0].get_weekdays
24
- if wd.length > 0
25
- result += " #{wd}"
26
- end
22
+ result += " #{wd}" if !wd.empty?
27
23
  end
28
24
  if @is_defined_off
29
- result += " off"
30
- elsif @time.length > 0
31
- result += " "
25
+ result += ' off'
26
+ elsif !@time.empty?
27
+ result += ' '
32
28
  @time.uniq.each_with_index do |t, i|
33
- if (i > 0)
34
- result += ","
35
- end
29
+ result += ',' if i > 0
36
30
  result += t.get
37
31
  end
38
32
  else
39
- result += " off"
33
+ result += ' off'
40
34
  end
41
35
 
42
36
  rgx_day = /(Mo|Tu|We|Th|Fr|Sa|Su)/
43
37
 
44
- if result.strip == "00:00-24:00" || (!(result =~ /00:00-24:00/).nil? && (result =~ rgx_day).nil?)
45
- result.gsub!("00:00-24:00", "24/7")
38
+ if result.strip == '00:00-24:00' || (!(result =~ /00:00-24:00/).nil? && (result =~ rgx_day).nil?)
39
+ result.gsub!('00:00-24:00', '24/7')
46
40
  end
47
41
 
48
- if !comment.nil? && comment.length != 0
49
- result += " #{comment}"
50
- end
42
+ result += " #{comment}" if !comment.nil? && !comment.empty?
51
43
 
52
44
  result.strip
53
45
  end
54
46
 
55
47
  def get_wide_selector
56
- if @date.length == 1 && @date[0].wide_interval.type == "holiday"
48
+ if @date.length == 1 && @date[0].wide_interval.type == 'holiday'
57
49
  if @date[0].wide_interval.start[:year].nil?
58
- return "PH"
50
+ return 'PH'
59
51
  else
60
52
  if @date[0].wide_interval.end && @date[0].wide_interval.end[:year]
61
53
  return "#{@date[0].wide_interval.start[:year]}-#{@date[0].wide_interval.end[:year]} PH"
@@ -71,8 +63,8 @@ module OpeningHoursConverter
71
63
 
72
64
  result = {}
73
65
 
74
- if !years["always"].nil?
75
- always = years.delete("always")
66
+ if !years['always'].nil?
67
+ always = years.delete('always')
76
68
 
77
69
  always.each_with_index do |month_array, month|
78
70
  month_array.each_with_index do |day_bool, day|
@@ -81,17 +73,17 @@ module OpeningHoursConverter
81
73
  day_start = day
82
74
  end
83
75
  if day_bool && month_start >= 0 && month == 11 && day == 30
84
- result["always"] ||= []
85
- result["always"] << {start: { day: day_start, month: month_start }, end: { day: 30, month: 11 }}
76
+ result['always'] ||= []
77
+ result['always'] << { start: { day: day_start, month: month_start }, end: { day: 30, month: 11 } }
86
78
  month_start = -1
87
79
  day_start = -1
88
80
  elsif !day_bool && month_start >= 0
89
- result["always"] ||= []
81
+ result['always'] ||= []
90
82
  end_res = day == 0 ?
91
83
  month == 0 ?
92
- { day: 30, month: 11 } : { day: MONTH_END_DAY[month-1]-1, month: month-1 } :
93
- { day: day-1, month: month }
94
- result["always"] << { start: { day: day_start, month: month_start }, end: end_res }
84
+ { day: 30, month: 11 } : { day: MONTH_END_DAY[month - 1] - 1, month: month - 1 } :
85
+ { day: day - 1, month: month }
86
+ result['always'] << { start: { day: day_start, month: month_start }, end: end_res }
95
87
  month_start = -1
96
88
  day_start = -1
97
89
  end
@@ -107,15 +99,15 @@ module OpeningHoursConverter
107
99
  month_start = month
108
100
  day_start = day
109
101
  end
110
- if day_bool && year_start >= 0 && month == 11 && day == 30 && years[year+1].nil?
102
+ if day_bool && year_start >= 0 && month == 11 && day == 30 && years[year + 1].nil?
111
103
  if year_start == year
112
104
  result[year] ||= []
113
105
  result[year] << { start: { day: day_start, month: month_start }, end: { day: 30, month: 11 } }
114
106
  else
115
- result["multi_year"] ||= {}
116
- result["multi_year"]["#{year_start}-#{year}"] ||= []
117
- result["multi_year"]["#{year_start}-#{year}"] << { start: { day: day_start, month: month_start, year: year_start },
118
- end: { day: 30, month: 11, year: year } }
107
+ result['multi_year'] ||= {}
108
+ result['multi_year']["#{year_start}-#{year}"] ||= []
109
+ result['multi_year']["#{year_start}-#{year}"] << { start: { day: day_start, month: month_start, year: year_start },
110
+ end: { day: 30, month: 11, year: year } }
119
111
  end
120
112
  year_start = -1
121
113
  month_start = -1
@@ -123,25 +115,25 @@ module OpeningHoursConverter
123
115
  elsif !day_bool && year_start >= 0
124
116
  end_res = {}
125
117
 
126
- if day == 0
127
- if month == 0
128
- end_res = { day: 30, month: 11 }
129
- else
130
- end_res = { day: MONTH_END_DAY[month-1]-1, month: month-1 }
131
- end
132
- else
133
- end_res = { day: day-1, month: month }
134
- end
118
+ end_res = if day == 0
119
+ if month == 0
120
+ { day: 30, month: 11 }
121
+ else
122
+ { day: MONTH_END_DAY[month - 1] - 1, month: month - 1 }
123
+ end
124
+ else
125
+ { day: day - 1, month: month }
126
+ end
135
127
 
136
128
  if year_start == year
137
129
  result[year] ||= []
138
130
  result[year] << { start: { day: day_start, month: month_start }, end: end_res }
139
131
  else
140
- result["multi_year"] ||= {}
141
- result["multi_year"]["#{year_start}-#{year}"] ||= []
132
+ result['multi_year'] ||= {}
133
+ result['multi_year']["#{year_start}-#{year}"] ||= []
142
134
  end_res[:year] = year
143
- result["multi_year"]["#{year_start}-#{year}"] << { start: { day: day_start, month: month_start, year: year_start },
144
- end: end_res }
135
+ result['multi_year']["#{year_start}-#{year}"] << { start: { day: day_start, month: month_start, year: year_start },
136
+ end: end_res }
145
137
  end
146
138
  year_start = -1
147
139
  month_start = -1
@@ -155,40 +147,36 @@ module OpeningHoursConverter
155
147
  end
156
148
 
157
149
  def result_to_string(result)
158
- str_result = ""
150
+ str_result = ''
159
151
  result.each do |selector, intervals|
160
- if selector == "always"
152
+ if selector == 'always'
161
153
  intervals.each do |interval|
162
- if str_result.length > 0
163
- str_result += ","
164
- end
154
+ str_result += ',' if !str_result.empty?
165
155
  if is_full_year?(interval)
166
156
  elsif is_full_month?(interval)
167
- str_result += "#{OSM_MONTHS[interval[:start][:month]]}"
157
+ str_result += (OSM_MONTHS[interval[:start][:month]]).to_s
168
158
  elsif starts_month?(interval) && ends_month?(interval)
169
159
  str_result += "#{OSM_MONTHS[interval[:start][:month]]}-#{OSM_MONTHS[interval[:end][:month]]}"
170
160
  elsif is_same_month?(interval)
171
161
  if is_same_day?(interval)
172
- str_result += "#{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day]+1 < 10 ? "0#{interval[:start][:day]+1}" : interval[:start][:day]+1}"
162
+ str_result += "#{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day] + 1 < 10 ? "0#{interval[:start][:day] + 1}" : interval[:start][:day] + 1}"
173
163
  else
174
- 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}"
164
+ 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}"
175
165
  end
176
166
  else
177
- 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}"
167
+ 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}"
178
168
  end
179
169
  end
180
- elsif selector == "multi_year"
181
- intervals.each do |years, intervals|
170
+ elsif selector == 'multi_year'
171
+ intervals.each do |_years, intervals|
182
172
  intervals.each do |interval|
183
- if str_result.length > 0
184
- str_result += ","
185
- end
173
+ str_result += ',' if !str_result.empty?
186
174
  if starts_year?(interval) && ends_year?(interval)
187
175
  str_result += "#{interval[:start][:year]}-#{interval[:end][:year]}"
188
176
  # elsif starts_month?(interval) && ends_month?(interval)
189
177
  # str_result += "#{interval[:start][:year]} #{OSM_MONTHS[interval[:start][:month]]}-#{interval[:end][:year]} #{OSM_MONTHS[interval[:end][:month]]}"
190
178
  else
191
- 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}"
179
+ 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}"
192
180
  end
193
181
  end
194
182
  end
@@ -196,21 +184,21 @@ module OpeningHoursConverter
196
184
  local_str = "#{selector} "
197
185
  intervals.each do |interval|
198
186
  if is_full_year?(interval)
199
- # elsif
187
+ # elsif
200
188
  # local_str += "#{local_str.length > 5 ? "," : ""}#{OSM_MONTHS[interval[:start][:month]]}"
201
- # elsif
189
+ # elsif
202
190
  # local_str += "#{local_str.length > 5 ? "," : ""}#{OSM_MONTHS[interval[:start][:month]]}-#{OSM_MONTHS[interval[:end][:month]]}"
203
191
  elsif is_same_month?(interval)
204
192
  if is_same_day?(interval)
205
- 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}"
193
+ 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}"
206
194
  else
207
- 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}"
195
+ 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}"
208
196
  end
209
197
  else
210
- 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}"
198
+ 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}"
211
199
  end
212
200
  end
213
- str_result += "#{str_result.length > 0 ? "," : ""}#{local_str}"
201
+ str_result += "#{!str_result.empty? ? ',' : ''}#{local_str}"
214
202
  end
215
203
  end
216
204
 
@@ -255,23 +243,23 @@ module OpeningHoursConverter
255
243
 
256
244
  def same_time?(o)
257
245
  if o.nil? || o.time.length != @time.length || @is_defined_off != o.is_defined_off
258
- return false
246
+ false
259
247
  else
260
248
  @time.each_with_index do |t, i|
261
249
  return false if !t.equals(o.time[i])
262
250
  end
263
- return true
251
+ true
264
252
  end
265
253
  end
266
254
 
267
255
  def same_date?(o)
268
256
  if o.nil? || o.date.length != @date.length
269
- return false
257
+ false
270
258
  else
271
259
  @date.each_with_index do |d, i|
272
260
  return false if !d.wide_interval.equals(o.date[i].wide_interval)
273
261
  end
274
- return true
262
+ true
275
263
  end
276
264
  end
277
265
 
@@ -281,11 +269,11 @@ module OpeningHoursConverter
281
269
  end
282
270
 
283
271
  def is_off?
284
- @time.length == 0 || (@time.length == 1 && time[0].start.nil?)
272
+ @time.empty? || (@time.length == 1 && time[0].start.nil?)
285
273
  end
286
274
 
287
275
  def has_overwritten_weekday?
288
- @date.length > 0 && @date[0].weekdays_over.length > 0
276
+ !@date.empty? && !@date[0].weekdays_over.empty?
289
277
  end
290
278
 
291
279
  def add_weekday(weekday)
@@ -295,9 +283,7 @@ module OpeningHoursConverter
295
283
  end
296
284
 
297
285
  def add_ph_weekday
298
- @date.each do |d|
299
- d.add_ph_weekday
300
- end
286
+ @date.each(&:add_ph_weekday)
301
287
  end
302
288
 
303
289
  def add_overwritten_weekday(weekday)
@@ -311,11 +297,11 @@ module OpeningHoursConverter
311
297
  raise ArgumentError
312
298
  end
313
299
 
314
- if @date.length == 0 || date.same_weekdays?(@date.first.weekdays)
300
+ if @date.empty? || date.same_weekdays?(@date.first.weekdays)
315
301
  @date << date
316
302
  else
317
303
  if !@date.first.same_weekdays?(date.weekdays)
318
- raise ArgumentError, "This date #{@date.inspect} can't be added to this rule #{self.inspect}"
304
+ raise ArgumentError, "This date #{@date.inspect} can't be added to this rule #{inspect}"
319
305
  end
320
306
  end
321
307
  end
@@ -324,13 +310,11 @@ module OpeningHoursConverter
324
310
  @time.each do |t|
325
311
  return true if t.start == time.start && t.end == time.end
326
312
  end
327
- return false
313
+ false
328
314
  end
329
315
 
330
316
  def add_time(time)
331
- if (@time.length == 0 || @time[0].get != "off") && !include_time?(time)
332
- @time << time
333
- end
317
+ @time << time if (@time.empty? || @time[0].get != 'off') && !include_time?(time)
334
318
  end
335
319
 
336
320
  def add_comment(comment)
@@ -2,16 +2,14 @@ module OpeningHoursConverter
2
2
  class OpeningHoursTime
3
3
  attr_reader :start, :end, :priority
4
4
 
5
- def initialize(minute_start=nil, minute_end=nil)
5
+ def initialize(minute_start = nil, minute_end = nil)
6
6
  @start = minute_start
7
- unless minute_start == minute_end
8
- @end = minute_end
9
- end
7
+ @end = minute_end unless minute_start == minute_end
10
8
  end
11
9
 
12
10
  def get
13
- return "off" if (@start.nil? && @end.nil?)
14
- "#{time_string(@start)}#{@end.nil? ? "" : "-#{time_string(@end)}"}"
11
+ return 'off' if @start.nil? && @end.nil?
12
+ "#{time_string(@start)}#{@end.nil? ? '' : "-#{time_string(@end)}"}"
15
13
  end
16
14
 
17
15
  def equals(t)
@@ -20,9 +18,9 @@ module OpeningHoursConverter
20
18
 
21
19
  def time_string(minutes)
22
20
  fminutes = minutes.to_f
23
- h = (fminutes/60).floor.to_i
24
- m = (fminutes%60).to_i
25
- "#{h < 10 ? "0" : ""}#{h}:#{m < 10 ? "0" : ""}#{m}"
21
+ h = (fminutes / 60).floor.to_i
22
+ m = (fminutes % 60).to_i
23
+ "#{h < 10 ? '0' : ''}#{h}:#{m < 10 ? '0' : ''}#{m}"
26
24
  end
27
25
  end
28
26
  end
@@ -1,10 +1,10 @@
1
1
  module OpeningHoursConverter
2
2
  class PublicHoliday
3
- def self.easter(year=Time.now.year)
3
+ def self.easter(year = Time.now.year)
4
4
  # code from https://github.com/jrobertson/easter
5
5
  golden_number = (year % 19) + 1
6
6
 
7
- if year <= 1752 then
7
+ if year <= 1752
8
8
  # Julian calendar
9
9
  dominical_number = (year + (year / 4) + 5) % 7
10
10
  paschal_full_moon = (3 - (11 * golden_number) - 7) % 30
@@ -19,14 +19,14 @@ module OpeningHoursConverter
19
19
  dominical_number += 7 until dominical_number > 0
20
20
 
21
21
  paschal_full_moon += 30 until paschal_full_moon > 0
22
- paschal_full_moon -= 1 if paschal_full_moon == 29 or (paschal_full_moon == 28 and golden_number > 11)
22
+ paschal_full_moon -= 1 if (paschal_full_moon == 29) || ((paschal_full_moon == 28) && (golden_number > 11))
23
23
 
24
24
  difference = (4 - paschal_full_moon - dominical_number) % 7
25
25
  difference += 7 if difference < 0
26
26
 
27
27
  day_easter = paschal_full_moon + difference + 1
28
28
 
29
- if day_easter < 11 then
29
+ if day_easter < 11
30
30
  # Easter occurs in March.
31
31
  return Time.new(year, 3, day_easter + 21)
32
32
  else
@@ -35,59 +35,59 @@ module OpeningHoursConverter
35
35
  end
36
36
  end
37
37
 
38
- def self.new_year(year=Time.now.year)
38
+ def self.new_year(year = Time.now.year)
39
39
  Time.new(year, 1, 1)
40
40
  end
41
41
 
42
- def self.christmas(year=Time.now.year)
42
+ def self.christmas(year = Time.now.year)
43
43
  Time.new(year, 12, 25)
44
44
  end
45
45
 
46
- def self.assomption(year=Time.now.year)
46
+ def self.assomption(year = Time.now.year)
47
47
  Time.new(year, 8, 15)
48
48
  end
49
49
 
50
- def self.work_day(year=Time.now.year)
50
+ def self.work_day(year = Time.now.year)
51
51
  Time.new(year, 5, 1)
52
52
  end
53
53
 
54
- def self.toussaint(year=Time.now.year)
54
+ def self.toussaint(year = Time.now.year)
55
55
  Time.new(year, 11, 1)
56
56
  end
57
57
 
58
- def self.victory(year=Time.now.year)
58
+ def self.victory(year = Time.now.year)
59
59
  Time.new(year, 5, 8)
60
60
  end
61
61
 
62
- def self.armistice(year=Time.now.year)
62
+ def self.armistice(year = Time.now.year)
63
63
  Time.new(year, 11, 11)
64
64
  end
65
65
 
66
- def self.bastille_day(year=Time.now.year)
66
+ def self.bastille_day(year = Time.now.year)
67
67
  Time.new(year, 7, 14)
68
68
  end
69
69
 
70
- def self.easter_monday(year=Time.now.year)
70
+ def self.easter_monday(year = Time.now.year)
71
71
  easter(year) + days(1)
72
72
  end
73
73
 
74
- def self.good_friday(year=Time.now.year)
74
+ def self.good_friday(year = Time.now.year)
75
75
  easter(year) - days(2)
76
76
  end
77
77
 
78
- def self.rise(year=Time.now.year)
78
+ def self.rise(year = Time.now.year)
79
79
  easter(year) + days(39)
80
80
  end
81
81
 
82
- def self.pentecote(year=Time.now.year)
82
+ def self.pentecote(year = Time.now.year)
83
83
  easter(year) + days(49)
84
84
  end
85
85
 
86
- def self.pentecote_monday(year=Time.now.year)
86
+ def self.pentecote_monday(year = Time.now.year)
87
87
  easter(year) + days(50)
88
88
  end
89
89
 
90
- def self.ph_for_year(year=Time.now.year)
90
+ def self.ph_for_year(year = Time.now.year)
91
91
  ph = []
92
92
  ph << new_year(year)
93
93
  ph << good_friday(year)
@@ -106,7 +106,7 @@ module OpeningHoursConverter
106
106
  ph.sort
107
107
  end
108
108
 
109
- def self.days(days=0)
109
+ def self.days(days = 0)
110
110
  days * 24 * 60 * 60
111
111
  end
112
112
  end