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::EASTER_WEEKDAY, Constants::IRL_DAYS, Constants::IRL_MONTHS, Constants::MINUTES_MAX, Constants::MONTH_END_DAY, Constants::OSM_DAYS, Constants::OSM_DAYS_ABBREVIATIONS, Constants::OSM_MONTHS, Constants::PH_WEEKDAY, Constants::YEAR_DAYS_MAX
Instance Attribute Summary collapse
-
#end ⇒ Object
Returns the value of attribute end.
-
#indexes ⇒ Object
Returns the value of attribute indexes.
-
#open_ended ⇒ Object
Returns the value of attribute open_ended.
-
#start ⇒ Object
Returns the value of attribute start.
-
#step ⇒ Object
Returns the value of attribute step.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #always ⇒ Object
- #contains?(o) ⇒ Boolean
-
#covered_years ⇒ Object
The years the range spans.
- #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_public_holidays_for_year(year = Time.now.year) ⇒ Object
- #get_time_for_humans ⇒ Object
- #get_time_selector ⇒ Object
-
#get_variable_days_for_year(year = Time.now.year) ⇒ Object
Resolves the bounds against one year.
- #get_weeks_for_year(year = Time.now.year) ⇒ 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
- #my_end_day_is_after_o_start_day?(my, o) ⇒ Boolean
- #my_end_day_is_before_o_end_day?(my, o) ⇒ Boolean
- #my_end_day_is_before_o_start_day?(my, o) ⇒ Boolean
- #my_end_is_after_o_start?(my, o) ⇒ Boolean
- #my_end_is_before_o_end?(my, o) ⇒ Boolean
- #my_start_day_is_after_o_end_day?(my, o) ⇒ Boolean
- #my_start_day_is_after_o_start_day?(my, o) ⇒ Boolean
- #my_start_day_is_before_o_end_day?(my, o) ⇒ Boolean
- #my_start_day_is_before_o_start_day?(my, o) ⇒ Boolean
- #my_start_is_after_o_start?(my, o) ⇒ Boolean
- #my_start_is_before_o_end?(my, o) ⇒ Boolean
- #my_start_is_before_o_start?(my, o) ⇒ Boolean
- #resolve_variable_date(bound, year) ⇒ Object
- #sort_days_array(days_array, by = :start) ⇒ Object
- #starts_month? ⇒ Boolean
- #starts_year? ⇒ Boolean
- #to_day ⇒ Object
- #touch?(o) ⇒ Boolean
- #variable_date_selector(bound) ⇒ Object
- #variable_day(start_date, end_date) ⇒ Object
- #week(week_indexes, start_year = nil, end_year = nil) ⇒ Object
-
#week_as_day(index, year, weeks_as_days) ⇒ Object
A year has 52 or 53 ISO weeks, so an index past the last one selects nothing rather than a week borrowed from the next year.
- #width ⇒ Object
- #year(start_year, end_year = nil, open_ended: false, step: nil) ⇒ Object
Constructor Details
#initialize ⇒ WideInterval
Returns a new instance of WideInterval.
8 9 10 11 12 13 14 15 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 8 def initialize @start = nil @end = nil @indexes = nil @type = nil @open_ended = false @step = 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 |
#indexes ⇒ Object
Returns the value of attribute indexes.
6 7 8 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 6 def indexes @indexes end |
#open_ended ⇒ Object
Returns the value of attribute open_ended.
6 7 8 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 6 def open_ended @open_ended 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 |
#step ⇒ Object
Returns the value of attribute step.
6 7 8 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 6 def step @step 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
195 196 197 198 199 200 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 195 def always @start = nil @end = nil @type = 'always' self end |
#contains?(o) ⇒ Boolean
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 244 def contains?(o) return false if o.type == 'always' result = false if equals(o) result = false elsif @type == 'always' result = true else my = to_day o = o.to_day if my.is_a?(Array) if o.is_a?(Array) return false else return my.all? { |day| day.contains?(o) } end elsif o.is_a?(Array) return o.all? { |day| contains?(day) } end result = has_superior_or_equal_start_day?(my, o) && has_inferior_or_equal_end_day?(my, o) end result end |
#covered_years ⇒ Object
The years the range spans. A year range can carry a step ("2020-2030/2" is every other year); every other kind of range covers all of them.
169 170 171 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 169 def covered_years @start[:year].step(@end[:year], @step || 1) end |
#date_time(start_date, end_date = nil) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 124 def date_time(start_date, end_date = nil) raise(ArgumentError, 'start_date is required') if start_date.nil? raise(ArgumentError, 'start_date is not a DateTime') if !start_date.instance_of?(DateTime) raise(ArgumentError, 'end_date is not a DateTime') if !end_date.instance_of?(DateTime) @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
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 112 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
218 219 220 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 218 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
240 241 242 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 240 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
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 536 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 false if self_to_day.is_a?(Array) != o_to_day.is_a?(Array) if self_to_day.is_a?(Array) if o_to_day.is_a?(Array) return false unless o_to_day.length == self_to_day.length return self.start == o.start && self_to_day.all? do |self_day| o_to_day.any? do |o_day| self_day.equals(o_day) end end else return self_to_day.all? { |day| self_to_day.equals(o_to_day) } end elsif o_to_day.is_a?(Array) return o_to_day.all? { |day| equals(day) } end (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_public_holidays_for_year(year = Time.now.year) ⇒ Object
763 764 765 766 767 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 763 def get_public_holidays_for_year(year = Time.now.year) OpeningHoursConverter::PublicHoliday.ph_for_year(year).map do |holiday| OpeningHoursConverter::WideInterval.new.day(holiday.day, holiday.month, holiday.year) end end |
#get_time_for_humans ⇒ Object
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 105 106 107 108 109 110 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 58 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] || ''}" 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' result = if !@end.nil? "de #{@start[:year]} à #{@end[:year]}" else (@start[:year]).to_s end when 'holiday' result = if !@end.nil? if !@start[:year] 'Jours fériés' else "Les jours fériés de #{@start[:year]} à #{@end[:year]}" end elsif !@start[:year] 'Jours fériés' else "Les jours fériés de #{@start[:year]}" end when 'always' result = 'Toute l\'année' end result end |
#get_time_selector ⇒ Object
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 49 50 51 52 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 17 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]).to_s result += "-#{@end[:year]}" if !@end.nil? result += "/#{@step}" if !@step.nil? when 'holiday' result = "#{@start[:year].nil? ? '' : "#{@start[:year]} "}PH" if !@end.nil? result = "#{@start[:year]}#{@start[:year] == @end[:year] ? '' : "-#{@end[:year]}"} PH" end when 'variable_day' result = "#{variable_date_selector(@start)}-#{variable_date_selector(@end)}" when 'always' result = '' else result = '' end result end |
#get_variable_days_for_year(year = Time.now.year) ⇒ Object
747 748 749 750 751 752 753 754 755 756 757 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 747 def get_variable_days_for_year(year = Time.now.year) from = resolve_variable_date(@start, year) to = resolve_variable_date(@end, year) to = resolve_variable_date(@end, year + 1) if to < from to -= 1 return [] if to < from [OpeningHoursConverter::WideInterval.new.day(from.day, from.month, from.year, to.day, to.month, to.year)] rescue ArgumentError [] end |
#get_weeks_for_year(year = Time.now.year) ⇒ Object
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 710 def get_weeks_for_year(year = Time.now.year) weeks_as_days = [] @indexes.each do |week_index| if week_index.is_a?(Integer) week_as_day(week_index, year, weeks_as_days) else if week_index.key?(:modifier) i = 0 (week_index[:from]..week_index[:to]).map do |index| week_as_day(index, year, weeks_as_days) if i % week_index[:modifier] == 0 i += 1 end else (week_index[:from]..week_index[:to]).map do |index| week_as_day(index, year, weeks_as_days) end end end end weeks_as_days end |
#has_end_year?(date) ⇒ Boolean
532 533 534 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 532 def has_end_year?(date) !date.end[:year].nil? end |
#has_inferior_end_month?(my, o) ⇒ Boolean
512 513 514 515 516 517 518 519 520 521 522 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 512 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
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 492 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?(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_end_year?(my) result = has_inferior_end_month?(my, o) end end result end |
#has_start_year?(date) ⇒ Boolean
524 525 526 527 528 529 530 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 524 def has_start_year?(date) if date.is_a?(Array) date.all? { |d| !d.start[:year].nil? } else !date.start[:year].nil? end end |
#has_superior_or_equal_start_day?(my, o) ⇒ Boolean
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 468 def has_superior_or_equal_start_day?(my, o) result = false if has_start_year?(o) && has_start_year?(my) if o.is_a?(Array) if my.is_a?(Array) o = sort_days_array o my = sort_days_array my has_superior_or_equal_start_day?(my.first, o.first) else o = sort_days_array o has_superior_or_equal_start_day?(my, o.first) end elsif my.is_a?(Array) my = sort_days_array my has_superior_or_equal_start_day?(my.first, o) else result = o.start[:year] > my.start[:year] || (o.start[:year] == my.start[:year] && has_superior_start_month?(my, o)) end elsif !has_start_year?(my) result = has_superior_start_month?(my, o) end result end |
#has_superior_start_month?(my, o) ⇒ Boolean
453 454 455 456 457 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 453 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
173 174 175 176 177 178 179 180 181 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 173 def holiday(holiday, start_year = nil, end_year = nil) if holiday.nil? || !%w[PH easter].include?(holiday) raise(ArgumentError, 'holiday is required and can only be PH or easter') end @start = { holiday: holiday, year: start_year } @end = { holiday: holiday, year: end_year } unless end_year.nil? || end_year == start_year @type = 'holiday' self end |
#is_full_month? ⇒ Boolean
202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 202 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
222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 222 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 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 136 def month(start_month, start_year = nil, end_month = nil, end_year = nil) raise(ArgumentError, 'start_month is required') if start_month.nil? @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 |
#my_end_day_is_after_o_start_day?(my, o) ⇒ Boolean
429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 429 def my_end_day_is_after_o_start_day?(my, o) if !my.end.nil? (o.start[:month] < my.end[:month] || (o.start[:month] == my.end[:month] && o.start[:day] <= my.end[:day])) else (o.start[:month] < my.start[:month] || (o.start[:month] == my.start[:month] && o.start[:day] <= my.start[:day])) end end |
#my_end_day_is_before_o_end_day?(my, o) ⇒ Boolean
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 409 def my_end_day_is_before_o_end_day?(my, o) if !o.end.nil? && !my.end.nil? (o.end[:month] > my.end[:month] || (o.end[:month] == my.end[:month] && o.end[:day] >= my.end[:day])) elsif o.end && my.end (o.start[:month] > my.start[:month] || (o.start[:month] == my.start[:month] && o.start[:day] >= my.start[:day])) elsif o.end (o.end[:month] > my.start[:month] || (o.end[:month] == my.start[:month] && o.end[:day] >= my.start[:day])) else (o.start[:month] > my.end[:month] || (o.start[:month] == my.end[:month] && o.start[:day] >= my.end[:day])) end end |
#my_end_day_is_before_o_start_day?(my, o) ⇒ Boolean
441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 441 def my_end_day_is_before_o_start_day?(my, o) if !my.end.nil? (o.start[:month] < my.end[:month] || (o.start[:month] == my.end[:month] && o.start[:day] <= my.end[:day])) else (o.start[:month] < my.start[:month] || (o.start[:month] == my.start[:month] && o.start[:day] <= my.start[:day])) end end |
#my_end_is_after_o_start?(my, o) ⇒ Boolean
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 357 def my_end_is_after_o_start?(my, o) result = false if has_start_year?(o) && has_start_year?(my) result = if my.end o.start[:year] < my.end[:year] || (o.start[:year] == my.end[:year] && my_end_day_is_after_o_start_day?(my, o)) else o.start[:year] < my.start[:year] || (o.start[:year] == my.start[:year] && my_end_day_is_after_o_start_day?(my, o)) end elsif !has_start_year?(o) && !has_start_year?(my) result = my_end_day_is_after_o_start_day?(my, o) end result end |
#my_end_is_before_o_end?(my, o) ⇒ Boolean
326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 326 def my_end_is_before_o_end?(my, o) result = false if !my.end.nil? && !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] && my_end_day_is_before_o_end_day?(my, o)) elsif !has_end_year?(o) && !has_end_year?(my) result = my_end_day_is_before_o_end_day?(my, o) end else my_start_is_before_o_start?(my, o) end result end |
#my_start_day_is_after_o_end_day?(my, o) ⇒ Boolean
397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 397 def my_start_day_is_after_o_end_day?(my, o) if !o.end.nil? (o.end[:month] < my.start[:month] || (o.end[:month] == my.start[:month] && o.end[:day] <= my.start[:day])) else (o.start[:month] < my.start[:month] || (o.start[:month] == my.start[:month] && o.start[:day] <= my.start[:day])) end end |
#my_start_day_is_after_o_start_day?(my, o) ⇒ Boolean
373 374 375 376 377 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 373 def my_start_day_is_after_o_start_day?(my, o) (o.start[:month] < my.start[:month] || (o.start[:month] == my.start[:month] && o.start[:day] <= my.start[:day])) end |
#my_start_day_is_before_o_end_day?(my, o) ⇒ Boolean
385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 385 def my_start_day_is_before_o_end_day?(my, o) if !o.end.nil? (o.end[:month] > my.start[:month] || (o.end[:month] == my.start[:month] && o.end[:day] >= my.start[:day])) else (o.start[:month] > my.start[:month] || (o.start[:month] == my.start[:month] && o.start[:day] >= my.start[:day])) end end |
#my_start_day_is_before_o_start_day?(my, o) ⇒ Boolean
379 380 381 382 383 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 379 def my_start_day_is_before_o_start_day?(my, o) (o.start[:month] > my.start[:month] || (o.start[:month] == my.start[:month] && o.start[:day] >= my.start[:day])) end |
#my_start_is_after_o_start?(my, o) ⇒ Boolean
304 305 306 307 308 309 310 311 312 313 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 304 def my_start_is_after_o_start?(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] && my_start_day_is_after_o_start_day?(my, o)) elsif !has_start_year?(o) && !has_start_year?(my) result = my_start_day_is_after_o_start_day?(my, o) end result end |
#my_start_is_before_o_end?(my, o) ⇒ Boolean
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 341 def my_start_is_before_o_end?(my, o) result = false if has_start_year?(o) && has_start_year?(my) result = if o.end o.end[:year] > my.start[:year] || (o.end[:year] == my.start[:year] && my_start_day_is_before_o_end_day?(my, o)) else o.start[:year] > my.start[:year] || (o.start[:year] == my.start[:year] && my_start_day_is_before_o_end_day?(my, o)) end elsif !has_start_year?(o) && !has_start_year?(my) result = my_start_day_is_before_o_end_day?(my, o) end result end |
#my_start_is_before_o_start?(my, o) ⇒ Boolean
315 316 317 318 319 320 321 322 323 324 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 315 def my_start_is_before_o_start?(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] && my_start_day_is_before_o_start_day?(my, o)) elsif !has_start_year?(o) && !has_start_year?(my) result = my_start_day_is_before_o_start_day?(my, o) end result end |
#resolve_variable_date(bound, year) ⇒ Object
759 760 761 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 759 def resolve_variable_date(bound, year) OpeningHoursConverter::WeekIndex.nth_wday_of_month(bound[:index], bound[:weekday], bound[:month], year) end |
#sort_days_array(days_array, by = :start) ⇒ Object
459 460 461 462 463 464 465 466 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 459 def sort_days_array(days_array, by = :start) if days_array.all? { |day| day.start && !day.start[:year].nil? } days_array.sort_by { |day| [day.send(by)[:year], day.send(by)[:month], day.send(by)[:day]] } else days_array.sort_by { |day| [day.send(by)[:month], day.send(by)[:day]] } end end |
#starts_month? ⇒ Boolean
214 215 216 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 214 def starts_month? @type == 'month' || @type == 'always' || @type == 'year' || (@type == 'day' && @start[:day] == 1) end |
#starts_year? ⇒ Boolean
236 237 238 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 236 def starts_year? @type == 'year' || @type == 'always' || (@type == 'day' && @start[:day] == 1 && @start[:month] == 1) || (@type == 'month' && @start[:month] == 1) end |
#to_day ⇒ Object
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 656 def to_day # les semaines rentrent en opposition avec cette maniere de faire : il n'y a pas un debut et une fin il y en a plusieurs (1-53/2) 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 when 'variable_day' get_variable_days_for_year.first when 'holiday' if @start && @start[:year] if @end && @end[:year] weeks = [] (@start[:year]..@end[:year]).each do |year| weeks += get_public_holidays_for_year(year) end weeks else get_public_holidays_for_year(@start[:year]) end else get_public_holidays_for_year end when 'week' if @start && @start[:year] if @end && @end[:year] weeks = [] (@start[:year]..@end[:year]).each do |year| weeks += get_weeks_for_year(year) end weeks else get_weeks_for_year(@start[:year]) end else get_weeks_for_year end end end |
#touch?(o) ⇒ Boolean
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 270 def touch?(o) return true if contains?(o) return true if o.type == 'always' || @type == 'always' result = false if equals(o) result = true else my = to_day o = o.to_day if my.is_a?(Array) if o.is_a?(Array) return my.any? do |day| o.any? do |o_day| day.touch?(o_day) end end else return my.any? { |day| day.touch?(o) } end elsif o.is_a?(Array) return o.any? { |day| day.touch?(my) } end # solution poour les weeks, on peut convertir un wide interval week en array de wide interval days et comparer les wide interval week # un par un (long si deux wide interval multiweek) # problem : comment calculer les weeks always ? comparer sur l'année en cours si o est always, les années de o sinon result = ((my_start_is_before_o_end?(my, o) && my_start_is_after_o_start?(my, o)) || (my_end_is_before_o_end?(my, o) && my_end_is_after_o_start?(my, o))) || ((my_start_is_before_o_end?(o, my) && my_start_is_after_o_start?(o, my)) || (my_end_is_before_o_end?(o, my) && my_end_is_after_o_start?(o, my))) end result end |
#variable_date_selector(bound) ⇒ Object
54 55 56 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 54 def variable_date_selector(bound) "#{OSM_MONTHS[bound[:month] - 1]} #{OSM_DAYS[bound[:weekday]]}[#{bound[:index]}]" end |
#variable_day(start_date, end_date) ⇒ Object
186 187 188 189 190 191 192 193 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 186 def variable_day(start_date, end_date) raise(ArgumentError, 'start_date and end_date are required') if start_date.nil? || end_date.nil? @start = start_date @end = end_date @type = 'variable_day' self end |
#week(week_indexes, start_year = nil, end_year = nil) ⇒ Object
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 146 def week(week_indexes, start_year = nil, end_year = nil) raise(ArgumentError, 'weeks are required (array of ints)') if week_indexes.nil? || !week_indexes.is_a?(Array) @start = { year: start_year } @indexes = week_indexes if !start_year.nil? && !end_year.nil? && end_year != start_year @end = { year: end_year } end @type = 'week' self end |
#week_as_day(index, year, weeks_as_days) ⇒ Object
A year has 52 or 53 ISO weeks, so an index past the last one selects nothing rather than a week borrowed from the next year.
735 736 737 738 739 740 741 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 735 def week_as_day(index, year, weeks_as_days) return if index > OpeningHoursConverter::WeekIndex.week_count(year) week = OpeningHoursConverter::WeekIndex.week_from_index(index, year) weeks_as_days << OpeningHoursConverter::WideInterval.new.day(week[:from].day, week[:from].month, week[:from].year, week[:to].day, week[:to].month, week[:to].year) end |
#width ⇒ Object
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 572 def width return Float::INFINITY if @type == 'always' in_day = to_day days_count = 0 return in_day.map(&:width).sum if in_day.is_a?(Array) if in_day.end if in_day.start[:year] && in_day.end[:year] if in_day.start[:year] != in_day.end[:year] for year in in_day.start[:year]..in_day.end[:year] if year == in_day.start[:year] for month in in_day.start[:month]..12 days_count += if month == in_day.start[:month] MONTH_END_DAY[month - 1] - in_day.start[:day] + 1 else MONTH_END_DAY[month - 1] end end elsif year == in_day.end[:year] for month in 1..in_day.end[:month] days_count += if month == in_day.end[:month] in_day.end[:day] else MONTH_END_DAY[month - 1] end end else for month in 1..12 days_count += MONTH_END_DAY[month - 1] end end end else if in_day.start[:month] == in_day.end[:month] days_count += in_day.end[:day] - in_day.start[:day] + 1 else for month in in_day.start[:month]..in_day.end[:month] days_count += if month == in_day.end[:month] in_day.end[:day] elsif month == in_day.start[:month] MONTH_END_DAY[month - 1] - in_day.start[:day] + 1 else MONTH_END_DAY[month - 1] end end end end else if in_day.start[:month] == in_day.end[:month] days_count += in_day.end[:day] - in_day.start[:day] + 1 elsif in_day.start[:month] > in_day.end[:month] for month in in_day.start[:month]..12 days_count += if month == in_day.start[:month] MONTH_END_DAY[month - 1] - in_day.start[:day] + 1 else MONTH_END_DAY[month - 1] end end for month in 1..in_day.end[:month] days_count += if month == in_day.end[:month] in_day.end[:day] else MONTH_END_DAY[month - 1] end end else for month in in_day.start[:month]..in_day.end[:month] days_count += if month == in_day.end[:month] in_day.end[:day] elsif month == in_day.start[:month] MONTH_END_DAY[month - 1] - in_day.start[:day] + 1 else MONTH_END_DAY[month - 1] end end end end return days_count else return 1 end end |
#year(start_year, end_year = nil, open_ended: false, step: nil) ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'lib/opening_hours_converter/wide_interval.rb', line 157 def year(start_year, end_year = nil, open_ended: false, step: nil) raise(ArgumentError, 'start_year is required') if start_year.nil? @start = { year: start_year } @end = { year: end_year } unless end_year.nil? || end_year == start_year @open_ended = open_ended @step = step @type = 'year' self end |