Class: OpeningHoursConverter::WideInterval

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

Constant Summary

Constants included from Constants

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWideInterval

Returns a new instance of WideInterval.



8
9
10
11
12
# File 'lib/opening_hours_converter/wide_interval.rb', line 8

def initialize
  @start = nil
  @end = nil
  @type = nil
end

Instance Attribute Details

#endObject

Returns the value of attribute end.



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

def end
  @end
end

#startObject

Returns the value of attribute start.



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

def start
  @start
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#alwaysObject



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

def always
  @start = nil
  @end = nil
  @type = "always"
  self
end

#contains?(o) ⇒ Boolean

Returns:

  • (Boolean)


190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/opening_hours_converter/wide_interval.rb', line 190

def contains?(o)
  return false if o.type == "always"
  result = false
  if self.equals(o)
    result = false
  elsif @type == "always"
    result = true
  else
    my = to_day
    o = o.to_day
    result = has_superior_or_equal_start_day?(my, o) && has_inferior_or_equal_end_day?(my, o)
  end
  return result
end

#date_time(start_date, end_date = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/opening_hours_converter/wide_interval.rb', line 87

def date_time(start_date, end_date=nil)
  if start_date.nil?
    raise(ArgumentError, "start_date is required")
  end
  if !start_date.instance_of?(DateTime)
    raise(ArgumentError, "start_date is not a DateTime")
  end
  if !end_date.instance_of?(DateTime)
    raise(ArgumentError, "end_date is not a DateTime")
  end
  @start = { day: start_date.day, month: start_date.month, year: start_date.year }
  if !end_date.nil? && end_date != start_date
    @end = { day: end_date.day, month: end_date.month, year: end_date.year }
  end
  @type = "day"
  self
end

#day(start_day, start_month, start_year = nil, end_day = nil, end_month = nil, end_year = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/opening_hours_converter/wide_interval.rb', line 75

def day(start_day, start_month, start_year=nil, end_day=nil, end_month=nil, end_year=nil)
  if start_day.nil? || start_month.nil?
    raise(ArgumentError, "start_day, start_month and start_year are required")
  end
  @start = { day: start_day, month: start_month, year: start_year }
  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)))
    @end = { day: end_day, month: end_month, year: end_year }
  end
  @type = "day"
  self
end

#ends_month?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/opening_hours_converter/wide_interval.rb', line 164

def ends_month?
  @type == "month" || @type == "always" || @type == "year" || (@type == "day" && !@end.nil? && @end[:day] == MONTH_END_DAY[@end[:month] - 1])
end

#ends_year?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/opening_hours_converter/wide_interval.rb', line 186

def ends_year?
  @type == "year" || @type == "always" || (@type == "day" && !@end.nil? && @end[:month] == 12 && @end[:day] == MONTH_END_DAY[@end[:month] - 1])
end

#equals(o) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/opening_hours_converter/wide_interval.rb', line 260

def equals(o)
  return false unless o.instance_of?(OpeningHoursConverter::WideInterval)
  return @type == "always" if o.type == "always"
  if @type == "holiday"
    return (o.type == "holiday" && (@start[:year] == o.start[:year]) &&
      (@end.nil? && o.end.nil? || (@end && o.end && @end[:year] == o.end[:year])))
  end
  return false if @type == "always"
  self_to_day = to_day
  o_to_day = o.to_day
  return (self_to_day.start[:year] == o_to_day.start[:year] &&
    self_to_day.start[:month] == o_to_day.start[:month] &&
    self_to_day.start[:day] == o_to_day.start[:day]) &&
    ((self_to_day.end.nil? && o_to_day.end.nil?) || ((!self_to_day.end.nil? && !o_to_day.end.nil?) &&
      (self_to_day.end[:year] == o_to_day.end[:year] &&
        self_to_day.end[:month] == o_to_day.end[:month] &&
        self_to_day.end[:day] == o_to_day.end[:day])))
end

#get_time_for_humansObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/opening_hours_converter/wide_interval.rb', line 50

def get_time_for_humans
  result = ""

  case @type
  when "day"
    if !@end.nil?
      result = "toutes les semaines du #{@start[:day]} #{IRL_MONTHS[@start[:month] - 1]}"
      if @start[:month] != @end[:month]
        result += " au #{@end[:day]} #{IRL_MONTHS[@end[:month] - 1]}"
      end
    else
      result = "le #{@start[:day]} #{IRL_MONTHS[@start[:month] - 1]}"
    end
  when "month"
    if !@end.nil?
      result = "toutes les semaines de #{IRL_MONTHS[@start[:month] - 1]} à #{IRL_MONTHS[@endd[:month] - 1]}"
    else
      result = "toutes les semaines de #{IRL_MONTHS[@start[:month] - 1]}"
    end
  when "always"
    result = "toutes les semaines"
  end
  return result
