opening_hours_converter 1.7.20 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f84dd0e31e985cbb8d0e31d6b2da2dc54c4667555daf22277e31fa26fe72e3c
4
- data.tar.gz: 0aa24ec1f8b071578f592bd330725d8c18e27fa4a2869af82e75f41f332c3c49
3
+ metadata.gz: 50b769d524ea032bea248de2b6dded3bfd944f3f3402f4213ef160075f4c68e6
4
+ data.tar.gz: 64ca508b6a8d07c9354e22c3371a1a51d77452eba94d2d5826f133ce26591e37
5
5
  SHA512:
6
- metadata.gz: 5b029e0e307dad79447d7834a674d0555b5b8a9690aa23ca9b4d1763ef4d1379184a822d97051a15963eb97c8d3a8fef9955de708dad2bfeb0d42951c143ec85
7
- data.tar.gz: 27a803a86bf8d3affc16ae04c554b837038c0b03c96cc68b1a37bc2808450f14c5e65bf90c8a858e009134973f3df45b16bb58defc595c3d01b0e8c1ba0ca1d2
6
+ metadata.gz: 2a86e27cd5699f95e1bd25559b22df74255f3efffbf9b7b5b4a1227a4a95bb80bf69f0249c9a76a283b9ae05697cb33aceafc47eec63477ffa95c8544274790e
7
+ data.tar.gz: 320f7a39295e8a9c29cf33f002e3cf03436a9b91d027320af3ce96114f8dbf9899a08b30b2e95fd2c6d423d6f1a68d8c516c5aa07bda2c58155b9323728415e2
@@ -2,11 +2,11 @@ module OpeningHoursConverter
2
2
  class DateRange
3
3
  attr_accessor :wide_interval, :typical, :comment
4
4
 
5
- def initialize(w=nil)
5
+ def initialize(wide_interval = nil)
6
6
  @wide_interval = nil
7
7
  @typical = nil
8
- @comment = ""
9
- update_range(w)
8
+ @comment = ''
9
+ update_range(wide_interval)
10
10
  end
11
11
 
12
12
  def defines_typical_day?
@@ -17,24 +17,24 @@ module OpeningHoursConverter
17
17
  @typical.instance_of?(OpeningHoursConverter::Week)
18
18
  end
19
19
 
20
- def update_range(wide)
21
- @wide_interval = !wide.nil? ? wide : OpeningHoursConverter::WideInterval.new.always
20
+ def update_range(wide_interval)
21
+ @wide_interval = !wide_interval.nil? ? wide_interval : OpeningHoursConverter::WideInterval.new.always
22
22
 
23
- if @typical.nil?
24
- case @wide_interval.type
25
- when "day"
26
- if @wide_interval.end.nil?
27
- @typical = OpeningHoursConverter::Day.new
28
- else
29
- @typical = OpeningHoursConverter::Week.new
30
- end
31
- else
32
- @typical = OpeningHoursConverter::Week.new
33
- end
34
- end
23
+ return unless @typical.nil?
24
+
25
+ @typical = case @wide_interval.type
26
+ when 'day'
27
+ if @wide_interval.end.nil?
28
+ OpeningHoursConverter::Day.new
29
+ else
30
+ OpeningHoursConverter::Week.new
31
+ end
32
+ else
33
+ OpeningHoursConverter::Week.new
34
+ end
35
35
  end
36
36
 
37
- def add_comment(comment="")
37
+ def add_comment(comment = '')
38
38
  @comment += comment if comment
39
39
  end
40
40
 
@@ -47,8 +47,7 @@ module OpeningHoursConverter
47
47
  end
48
48
 
49
49
  def is_holiday?
50
- result = false
51
- result = @wide_interval.type == "holiday"
50
+ result = @wide_interval.type == 'holiday'
52
51
  if !result
53
52
  @typical.intervals.each do |i|
54
53
  if !i.nil?
@@ -13,60 +13,46 @@ module OpeningHoursConverter
13
13
  minute_array = Array.new(MINUTES_MAX + 1, false)
14
14
 
15
15
  @intervals.each do |interval|
