opening_hours_converter 0.0.6 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
1
  require 'opening_hours_converter/constants'
2
+ require 'pry-nav'
2
3
 
3
4
  module OpeningHoursConverter
4
5
  class WideInterval
@@ -16,23 +17,23 @@ module OpeningHoursConverter
16
17
 
17
18
  case @type
18
19
  when "day"
19
- result = "#{OSM_MONTHS[@start[:month]-1]} #{@start[:day] < 10 ? "0" : ""}#{@start[:day]}"
20
+ result = "#{@start[:year].nil? ? "" : "#{@start[:year]} "}#{OSM_MONTHS[@start[:month]-1]} #{@start[:day] < 10 ? "0" : ""}#{@start[:day]}"
20
21
  if !@end.nil?
21
22
  if @start[:month] == @end[:month]
22
- result += "-#{@end[:day] < 10 ? "0" : ""}#{@end[:day]}"
23
+ result += "-#{@start[:year] == @end[:year] ? "" : "#{@end[:year]} "}#{@end[:day] < 10 ? "0" : ""}#{@end[:day]}"
23
24
  else
24
- result += "-#{OSM_MONTHS[@end[:month]-1]} #{@end[:day] < 10 ? "0" : ""}#{@end[:day]}"
25
+ result += "-#{@start[:year] == @end[:year] ? "" : "#{@end[:year]} "}#{OSM_MONTHS[@end[:month]-1]} #{@end[:day] < 10 ? "0" : ""}#{@end[:day]}"
25
26
  end
26
27
  end
27
- when "week"
28
- result = "week #{@start[:week] < 10 ? "0" : ""}#{@start[:week]}"
28
+ when "month"
29
+ result = "#{@start[:year].nil? ? "" : "#{@start[:year]} "}#{OSM_MONTHS[@start[:month]-1]}"
29
30
  if !@end.nil?
30
- result += "-#{@end[:week] < 10 ? "0" : ""}#{@end[:week]}"
31
+ result += "-#{@start[:year] == @end[:year] ? "" : "#{@end[:year]} "}#{OSM_MONTHS[@end[:month]-1]}"
31
32
  end
32
- when "month"
33
- result = "#{OSM_MONTHS[@start[:month]-1]}"
33
+ when "year"
34
+ result = "#{@start[:year]}"
34
35
  if !@end.nil?
35
- result += "-#{OSM_MONTHS[@end[:month]-1]}"
36
+ result += "-#{@end[:year]}"
36
37
  end
37
38
  when "always"
38
39
  result = ""
@@ -55,12 +56,6 @@ module OpeningHoursConverter
55
56
  else
56
57
  result = "le #{@start[:day]} #{IRL_MONTHS[@start[:month] - 1]}"
57
58
  end
58
- when "week"
59
- if !@end.nil?
60
- result = "toutes les semaines de la semaine #{@start[:week]} à la semaine #{@end[:week]}"
61
- else
62
- result = "la semaine #{@start[:week]}"
63
- end
64
59
  when "month"
65
60
  if !@end.nil?
66
61
  result = "toutes les semaines de #{IRL_MONTHS[@start[:month] - 1]} à #{IRL_MONTHS[@endd[:month] - 1]}"
@@ -73,39 +68,39 @@ module OpeningHoursConverter
73
68
  return result
74
69
  end
75
70
 
76
- def day(start_day, start_month, end_day=nil, end_month=nil)
71
+ def day(start_day, start_month, start_year=nil, end_day=nil, end_month=nil, end_year=nil)
77
72
  if start_day.nil? || start_month.nil?
78
- raise(ArgumentError, "start_day and start_month are required")
73
+ raise(ArgumentError, "start_day, start_month and start_year are required")
79
74
  end