end

#get_time_selectorObject



14
15
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/wide_interval.rb', line 14

def get_time_selector
  result = ""

  case @type
  when "day"
    result = "#{@start[:year].nil? ? "" : "#{@start[:year]} "}#{OSM_MONTHS[@start[:month]-1]} #{@start[:day] < 10 ? "0" : ""}#{@start[:day]}"
    if !@end.nil?
      if @start[:month] == @end[:month]
        result += "-#{@start[:year] == @end[:year] ? "" : "#{@end[:year]} "}#{@end[:day] < 10 ? "0" : ""}#{@end[:day]}"
      else
        result += "-#{@start[:year] == @end[:year] ? "" : "#{@end[:year]} "}#{OSM_MONTHS[@end[:month]-1]} #{@end[:day] < 10 ? "0" : ""}#{@end[:day]}"
      end
    end
  when "month"
    result = "#{@start[:year].nil? ? "" : "#{@start[:year]} "}#{OSM_MONTHS[@start[:month]-1]}"
    if !@end.nil?
      result += "-#{@start[:year] == @end[:year] ? "" : "#{@end[:year]} "}#{OSM_MONTHS[@end[:month]-1]}"
    end
  when "year"
    result = "#{@start[:year]}"
    if !@end.nil?
      result += "-#{@end[:year]}"
    end
  when "holiday"
    result = "#{@start[:year].nil? ? "" : "#{@start[:year]} "}PH"
    if !@end.nil?
      result = "#{@start[:year]}#{@start[:year] == @end[:year] ? "" : "-#{@end[:year]}"} PH"
    end
  when "always"
    result = ""
  else
    result = ""
  end
  result
end

#has_end_year?(date) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_end_year?(date)
  !date.end[:year].nil?
end

#has_inferior_end_month?(my, o) ⇒ Boolean

Returns:

  • (Boolean)


241
242
243
244
245
246
247
248
249
250
251
# File 'lib/opening_hours_converter/wide_interval.rb', line 241

def has_inferior_end_month?(my, o)
  if !o.end.nil?
    (o.end[:month] < my.end[:month] ||
      (o.end[:month] == my.end[:month] &&
        o.end[:day] <= my.end[:day]))
  else
    (o.start[:month] < my.end[:month] ||
      (o.start[:month] == my.end[:month] &&
        o.start[:day] <= my.end[:day]))
  end
end

#has_inferior_or_equal_end_day?(my, o) ⇒ Boolean

Returns:

  • (Boolean)


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/opening_hours_converter/wide_interval.rb', line 221

def has_inferior_or_equal_end_day?(my, o)
  result = false
  return false if my.end.nil?
  if !o.end.nil?
    if has_end_year?(o) && has_end_year?(my)
      result = o.end[:year] < my.end[:year] || (o.end[:year] == my.end[:year] && has_inferior_end_month?(my, o))
    elsif !has_end_year?(o) && !has_end_year?(my)
      result = has_inferior_end_month?(my, o)
    end
  else
    if has_start_year?(o) && has_end_year?(my)
      result = o.start[:year] < my.end[:year] || (o.start[:year] == my.end[:year] && has_inferior_end_month?(my, o))
    elsif !has_start_year?(o) && !has_end_year?(my)
      result = has_inferior_end_month?(my, o)
    end
  end

  result
end

#has_start_year?(date) ⇒ Boolean

Returns:

  • (Boolean)


253
254
255
# File 'lib/opening_hours_converter/wide_interval.rb', line 253

def has_start_year?(date)
  !date.start[:year].nil?
end

#has_superior_or_equal_start_day?(my, o) ⇒ Boolean

Returns:

  • (Boolean)


205
206
207
208
209
210
211
212
213
# File 'lib/opening_hours_converter/wide_interval.rb', line 205

def has_superior_or_equal_start_day?(my, o)
  result = false
  if has_start_year?(o) && has_start_year?(my)
    result = o.start[:year] > my.start[:year] || (o.start[:year] == my.start[:year] && has_superior_start_month?(my, o))
  elsif !has_start_year?(o) && !has_start_year?(my)
    result = has_superior_start_month?(my, o)
  end
  result
end

#has_superior_start_month?(my, o) ⇒ Boolean

Returns:

  • (Boolean)


215
216
217
218
219
# File 'lib/opening_hours_converter/wide_interval.rb', line 215