16
- if !interval.nil?
17
- start_minute = nil
18
- end_minute = nil
19
- off = interval.is_off
20
-
21
- if off
22
- start_minute = 0
23
- end_minute = MINUTES_MAX
24
- elsif interval.day_start == interval.day_end || interval.day_end == DAYS_MAX && interval.end == MINUTES_MAX
25
- start_minute = interval.start
26
- end_minute = interval.end
27
- elsif interval.day_end == interval.day_start + 1 && interval.end == 0
28
- start_minute = interval.start
29
- end_minute = MINUTES_MAX
30
- end
31
-
32
- unless start_minute.nil? && end_minute.nil?
33
- for minute in start_minute..end_minute
34
- minute_array[minute] = off ? "off" : true
35
- end
36
- else
37
- raise "Invalid interval #{interval.inspect}"
38
- end
16
+ next if interval.nil?
17
+
18
+ off, start_minute, end_minute = handle_interval(interval)
19
+
20
+ raise "Invalid interval #{interval.inspect}" if start_minute.nil? && end_minute.nil?
21
+
22
+ (start_minute..end_minute).step do |minute|
23
+ minute_array[minute] = off ? 'off' : true
39
24
  end
40
25
  end
41
26
 
42
27
  minute_array
43
28
  end
44
29
 
45
- def get_intervals(clean=false)
30
+ def handle_interval(interval)
31
+ off = interval.is_off
32
+
33
+ if off
34
+ start_minute = 0
35
+ end_minute = MINUTES_MAX
36
+ elsif interval.single_day? || interval.max?
37
+ start_minute = interval.start
38
+ end_minute = interval.end
39
+ elsif interval.single_day_end_at_midnight?
40
+ start_minute = interval.start
41
+ end_minute = MINUTES_MAX
42
+ end
43
+
44
+ [off, start_minute, end_minute]
45
+ end
46
+
47
+ def get_intervals(clean = false)
46
48
  if clean
47
49
  minute_array = get_as_minute_array
48
50
  intervals = []
49
51
  minute_start = -1
50
- minute_end = nil
51
52
  off = false
52
53
 
53
54
  minute_array.each_with_index do |minute, i|
54
- if i == 0 && minute
55
- off = true if minute == "off"
56
- minute_start = i
57
- elsif i == minute_array.length - 1 && minute
58
- intervals << OpeningHoursConverter::Interval.new(0, minute_start, 0, i - 1, off)
59
- minute_start = -1
60
- off = false
61
- else
62
- if minute && minute_start < 0
63
- minute_start = i
64
- elsif !minute && minute_start >= 0
65
- intervals << OpeningHoursConverter::Interval.new(0, minute_start, 0, i - 1, off)
66
- minute_start = -1
67
- off = false
68
- end
69
- end
55
+ off, minute_start, intervals = handle_minute(minute, off, minute_start, intervals, i, minute_array)
70
56
  end
71
57
  intervals
72
58
  else
@@ -74,9 +60,29 @@ module OpeningHoursConverter
74
60
  end
75
61
  end
76
62
 
63
+ def handle_minute(minute, off, minute_start, intervals, i, minute_array)
64
+ if minute
65
+ if i == 0
66
+ off = true if minute == 'off'
67
+ minute_start = i
68
+ elsif minute_start < 0
69
+ minute_start = i
70
+ elsif i == minute_array.length - 1
71
+ intervals << OpeningHoursConverter::Interval.new(0, minute_start, 0, i - 1, off)
72
+ minute_start = -1
73
+ off = false
74
+ end
75
+ elsif minute_start >= 0
76
+ intervals << OpeningHoursConverter::Interval.new(0, minute_start, 0, i - 1, off)
77
+ minute_start = -1
78
+ off = false
79
+ end
80
+ [off, minute_start, intervals]
81
+ end
82
+
77
83
  def add_interval(interval)
78
84
  @intervals << interval
79
- return @intervals.length - 1
85
+ @intervals.length - 1
80
86
  end
81
87
 
82
88
  def edit_interval(id, interval)