80
- @start = { day: start_day, month: start_month }
81
- if (!end_day.nil? && !end_month.nil? && (end_day != start_day || end_month != start_month))
82
- @end = { day: end_day, month: end_month }
75
+ @start = { day: start_day, month: start_month, year: start_year }
76
+ if (!end_day.nil? && !end_month.nil? && (end_day != start_day || end_month != start_month || (!start_year.nil? && !end_year.nil? && end_year != start_year)))
77
+ @end = { day: end_day, month: end_month, year: end_year }
83
78
  end
84
79
  @type = "day"
85
80
  self
86
81
  end
87
82
 
88
- def week(start_week, end_week=nil)
89
- if start_week.nil?
90
- raise(ArgumentError, "start_week is required")
83
+ def month(start_month, start_year=nil, end_month=nil, end_year=nil)
84
+ if start_month.nil?
85
+ raise(ArgumentError, "start_month is required")
91
86
  end
92
- @start = { week: start_week }
93
- unless end_week.nil? || end_week == start_week
94
- @end = { week: end_week }
87
+ @start = { month: start_month, year: start_year }
88
+ if !end_month.nil? && (end_month != start_month || (!start_year.nil? && !end_year.nil? && end_year != start_year))
89
+ @end = { month: end_month, year: end_year }
95
90
  end
96
- @type = "week"
91
+ @type = "month"
97
92
  self
98
93
  end
99
94
 
100
- def month(start_month, end_month=nil)
101
- if start_month.nil?
102
- raise(ArgumentError, "start_month is required")
95
+ def year(start_year, end_year=nil)
96
+ if start_year.nil?
97
+ raise(ArgumentError, "start_year is required")
103
98
  end
104
- @start = { month: start_month }
105
- unless end_month.nil? || end_month == start_month
106
- @end = { month: end_month }
99
+ @start = { year: start_year }
100
+ unless end_year.nil? || end_year == start_year
101
+ @end = { year: end_year }
107
102
  end
108
- @type = "month"
103
+ @type = "year"
109
104
  self
110
105
  end
111
106
 
@@ -117,9 +112,10 @@ module OpeningHoursConverter
117
112
  end
118
113
 
119
114
  def is_full_month?
120
- if @type == "month" && @end.nil?
121
- true
122
- elsif @type == "day"
115
+ return true if @type == "month" && @end.nil?
116
+ return false if @end.nil?
117
+ return false if !@start[:year].nil? && !@end[:year].nil? && @start[:year] != @end[:year]
118
+ if @type == "day"
123
119
  @start[:day] == 1 && !@end.nil? && @start[:month] == @end[:month] &&
124
120
  !@end[:day].nil? && @end[:day] == MONTH_END_DAY[@end[:month] - 1]
125
121
  else
@@ -128,137 +124,140 @@ module OpeningHoursConverter
128
124
  end
129
125
 
130
126
  def starts_month?
131
- @type == "month" || @type == "always" || (@type == "day" && @start[:day] == 1)
127
+ @type == "month" || @type == "always" || @type == "year" || (@type == "day" && @start[:day] == 1)
132
128
  end
133
129
 
134
130
  def ends_month?
135
- @type == "month" || @type == "always" || (@type == "day" && !@end.nil? && @end[:day] == MONTH_END_DAY[@end[:month] - 1])
131
+ @type == "month" || @type == "always" || @type == "year" || (@type == "day" && !@end.nil? && @end[:day] == MONTH_END_DAY[@end[:month] - 1])
132
+ end
133
+
134
+ def is_full_year?
135
+ return true if @end.nil? && type == "year"
136
+ return false if @end.nil?
137
+ return false if !@start[:year].nil? && !@end[:year].nil? && @start[:year] != @end[:year]
138
+ if @type == "month"
139
+ @start[:month] == 1 && !@end.nil? && @start[:year] == @end[:year] && !@end[:month].nil? && @end[:month] == 12
140
+ elsif @type == "day"
141
+ @start[:day] == 1 && @start[:month] == 1 && !@end.nil? && !@start[:year].nil? && !@end[:year].nil? && @start[:year] == @end[:year] &&
142
+ !@end[:month].nil? && @end[:month] == 12 && !@end[:day].nil? && @end[:day] == MONTH_END_DAY[@end[:month] - 1]
143
+ else
144
+ false
145
+ end
146
+ end
147
+
148
+ def starts_year?
149
+ @type == "year" || @type == "always" || (@type == "day" && @start[:day] == 1 && @start[:month] == 1) || (@type == "month" && @start[:month] == 1)
150
+ end
151
+
152
+ def ends_year?
153
+ @type == "year" || @type == "always" || (@type == "day" && !@end.nil? && @end[:month] == 12 && @end[:day] == MONTH_END_DAY[@end[:month] - 1])
136
154
  end
