Class: OpeningHoursConverter::WideInterval
- Inherits:
-
Object
- Object
- OpeningHoursConverter::WideInterval
- 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
-
#end ⇒ Object
Returns the value of attribute end.
-
#start ⇒ Object
Returns the value of attribute start.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #always ⇒ Object
- #contains?(o) ⇒ Boolean
- #date_time(start_date, end_date = nil) ⇒ Object
- #day(start_day, start_month, start_year = nil, end_day = nil, end_month = nil, end_year = nil) ⇒ Object
- #ends_month? ⇒ Boolean
- #ends_year? ⇒ Boolean
- #equals(o) ⇒ Object
- #get_time_for_humans ⇒ Object
- #get_time_selector ⇒ Object
- #has_end_year?(date) ⇒ Boolean
- #has_inferior_end_month?(my, o) ⇒ Boolean
- #has_inferior_or_equal_end_day?(my, o) ⇒ Boolean
- #has_start_year?(date) ⇒ Boolean
- #has_superior_or_equal_start_day?(my, o) ⇒ Boolean
- #has_superior_start_month?(my, o) ⇒ Boolean
- #holiday(holiday, start_year = nil, end_year = nil) ⇒ Object
-
#initialize ⇒ WideInterval
constructor
A new instance of WideInterval.
- #is_full_month? ⇒ Boolean
- #is_full_year? ⇒ Boolean
- #month(start_month, start_year = nil, end_month = nil, end_year = nil) ⇒ Object
- #starts_month? ⇒ Boolean
- #starts_year? ⇒ Boolean
- #to_day ⇒ Object
- #year(start_year, end_year = nil) ⇒ Object
Constructor Details
#initialize ⇒ WideInterval
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
#end ⇒ Object
Returns the value of attribute end.
6 7 8 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 6 def end @end end |
#start ⇒ Object
Returns the value of attribute start.
6 7 8 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 6 def start @start end |
#type ⇒ Object
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
#always ⇒ Object
172 173 174 175 176 177 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 172 def always @start = nil @end = nil @type = "always" self end |
#contains?(o) ⇒ Boolean
221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 221 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
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 118 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
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 106 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
195 196 197 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 195 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
217 218 219 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 217 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
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 291 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_humans ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 50 def get_time_for_humans result = "" case @type when "day" if !@end.nil? if @start[:year] && !@end[:year] || @start[:year] && @start[:year] == @end[:year] result = "du #{@start[:day]} #{IRL_MONTHS[@start[:month] - 1]} #{@start[:year]} au #{@end[:day]} #{IRL_MONTHS[@end[:month] - 1]} #{@start[:year]}" elsif @start[:year] && @end[:year] && @start[:year] != @end[:year] result = "du #{@start[:day]} #{IRL_MONTHS[@start[:month] - 1]} #{@start[:year]} au #{@end[:day]} #{IRL_MONTHS[@end[:month] - 1]} #{@end[:year]}" elsif @start[:month] != @end[:month] result = "du #{@start[:day]} #{IRL_MONTHS[@start[:month] - 1]} au #{@end[:day]} #{IRL_MONTHS[@end[:month] - 1]}" else result = "le #{@start[:day]} #{IRL_MONTHS[@start[:month] - 1]}" end else result = "le #{@start[:day]} #{IRL_MONTHS[@start[:month] - 1]} #{@start[:year] ? @start[:year] : ""}" end when "month" if !@end.nil? if @start[:year] && !@end[:year] || @start[:year] && @start[:year] == @end[:year] result = "de #{IRL_MONTHS[@start[:month] - 1]} #{@start[:year]} à #{IRL_MONTHS[@end[:month] - 1]} #{@start[:year]}" elsif @start[:year] && @end[:year] && @start[:year] != @end[:year] result = "de #{IRL_MONTHS[@start[:month] - 1]} #{@start[:year]} à #{IRL_MONTHS[@end[:month] - 1]} #{@end[:year]}" else result = "de #{IRL_MONTHS[@start[:month] - 1]} à #{IRL_MONTHS[@end[:month] - 1]}" end else result = "#{IRL_MONTHS[@start[:month] - 1]}#{@start[:year] ? " #{@start[:year]}" : ""}" end when "year" if !@end.nil? result = "de #{@start[:year]} à #{@end[:year]}" else result = "#{@start[:year]}" end when "holiday" if !@end.nil? if !@start[:year] result = "jours fériés" else result = "les jours fériés de #{@start[:year]} à #{@end[:year]}" end else if !@start[:year] result = "jours fériés" else result = "les jours fériés de #{@start[:year]}" end end when "always" result = "tout le temps" end return result end |
#get_time_selector ⇒ Object
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
287 288 289 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 287 def has_end_year?(date) !date.end[:year].nil? end |
#has_inferior_end_month?(my, o) ⇒ Boolean
272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 272 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
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 252 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
284 285 286 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 284 def has_start_year?(date) !date.start[:year].nil? end |
#has_superior_or_equal_start_day?(my, o) ⇒ Boolean
236 237 238 239 240 241 242 243 244 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 236 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
246 247 248 249 250 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 246 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
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 160 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
179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 179 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
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 199 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
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 136 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
191 192 193 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 191 def starts_month? @type == "month" || @type == "always" || @type == "year" || (@type == "day" && @start[:day] == 1) end |
#starts_year? ⇒ Boolean
213 214 215 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 213 def starts_year? @type == "year" || @type == "always" || (@type == "day" && @start[:day] == 1 && @start[:month] == 1) || (@type == "month" && @start[:month] == 1) end |
#to_day ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 309 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
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 148 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 |