@@ -6,7 +6,6 @@ module OpeningHoursConverter
6
6
  attr_reader :day_start, :day_end, :start, :end, :is_off
7
7
 
8
8
  def initialize(day_start, min_start, day_end=0, min_end=0, is_off=false)
9
-
10
9
  @day_start = day_start
11
10
  @day_end = day_end
12
11
  @start = min_start
@@ -18,5 +17,17 @@ module OpeningHoursConverter
18
17
  @end = MINUTES_MAX
19
18
  end
20
19
  end
20
+
21
+ def single_day?
22
+ @day_start == @day_end
23
+ end
24
+
25
+ def max?
26
+ @day_end == DAYS_MAX && @end == MINUTES_MAX
27
+ end
28
+
29
+ def single_day_end_at_midnight?
30
+ @day_end == @day_start + 1 && @end == 0
31
+ end
21
32
  end
22
33
  end
@@ -77,10 +77,10 @@ module OpeningHoursConverter
77
77
  oh_rule_added = true
78
78
  rescue Exception => e
79
79
  puts e
80
- if oh_rule.date[0].wide_type == "holiday" && oh_rule.date[0].wide.get_time_selector == "PH"
80
+ if oh_rule.date[0].wide_interval.type == "holiday" && oh_rule.date[0].wide_interval.get_time_selector == "PH"
81
81
  rules[rule_index].add_ph_weekday
82
82
  oh_rule_added = true
83
- elsif rules[rule_index].date[0].wide_type == "holiday" && rules[rule_index].date[0].wide.get_time_selector == "PH"
83
+ elsif rules[rule_index].date[0].wide_interval.type == "holiday" && rules[rule_index].date[0].wide_interval.get_time_selector == "PH"
84
84
  oh_rule.add_ph_weekday
85
85
  rules[rule_index] = oh_rule
86
86
  oh_rule_added = true
@@ -110,7 +110,7 @@ module OpeningHoursConverter
110
110
  oh_rule_over = OpeningHoursConverter::OpeningHoursRule.new
111
111
 
112
112
  oh_rule.date.each do |date|
113
- oh_rule_over.add_date(OpeningHoursConverter::OpeningHoursDate.new(date.wide, date.wide_type, date.weekdays_over))
113
+ oh_rule_over.add_date(OpeningHoursConverter::OpeningHoursDate.new(date.wide_interval, date.weekdays_over))
114
114
  end
115
115
  oh_rule_over.add_time(OpeningHoursConverter::OpeningHoursTime.new)
116
116
  oh_rules << oh_rule_over
@@ -141,7 +141,7 @@ module OpeningHoursConverter
141
141
  date_range = OpeningHoursConverter::DateRange.new(OpeningHoursConverter::WideInterval.new.holiday("PH", start_year, end_year))
142
142
 
143
143
  rule = OpeningHoursConverter::OpeningHoursRule.new
144
- date = OpeningHoursConverter::OpeningHoursDate.new(date_range.wide_interval, date_range.wide_interval.type, [-1])
144
+ date = OpeningHoursConverter::OpeningHoursDate.new(date_range.wide_interval, [-1])
145
145
  rule.add_date(date)
146
146
  rule.is_defined_off = true
147
147
 
@@ -165,7 +165,7 @@ module OpeningHoursConverter
165
165
  end
166
166
  end
167
167
  rule = OpeningHoursConverter::OpeningHoursRule.new
168
- date = OpeningHoursConverter::OpeningHoursDate.new(date_range.wide_interval, date_range.wide_interval.type, [-1])
168
+ date = OpeningHoursConverter::OpeningHoursDate.new(date_range.wide_interval, [-1])
169
169
  rule.add_date(date)
170
170
 
171
171
  date_range.typical.intervals.each do |interval|
@@ -183,7 +183,7 @@ module OpeningHoursConverter
183
183
  intervals = date_range.typical.get_intervals(true)
184
184
 
185
185
  rule = OpeningHoursConverter::OpeningHoursRule.new