137
155
 
138
156
  def contains?(o)
157
+ return false if o.type == "always"
139
158
  result = false
140
159
  if self.equals(o)
141
160
  result = false
142
161
  elsif @type == "always"
143
162
  result = true
144
- elsif @type == "day"
145
- if o.type == "day"
146
- if o.start[:month] > @start[:month] || (o.start[:month] == @start[:month] && o.start[:day] >= @start[:day])
163
+ else
164
+ my = to_day
165
+ o = o.to_day
166
+ result = has_superior_or_equal_start_day?(my, o) && has_inferior_or_equal_end_day?(my, o)
167
+ end
168
+ return result
169
+ end
147
170
 
148
- if !o.end.nil?
149
- if !@end.nil? && (o.end[:month] < @end[:month] || (o.end[:month] == @end[:month] && o.end[:day] <= @end[:day]))
150
- result = true
151
- end
152
- else
153
- if !@end.nil? && (o.start[:month] < @end[:month] || (o.start[:month] == @end[:month] && o.start[:day] <= @end[:day]))
154
- result = true
155
- end
156
- end
171
+ def has_superior_or_equal_start_day?(my, o)
172
+ result = false
173
+ if has_start_year?(o) && has_start_year?(my)
174
+ result = o.start[:year] > my.start[:year] || (o.start[:year] == my.start[:year] && has_superior_start_month?(my, o))
175
+ elsif !has_start_year?(o) && !has_start_year?(my)
176
+ result = has_superior_start_month?(my, o)
177
+ end
178
+ result
179
+ end
157
180
 
158
- end
159
- elsif o.type == "month"
160
- if o.start[:month] > @start[:month] || (o.start[:month] == @start[:month] && @start[:day] == 1)
161
- if !o.end.nil? && !@end.nil? && (o.end[:month] < @end[:month] || (o.end[:month] == @end[:month] && @end[:day] == MONTH_END_DAY[@end.month-1]))
162
- result = true
163
- elsif o.end.nil? && (!@end.nil? && o.start[:month] < @end[:month])
164
- result = true
165
- end
166
- end
167
- end
168
- elsif @type == "week"
169
- if o.type == "week"
170
- if o.start[:week] >= @start[:week]
171
- if !o.end.nil? && !@end.nil? && o.end[:week] <= @end[:week]
172
- result = true
173
- elsif o.end.nil? && ((!@end.nil? && o.start[:week] <= @end[:week]) || o.start[:week] == @start[:week])
174
- result = true
175
- end
176
- end
181
+ def has_superior_start_month?(my, o)
182
+ (o.start[:month] > my.start[:month] ||
183
+ (o.start[:month] == my.start[:month] &&
184
+ o.start[:day] >= my.start[:day]))
185
+ end
186
+
187
+ def has_inferior_or_equal_end_day?(my, o)
188
+ result = false
189
+ return false if my.end.nil?
190
+ if !o.end.nil?
191
+ if has_end_year?(o) && has_end_year?(my)
192
+ result = o.end[:year] < my.end[:year] || (o.end[:year] == my.end[:year] && has_inferior_end_month?(my, o))
193
+ elsif !has_end_year?(o) && !has_end_year?(my)
194
+ result = has_inferior_end_month?(my, o)
177
195
  end