def has_superior_start_month?(my, o)
  (o.start[:month] > my.start[:month] ||
    (o.start[:month] == my.start[:month] &&
      o.start[:day] >= my.start[:day]))
end

#holiday(holiday, start_year = nil, end_year = nil) ⇒ Object



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

def holiday(holiday, start_year=nil, end_year=nil)
  if holiday.nil? || holiday != "PH"
    raise(ArgumentError, "holiday is required and can only be PH")
  end
  @start = { holiday: holiday, year: start_year }
  unless end_year.nil? || end_year == start_year
    @end = { holiday: holiday, year: end_year }
  end
  @type = "holiday"
  self
end

#is_full_month?Boolean

Returns:

  • (Boolean)


148
149
150
151
152
153
154
155
156
157
158
# File 'lib/opening_hours_converter/wide_interval.rb', line 148

def is_full_month?
  return true if @type == "month" && @end.nil?
  return false if @end.nil?
  return false if !@start[:year].nil? && !@end[:year].nil? && @start[:year] != @end[:year]
  if @type == "day"
    @start[:day] == 1 && !@end.nil? && @start[:month] == @end[:month] &&
    !@end[:day].nil? && @end[:day] == MONTH_END_DAY[@end[:month] - 1]
  else
    false
  end
end

#is_full_year?Boolean

Returns:

  • (Boolean)


168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/opening_hours_converter/wide_interval.rb', line 168

def is_full_year?
  return true if @end.nil? && type == "year"
  return false if @end.nil?
  return false if !@start[:year].nil? && !@end[:year].nil? && @start[:year] != @end[:year]
  if @type == "month"
    @start[:month] == 1 && !@end.nil? && @start[:year] == @end[:year] && !@end[:month].nil? && @end[:month] == 12
  elsif @type == "day"
    @start[:day] == 1 && @start[:month] == 1 && !@end.nil? && !@start[:year].nil? && !@end[:year].nil? && @start[:year] == @end[:year] &&
    !@end[:month].nil? && @end[:month] == 12 && !@end[:day].nil? && @end[:day] == MONTH_END_DAY[@end[:month] - 1]
  else
    false
  end
end

#month(start_month, start_year = nil, end_month = nil, end_year = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/opening_hours_converter/wide_interval.rb', line 105

def month(start_month, start_year=nil, end_month=nil, end_year=nil)
  if start_month.nil?
    raise(ArgumentError, "start_month is required")
  end
  @start = { month: start_month, year: start_year }
  if !end_month.nil? && (end_month != start_month || (!start_year.nil? && !end_year.nil? && end_year != start_year))
    @end = { month: end_month, year: end_year }
  end
  @type = "month"
  self
end

#starts_month?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/opening_hours_converter/wide_interval.rb', line 160

def starts_month?
  @type == "month" || @type == "always" || @type == "year" || (@type == "day" && @start[:day] == 1)
end

#starts_year?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/opening_hours_converter/wide_interval.rb', line 182

def starts_year?
  @type == "year" || @type == "always" || (@type == "day" && @start[:day] == 1 && @start[:month] == 1) || (@type == "month" && @start[:month] == 1)
end

#to_dayObject



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/opening_hours_converter/wide_interval.rb', line 278

def to_day
  case @type
  when "day"
    if @end.nil?
      OpeningHoursConverter::WideInterval.new.day(@start[:day], @start[:month], @start[:year])
    else
      OpeningHoursConverter::WideInterval.new.day(@start[:day], @start[:month], @start[:year], @end[:day], @end[:month], @end[:year])
    end
  when "month"
    if @end.nil?
      OpeningHoursConverter::WideInterval.new.day(1, @start[:month], @start[:year], MONTH_END_DAY[@start[:month] - 1], @start[:month], @start[:year])
    else
      OpeningHoursConverter::WideInterval.new.day(1, @start[:month], @start[:year], MONTH_END_DAY[@end[:month] - 1], @end[:month], @end[:year])
    end
  when "year"
    if @end.nil?
      OpeningHoursConverter::WideInterval.new.day(1, 1, @start[:year], 31, 12, @start[:year])
    else
      OpeningHoursConverter::WideInterval.new.day(1, 1, @start[:year], 31, 12, @end[:year])
    end
  end
end

#year(start_year, end_year = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/opening_hours_converter/wide_interval.rb', line 117

def year(start_year, end_year=nil)
  if start_year.nil?
    raise(ArgumentError, "start_year is required")
  end
  @start = { year: start_year }
  unless end_year.nil? || end_year == start_year
    @end = { year: end_year }
  end
  @type = "year"
  self
end