186
- date = OpeningHoursConverter::OpeningHoursDate.new(date_range.wide_interval, date_range.wide_interval.type, [-1])
186
+ date = OpeningHoursConverter::OpeningHoursDate.new(date_range.wide_interval, [-1])
187
187
  rule.add_date(date)
188
188
 
189
189
  intervals.each do |interval|
@@ -200,7 +200,7 @@ module OpeningHoursConverter
200
200
  result = []
201
201
 
202
202
  intervals = date_range.typical.get_intervals(true)
203
- days = create_time_intervals(date_range.wide_interval, date_range.wide_interval.type, intervals)
203
+ days = create_time_intervals(date_range.wide_interval, intervals)
204
204
 
205
205
  days_status = Array.new(OSM_DAYS.length, 0)
206
206
 
@@ -259,7 +259,6 @@ module OpeningHoursConverter
259
259
  intervals = date_range.typical.get_intervals_diff(general_date_range.typical)
260
260
  days = create_time_intervals(
261
261
  date_range.wide_interval,
262
- date_range.wide_interval.type,
263
262
  intervals[:open])
264
263
 
265
264
 
@@ -359,11 +358,11 @@ module OpeningHoursConverter
359
358
  return result
360
359
  end
361
360
 
362
- def create_time_intervals(wide_interval, type, intervals)
361
+ def create_time_intervals(wide_interval, intervals)
363
362
  days = []
364
363
  for i in 0...7
365
364
  days << OpeningHoursConverter::OpeningHoursRule.new
366
- days[i].add_date(OpeningHoursConverter::OpeningHoursDate.new(wide_interval, type, [ i ]))
365
+ days[i].add_date(OpeningHoursConverter::OpeningHoursDate.new(wide_interval, [ i ]))
367
366
  end
368
367
 
369
368
  intervals.each do |interval|
@@ -4,15 +4,14 @@ module OpeningHoursConverter
4
4
  class OpeningHoursDate
5
5
  include Constants
6
6
  attr_accessor :weekdays, :weekdays_over
7
- attr_reader :wide_type, :wide
7
+ attr_reader :wide_interval
8
8
 
9
- def initialize(wide, wide_type, weekdays)
10
- if wide.nil? || wide_type.nil? || weekdays.nil?
9
+ def initialize(wide_interval, weekdays)
10
+ if wide_interval.nil? || weekdays.nil? || !wide_interval.is_a?(OpeningHoursConverter::WideInterval)
11
11
  raise ArgumentError
12
12
  end
13
13
 
14
- @wide = wide
15
- @wide_type = wide_type
14
+ @wide_interval = wide_interval
16
15
  @weekdays = weekdays.sort
17
16
  @weekdays_over = []
18
17
  end
@@ -122,7 +121,7 @@ module OpeningHoursConverter
122
121
  end
123
122
 
124
123
  def same_kind_as?(date)
125
- @wide_type == date.wide_type && date.same_weekdays?(@weekdays)
124
+ @wide_interval.type == date.wide_interval.type && date.same_weekdays?(@weekdays)
126
125
  end
127
126
 
128
127
  def same_weekdays?(weekdays)
@@ -130,7 +129,7 @@ module OpeningHoursConverter
130
129
  end
131
130
 
132
131
  def equals(o)
133
- o.instance_of?(OpeningHoursConverter::OpeningHoursDate) && @wide_type == o.wide_type && @wide == o.wide && o.same_weekdays?(@weekdays)
132
+ o.instance_of?(OpeningHoursConverter::OpeningHoursDate) && @wide_interval.type == o.wide_interval.type && @wide_interval.equals(o.wide_interval) && o.same_weekdays?(@weekdays)
134
133
  end
135
134
  end
136
135
  end
@@ -53,14 +53,14 @@ module OpeningHoursConverter
53
53
  end
54
54
 
55
55
  def get_wide_selector
56
- if @date.length == 1 && @date[0].wide.type == "holiday"
57
- if @date[0].wide.start[:year].nil?
56
+ if @date.length == 1 && @date[0].wide_interval.type == "holiday"
57
+ if @date[0].wide_interval.start[:year].nil?
58
58
  return "PH"
59
59
  else