178
- elsif @type == "month"
179
- if o.type == "month"
180
- if o.start[:month] >= @start[:month]
181
- if !o.end.nil? && !@end.nil? && o.end[:month] <= @end[:month]
182
- result = true
183
- elsif o.end.nil? && ((!@end.nil? && o.start[:month] <= @end[:month]) || o.start[:month] == @start[:month])
184
- result = true
185
- end
186
- end
187
- elsif o.type == "day"
188
- if !o.end.nil?
189
- if @end.nil?
190
- if o.start[:month] == @start[:month] &&
191
- o.end[:month] == @start[:month] &&
192
- ((o.start[:day] >= 1 && o.end[:day] < MONTH_END_DAY[o.start[:month]-1]) ||
193
- (o.start[:day] > 1 && o.end[:day] <= MONTH_END_DAY[o.start[:month]-1]))
194
- result = true
195
- end
196
- else
197
- if o.start[:month] >= @start[:month] && o.end[:month] <= @end[:month]
198
- if ((o.start[:month] > @start[:month] && o.end[:month] < @end[:month]) ||
199
- (o.start[:month] == @start[:month] && o.end[:month] < @end[:month] && o.start.day > 1) ||
200
- (o.start[:month] > @start[:month] && o.end[:month] == @end[:month] && o.end[:day] < MONTH_END_DAY[o.end[:month]-1]) ||
201
- (o.start[:day] >= 1 && o.end[:day] < MONTH_END_DAY[o.end[:month]-1]) ||
202
- (o.start[:day] > 1 && o.end[:day] <= MONTH_END_DAY[o.end[:month]-1]))
203
- result = true
204
- end
205
- end
206
- end
207
- else
208
- if @end.nil?
209
- if @start[:month] == o.start[:month]
210
- result = true
211
- end
212
- else
213
- if o.start[:month] >= @start[:month] && o.start[:month] <= @end[:month]
214
- result = true
215
- end
216
- end
217
- end
196
+ else
197
+ if has_start_year?(o) && has_end_year?(my)
198
+ result = o.start[:year] < my.end[:year] || (o.start[:year] == my.end[:year] && has_inferior_end_month?(my, o))
199
+ elsif !has_start_year?(o) && !has_end_year?(my)
200
+ result = has_inferior_end_month?(my, o)
218
201
  end
219
202
  end
220
- return result
203
+
204
+ result
205
+ end
206
+
207
+ def has_inferior_end_month?(my, o)
208
+ if !o.end.nil?
209
+ (o.end[:month] < my.end[:month] ||
210
+ (o.end[:month] == my.end[:month] &&
211
+ o.end[:day] <= my.end[:day]))
212
+ else
213
+ (o.start[:month] < my.end[:month] ||
214
+ (o.start[:month] == my.end[:month] &&
215
+ o.start[:day] <= my.end[:day]))
216
+ end
217
+ end
218
+
219
+ def has_start_year?(date)
220
+ !date.start[:year].nil?
221
+ end
222
+ def has_end_year?(date)
223
+ !date.end[:year].nil?
221
224
  end
222
225
 
223
226
  def equals(o)
224
227
  return false unless o.instance_of?(OpeningHoursConverter::WideInterval)
225
228
  return @type == "always" if o.type == "always"
226
-
227
- result = false
229
+ return false if @type == "always"
230
+ self_to_day = to_day
231
+ o_to_day = o.to_day
232
+ return (self_to_day.start[:year] == o_to_day.start[:year] &&
233
+ self_to_day.start[:month] == o_to_day.start[:month] &&
234
+ self_to_day.start[:day] == o_to_day.start[:day]) &&
235
+ ((self_to_day.end.nil? && o_to_day.end.nil?) || ((!self_to_day.end.nil? && !o_to_day.end.nil?) &&
236
+ (self_to_day.end[:year] == o_to_day.end[:year] &&
237
+ self_to_day.end[:month] == o_to_day.end[:month] &&
238
+ self_to_day.end[:day] == o_to_day.end[:day])))
239
+ end
240
+ def to_day
228
241
  case @type
