Class: OpeningHoursConverter::OpeningHoursRule
- Inherits:
-
Object
- Object
- OpeningHoursConverter::OpeningHoursRule
- Includes:
- Constants
- Defined in:
- lib/opening_hours_converter/opening_hours_rule.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
-
#comment ⇒ Object
Returns the value of attribute comment.
-
#date ⇒ Object
Returns the value of attribute date.
-
#time ⇒ Object
Returns the value of attribute time.
Instance Method Summary collapse
- #add_comment(comment) ⇒ Object
- #add_date(date) ⇒ Object
- #add_overwritten_weekday(weekday) ⇒ Object
- #add_ph_weekday ⇒ Object
- #add_time(time) ⇒ Object
- #add_weekday(weekday) ⇒ Object
- #ends_month?(r) ⇒ Boolean
- #ends_year?(r) ⇒ Boolean
- #equals(o) ⇒ Object
- #get ⇒ Object
- #get_wide_selector ⇒ Object
- #has_overwritten_weekday? ⇒ Boolean
- #include_time?(time) ⇒ Boolean
-
#initialize ⇒ OpeningHoursRule
constructor
A new instance of OpeningHoursRule.
- #is_full_month?(r) ⇒ Boolean
- #is_full_year?(r) ⇒ Boolean
- #is_off? ⇒ Boolean
- #is_same_month?(r) ⇒ Boolean
- #is_same_year?(r) ⇒ Boolean
- #result_to_string(result) ⇒ Object
- #same_date?(o) ⇒ Boolean
- #same_time?(o) ⇒ Boolean
- #starts_month?(r) ⇒ Boolean
- #starts_year?(r) ⇒ Boolean
Constructor Details
#initialize ⇒ OpeningHoursRule
Returns a new instance of OpeningHoursRule.
8 9 10 11 12 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 8 def initialize @date = [] @time = [] @comment = "" end |
Instance Attribute Details
#comment ⇒ Object
Returns the value of attribute comment.
6 7 8 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 6 def comment @comment end |
#date ⇒ Object
Returns the value of attribute date.
6 7 8 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 6 def date @date end |
#time ⇒ Object
Returns the value of attribute time.
6 7 8 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 6 def time @time end |
Instance Method Details
#add_comment(comment) ⇒ Object
315 316 317 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 315 def add_comment(comment) @comment = comment end |
#add_date(date) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 287 def add_date(date) if date.nil? || !date.instance_of?(OpeningHoursConverter::OpeningHoursDate) raise ArgumentError end if @date.length == 0 || date.same_weekdays?(@date.first.weekdays) @date << date else if !@date.first.same_weekdays?(date.weekdays) raise ArgumentError, "This date #{@date.inspect} can't be added to this rule #{self.inspect}" end end end |
#add_overwritten_weekday(weekday) ⇒ Object
281 282 283 284 285 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 281 def add_overwritten_weekday(weekday) @date.each do |d| d.add_overwritten_weekday(weekday) end end |
#add_ph_weekday ⇒ Object
275 276 277 278 279 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 275 def add_ph_weekday @date.each do |d| d.add_ph_weekday end end |
#add_time(time) ⇒ Object
308 309 310 311 312 313 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 308 def add_time(time) if (@time.length == 0 || @time[0].get != "off") && !include_time?(time) @time << time end end |
#add_weekday(weekday) ⇒ Object
269 270 271 272 273 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 269 def add_weekday(weekday) @date.each do |d| d.add_weekday(weekday) end end |
#ends_month?(r) ⇒ Boolean
222 223 224 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 222 def ends_month?(r) r[:end][:day] == MONTH_END_DAY[r[:end][:month]] - 1 end |
#ends_year?(r) ⇒ Boolean
230 231 232 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 230 def ends_year?(r) r[:end][:month] == 11 && ends_month?(r) end |
#equals(o) ⇒ Object
256 257 258 259 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 256 def equals(o) return false unless o.instance_of?(OpeningHoursConverter::OpeningHoursRule) (same_time?(o) && same_date?(o)) end |
#get ⇒ 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 49 50 51 52 53 54 55 56 57 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 14 def get result = "" if @date.length == 1 && @date[0].wide_type == "holiday" if !@date[0].wide.start[:year].nil? result += @date[0].wide.start[:year] end if !@date[0].wide.end.nil? result += "-#{@date[0].wide.end[:year]}" end result += " PH" elsif @date.length > 0 result += get_wide_selector end if @date.length > 0 wd = @date[0].get_weekdays if wd.length > 0 result += " #{wd}" end end if @time.length > 0 result += " " @time.uniq.each_with_index do |t, i| if (i > 0) result += "," end result += t.get end else result += " off" end rgx_day = /(Mo|Tu|We|Th|Fr|Sa|Su)/ if result.strip == "00:00-24:00" || (!(result =~ /00:00-24:00/).nil? && (result =~ rgx_day).nil?) result.gsub!("00:00-24:00", "24/7") end if !comment.nil? && comment.length != 0 result += " #{comment}" end result.strip end |
#get_wide_selector ⇒ Object
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 59 def get_wide_selector years = OpeningHoursConverter::Year.build_day_array_from_dates(@date) year_start = -1 month_start = -1 day_start = -1 result = {} if !years["always"].nil? always = years.delete("always") always.each_with_index do |month_array, month| month_array.each_with_index do |day_bool, day| if day_bool && month_start < 0 month_start = month day_start = day elsif day_bool && month_start >= 0 && month == 11 && day == 30 result["always"] ||= [] result["always"] << {start: { day: day_start, month: month_start }, end: { day: 30, month: 11 }} month_start = -1 day_start = -1 elsif !day_bool && month_start >= 0 result["always"] ||= [] end_res = day == 0 ? month == 0 ? { day: 30, month: 11 } : { day: MONTH_END_DAY[month-1]-1, month: month-1 } : { day: day-1, month: month } result["always"] << { start: { day: day_start, month: month_start }, end: end_res } month_start = -1 day_start = -1 end end end end years.each do |year, months| months.each_with_index do |month_array, month| month_array.each_with_index do |day_bool, day| if day_bool && year_start < 0 year_start = year month_start = month day_start = day elsif day_bool && year_start >= 0 && month == 11 && day == 30 && years[year+1].nil? if year_start == year result[year] ||= [] result[year] << { start: { day: day_start, month: month_start }, end: { day: 30, month: 11 } } else result["multi_year"] ||= {} result["multi_year"]["#{year_start}-#{year}"] ||= [] result["multi_year"]["#{year_start}-#{year}"] << { start: { day: day_start, month: month_start, year: year_start }, end: { day: 30, month: 11, year: year } } end year_start = -1 month_start = -1 day_start = -1 elsif !day_bool && year_start >= 0 end_res = {} if day == 0 if month == 0 end_res = { day: 30, month: 11 } else end_res = { day: MONTH_END_DAY[month-1]-1, month: month-1 } end else end_res = { day: day-1, month: month } end if year_start == year result[year] ||= [] result[year] << { start: { day: day_start, month: month_start }, end: end_res } else result["multi_year"] ||= {} result["multi_year"]["#{year_start}-#{year}"] ||= [] end_res[:year] = year result["multi_year"]["#{year_start}-#{year}"] << { start: { day: day_start, month: month_start, year: year_start }, end: end_res } end year_start = -1 month_start = -1 day_start = -1 end end end end result_to_string(result) end |
#has_overwritten_weekday? ⇒ Boolean
265 266 267 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 265 def has_overwritten_weekday? @date.length > 0 && @date[0].weekdays_over.length > 0 end |
#include_time?(time) ⇒ Boolean
301 302 303 304 305 306 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 301 def include_time?(time) @time.each do |t| return true if t.start == time.start && t.end == time.end end return false end |
#is_full_month?(r) ⇒ Boolean
206 207 208 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 206 def is_full_month?(r) is_same_month?(r) && starts_month?(r) && ends_month?(r) end |
#is_full_year?(r) ⇒ Boolean
202 203 204 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 202 def is_full_year?(r) starts_year?(r) && ends_year?(r) end |
#is_off? ⇒ Boolean
261 262 263 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 261 def is_off? @time.length == 0 || (@time.length == 1 && time[0].start.nil?) end |
#is_same_month?(r) ⇒ Boolean
214 215 216 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 214 def is_same_month?(r) r[:start][:month] == r[:end][:month] end |
#is_same_year?(r) ⇒ Boolean
210 211 212 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 210 def is_same_year?(r) r[:start][:year] == r[:end][:year] end |
#result_to_string(result) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 148 def result_to_string(result) str_result = "" result.each do |selector, intervals| if selector == "always" intervals.each do |interval| if str_result.length > 0 str_result += "," end if is_full_year?(interval) elsif is_full_month?(interval) str_result += "#{OSM_MONTHS[interval[:start][:month]]}" elsif starts_month?(interval) && ends_month?(interval) str_result += "#{OSM_MONTHS[interval[:start][:month]]}-#{OSM_MONTHS[interval[:end][:month]]}" elsif is_same_month?(interval) str_result += "#{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day]+1 < 10 ? "0#{interval[:start][:day]+1}" : interval[:start][:day]+1}-#{interval[:end][:day]+1 < 10 ? "0#{interval[:end][:day]+1}" : interval[:end][:day]+1}" else str_result += "#{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day]+1 < 10 ? "0#{interval[:start][:day]+1}" : interval[:start][:day]+1}-#{OSM_MONTHS[interval[:end][:month]]} #{interval[:end][:day]+1 < 10 ? "0#{interval[:end][:day]+1}" : interval[:end][:day]+1}" end end elsif selector == "multi_year" intervals.each do |years, intervals| intervals.each do |interval| if str_result.length > 0 str_result += "," end if starts_year?(interval) && ends_year?(interval) str_result += "#{interval[:start][:year]}-#{interval[:end][:year]}" elsif starts_month?(interval) && ends_month?(interval) str_result += "#{interval[:start][:year]} #{OSM_MONTHS[interval[:start][:month]]}-#{interval[:end][:year]} #{OSM_MONTHS[interval[:end][:month]]}" else str_result += "#{interval[:start][:year]} #{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day]+1 < 10 ? "0#{interval[:start][:day]+1}" : interval[:start][:day]+1}-#{interval[:end][:year]} #{OSM_MONTHS[interval[:end][:month]]} #{interval[:end][:day]+1 < 10 ? "0#{interval[:end][:day]+1}" : interval[:end][:day]+1}" end end end else str_result += "#{selector} " intervals.each do |interval| if is_full_year?(interval) elsif is_full_month?(interval) str_result += "#{str_result.length > 5 ? "," : ""}#{OSM_MONTHS[interval[:start][:month]]}" elsif starts_month?(interval) && ends_month?(interval) str_result += "#{str_result.length > 5 ? "," : ""}#{OSM_MONTHS[interval[:start][:month]]}-#{OSM_MONTHS[interval[:end][:month]]}" elsif is_same_month?(interval) str_result += "#{str_result.length > 5 ? "," : ""}#{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day]+1 < 10 ? "0#{interval[:start][:day]+1}" : interval[:start][:day]+1}-#{interval[:end][:day]+1 < 10 ? "0#{interval[:end][:day]+1}" : interval[:end][:day]+1}" else str_result += "#{str_result.length > 5 ? "," : ""}#{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day]+1 < 10 ? "0#{interval[:start][:day]+1}" : interval[:start][:day]+1}-#{OSM_MONTHS[interval[:end][:month]]} #{interval[:end][:day]+1 < 10 ? "0#{interval[:end][:day]+1}" : interval[:end][:day]+1}" end end end end str_result.strip end |
#same_date?(o) ⇒ Boolean
245 246 247 248 249 250 251 252 253 254 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 245 def same_date?(o) if o.nil? || o.date.length != @date.length return false else @date.each_with_index do |d, i| return false if !d.wide.equals(o.date[i].wide) end return true end end |
#same_time?(o) ⇒ Boolean
234 235 236 237 238 239 240 241 242 243 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 234 def same_time?(o) if o.nil? || o.time.length != @time.length return false else @time.each_with_index do |t, i| return false if !t.equals(o.time[i]) end return true end end |
#starts_month?(r) ⇒ Boolean
218 219 220 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 218 def starts_month?(r) r[:start][:day] == 0 end |
#starts_year?(r) ⇒ Boolean
226 227 228 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 226 def starts_year?(r) r[:start][:month] == 0 && starts_month?(r) end |