60
- if @date[0].wide.end && @date[0].wide.end[:year]
61
- return "#{@date[0].wide.start[:year]}-#{@date[0].wide.end[:year]} PH"
60
+ if @date[0].wide_interval.end && @date[0].wide_interval.end[:year]
61
+ return "#{@date[0].wide_interval.start[:year]}-#{@date[0].wide_interval.end[:year]} PH"
62
62
  else
63
- return "#{@date[0].wide.start[:year]} PH"
63
+ return "#{@date[0].wide_interval.start[:year]} PH"
64
64
  end
65
65
  end
66
66
  end
@@ -269,7 +269,7 @@ module OpeningHoursConverter
269
269
  return false
270
270
  else
271
271
  @date.each_with_index do |d, i|
272
- return false if !d.wide.equals(o.date[i].wide)
272
+ return false if !d.wide_interval.equals(o.date[i].wide_interval)
273
273
  end
274
274
  return true
275
275
  end
@@ -520,7 +520,6 @@ module OpeningHoursConverter
520
520
  end
521
521
  end
522
522
  else
523
- # binding.pry
524
523
  if in_day.start[:month] == in_day.end[:month]
525
524
  days_count += in_day.end[:day] - in_day.start[:day] + 1
526
525
  elsif in_day.start[:month] > in_day.end[:month]
@@ -82,25 +82,25 @@ module OpeningHoursConverter
82
82
  def self.build_day_array_from_dates(dates, get_iterator=false)
83
83
  years = {}
84
84
  dates.each do |date|
85
- if !date.wide.start.nil? && !date.wide.start[:year].nil?
86
- if date.wide.end.nil? || date.wide.end[:year].nil? || date.wide.start[:year] == date.wide.end[:year]
87
- if !years[date.wide.start[:year]].nil?
88
- years = process_single_year(date.wide, years)
85
+ if !date.wide_interval.start.nil? && !date.wide_interval.start[:year].nil?
86
+ if date.wide_interval.end.nil? || date.wide_interval.end[:year].nil? || date.wide_interval.start[:year] == date.wide_interval.end[:year]
87
+ if !years[date.wide_interval.start[:year]].nil?
88
+ years = process_single_year(date.wide_interval, years)
89
89
  else
90
- years[date.wide.start[:year]] = Array.new(OSM_MONTHS.length) { |i| Array.new(MONTH_END_DAY[i]) { false } }
91
- years = process_single_year(date.wide, years)
90
+ years[date.wide_interval.start[:year]] = Array.new(OSM_MONTHS.length) { |i| Array.new(MONTH_END_DAY[i]) { false } }
91
+ years = process_single_year(date.wide_interval, years)
92
92
  end
93
93
  else
94
- for year in date.wide.start[:year]..date.wide.end[:year]
94
+ for year in date.wide_interval.start[:year]..date.wide_interval.end[:year]
95
95
  years[year] ||= Array.new(OSM_MONTHS.length) { |i| Array.new(MONTH_END_DAY[i]) { false } }
96
96
  end
97
- process_multiple_years(date.wide, years)
97
+ process_multiple_years(date.wide_interval, years)
98
98
  end
99
99
  else
100
100
  unless get_iterator
101
101
  years["always"] ||= Array.new(OSM_MONTHS.length) { |i| Array.new(MONTH_END_DAY[i]) { false } }
102
102
  end
103
- years = process_always(date.wide, years, get_iterator)
103
+ years = process_always(date.wide_interval, years, get_iterator)
104
104
  end
105
105
  end
106
106
  years
@@ -209,7 +209,7 @@ module OpeningHoursConverter
209
209
  years[year][wide_interval.start[:month]-1][i] = true
210
210
  end
211
211
  else
212
- for month in wide_interval.start[:month]-1..date.wide.end[:month]-1
212
+ for month in wide_interval.start[:month]-1..date.wide_interval.end[:month]-1
213
213
  years[year][month].each_with_index do |day, i|
214
214
  years[year][month][i] = true
215
215
  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.7.20
4
+ version: 1.8.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: 2018-11-08 00:00:00.000000000 Z
11
+ date: 2019-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json