229
- when "always"
230
- result = o.start.nil?
231
242
  when "day"
232
- result = ((o.type == "day" &&
233
- o.start[:month] == @start[:month] &&
234
- o.start[:day] == @start[:day] &&
235
- ((o.end.nil? && @end.nil?) ||
236
- (!o.end.nil? && !@end.nil? &&
237
- o.end[:month] == @end[:month] &&
238
- o.end[:day] == @end[:day]))) ||
239
- (o.type == "month" &&
240
- o.start[:month] == @start[:month] &&
241
- (o.is_full_month? && is_full_month?) ||
242
- (!o.end.nil? && !@end.nil? &&
243
- o.end[:month] == @end[:month] &&
244
- o.ends_month? && ends_month?)))
245
- when "week"
246
- result = (o.type == "week" &&
247
- o.start[:week] == @start[:week] &&
248
- (o.end == @end ||
249
- (!o.end.nil? && !@end.nil? && o.end[:week] == @end[:week])))
243
+ if @end.nil?
244
+ OpeningHoursConverter::WideInterval.new.day(@start[:day], @start[:month], @start[:year])
245
+ else
246
+ OpeningHoursConverter::WideInterval.new.day(@start[:day], @start[:month], @start[:year], @end[:day], @end[:month], @end[:year])
247
+ end
250
248
  when "month"
251
- result = (o.type == "day" &&
252
- o.start[:month] == @start[:month] &&
253
- (o.starts_month? &&
254
- (!o.end.nil? && @end.nil? && o.end[:month] == @start[:month] && o.ends_month?) ||
255
- (!o.end.nil? && !@end.nil? && o.end[:month] == @end[:month] && o.ends_month?))) ||
256
- (o.type == "month" &&
257
- o.start[:month] == @start[:month] &&
258
- ((o.end.nil? && @end.nil?) ||
259
- (!o.end.nil? && !@end.nil? && o.end[:month] == @end[:month])))
249
+ if @end.nil?
250
+ OpeningHoursConverter::WideInterval.new.day(1, @start[:month], @start[:year], MONTH_END_DAY[@start[:month] - 1], @start[:month], @start[:year])
251
+ else
252
+ OpeningHoursConverter::WideInterval.new.day(1, @start[:month], @start[:year], MONTH_END_DAY[@end[:month] - 1], @end[:month], @end[:year])
253
+ end
254
+ when "year"
255
+ if @end.nil?
256
+ OpeningHoursConverter::WideInterval.new.day(1, 1, @start[:year], 31, 12, @start[:year])
257
+ else
258
+ OpeningHoursConverter::WideInterval.new.day(1, 1, @start[:year], 31, 12, @end[:year])
259
+ end
260
260
  end
261
- result
262
261
  end
263
262
  end
264
263
  end
@@ -1,4 +1,5 @@
1
1
  module OpeningHoursConverter
2
+ require 'date'
2
3
  require_relative './opening_hours_converter/date_range'
3
4
  require_relative './opening_hours_converter/day'
4
5
  require_relative './opening_hours_converter/interval'
metadata CHANGED
@@ -1,16 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opening_hours_converter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 1.0.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-10 00:00:00.000000000 Z
11
+ date: 2017-10-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Datetime range to openinghours, openinghours to datetime range
13
+ description: Datetime range to openinghours, openinghours to datetime range. Very
14
+ strongly inspired by yohours.
14
15
  email: tech@publidata.io
15
16
  executables: []
16
17
  extensions: []