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_day?(r) ⇒ 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
330 331 332 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 330 def add_comment(comment) @comment = comment end |
#add_date(date) ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 302 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
296 297 298 299 300 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 296 def add_overwritten_weekday(weekday) @date.each do |d| d.add_overwritten_weekday(weekday) end end |
#add_ph_weekday ⇒ Object
290 291 292 293 294 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 290 def add_ph_weekday @date.each do |d| d.add_ph_weekday end end |
#add_time(time) ⇒ Object
323 324 325 326 327 328 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 323 def add_time(time) if (@time.length == 0 || @time[0].get != "off") && !include_time?(time) @time << time end end |
#add_weekday(weekday) ⇒ Object
284 285 286 287 288 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 284 def add_weekday(weekday) @date.each do |d| d.add_weekday(weekday) end end |
#ends_month?(r) ⇒ Boolean
237 238 239 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 237 def ends_month?(r) r[:end][:day] == MONTH_END_DAY[r[:end][:month]] - 1 end |
#ends_year?(r) ⇒ Boolean
245 246 247 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 245 def ends_year?(r) r[:end][:month] == 11 && ends_month?(r) end |
#equals(o) ⇒ Object
271 272 273 274 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 271 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].to_s end if !@date[0].wide.end.nil? result += "-#{@date[0].wide.end[:year].to_s}" 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 147 148 149 |
# 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 end if 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 end if 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
280 281 282 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 280 def has_overwritten_weekday? @date.length > 0 && @date[0].weekdays_over.length > 0 end |
#include_time?(time) ⇒ Boolean
316 317 318 319 320 321 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 316 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
217 218 219 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 217 def is_full_month?(r) is_same_month?(r) && starts_month?(r) && ends_month?(r) end |
#is_full_year?(r) ⇒ Boolean
213 214 215 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 213 def is_full_year?(r) starts_year?(r) && ends_year?(r) end |
#is_off? ⇒ Boolean
276 277 278 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 276 def is_off? @time.length == 0 || (@time.length == 1 && time[0].start.nil?) end |
#is_same_day?(r) ⇒ Boolean
229 230 231 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 229 def is_same_day?(r) r[:start][:day] == r[:end][:day] end |
#is_same_month?(r) ⇒ Boolean
225 226 227 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 225 def is_same_month?(r) r[:start][:month] == r[:end][:month] end |
#is_same_year?(r) ⇒ Boolean
221 222 223 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 221 def is_same_year?(r) r[:start][:year] == r[:end][:year] end |
#result_to_string(result) ⇒ Object
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 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 151 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) if is_same_day?(interval) str_result += "#{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day]+1 < 10 ? "0#{interval[:start][:day]+1}" : interval[:start][:day]+1}" else 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}" end 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) if is_same_day?(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}" 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}-#{interval[:end][:day]+1 < 10 ? "0#{interval[:end][:day]+1}" : interval[:end][:day]+1}" end 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
260 261 262 263 264 265 266 267 268 269 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 260 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
249 250 251 252 253 254 255 256 257 258 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 249 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
233 234 235 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 233 def starts_month?(r) r[:start][:day] == 0 end |
#starts_year?(r) ⇒ Boolean
241 242 243 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 241 def starts_year?(r) r[:start][:month] == 0 && starts_month?(r) end |