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.
-
#is_defined_off ⇒ Object
Returns the value of attribute is_defined_off.
-
#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_only_week_interval?(r) ⇒ Boolean
- #is_same_day?(r) ⇒ Boolean
- #is_same_month?(r) ⇒ Boolean
- #is_same_year?(r) ⇒ Boolean
- #is_week_interval?(interval) ⇒ 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 13 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 8 def initialize @date = [] @time = [] @is_defined_off = false @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 |
#is_defined_off ⇒ Object
Returns the value of attribute is_defined_off.
6 7 8 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 6 def is_defined_off @is_defined_off 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
349 350 351 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 349 def add_comment(comment) @comment = comment end |
#add_date(date) ⇒ Object
324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 324 def add_date(date) if date.nil? || !date.instance_of?(OpeningHoursConverter::OpeningHoursDate) raise ArgumentError end if @date.empty? || 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 #{inspect}" end end end |
#add_overwritten_weekday(weekday) ⇒ Object
318 319 320 321 322 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 318 def add_overwritten_weekday(weekday) @date.each do |d| d.add_overwritten_weekday(weekday) end end |
#add_ph_weekday ⇒ Object
314 315 316 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 314 def add_ph_weekday @date.each(&:add_ph_weekday) end |
#add_time(time) ⇒ Object
345 346 347 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 345 def add_time(time) @time << time if (@time.empty? || @time[0].get != 'off') && !include_time?(time) end |
#add_weekday(weekday) ⇒ Object
308 309 310 311 312 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 308 def add_weekday(weekday) @date.each do |d| d.add_weekday(weekday) end end |
#ends_month?(r) ⇒ Boolean
261 262 263 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 261 def ends_month?(r) r[:end][:day] == MONTH_END_DAY[r[:end][:month]] - 1 end |
#ends_year?(r) ⇒ Boolean
269 270 271 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 269 def ends_year?(r) r[:end][:month] == 11 && ends_month?(r) end |
#equals(o) ⇒ Object
295 296 297 298 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 295 def equals(o) return false unless o.instance_of?(OpeningHoursConverter::OpeningHoursRule) (same_time?(o) && same_date?(o)) end |
#get ⇒ Object
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 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 15 def get result = '' result += get_wide_selector if !@date.empty? if !@date.empty? wd = @date[0].get_weekdays result += " #{wd}" if !wd.empty? end if @is_defined_off result += ' off' elsif !@time.empty? result += ' ' @time.uniq.each_with_index do |t, i| result += ',' if i > 0 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 result += " #{comment}" if !comment.nil? && !comment.empty? result.strip end |
#get_wide_selector ⇒ Object
47 48 49 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 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 47 def get_wide_selector if @date.length == 1 && @date[0].wide_interval.type == 'holiday' if @date[0].wide_interval.start[:year].nil? return 'PH' else if @date[0].wide_interval.end && @date[0].wide_interval.end[:year] return "#{@date[0].wide_interval.start[:year]}-#{@date[0].wide_interval.end[:year]} PH" else return "#{@date[0].wide_interval.start[:year]} PH" end end end years = OpeningHoursConverter::Year.build_day_array_from_dates(@date) year_start = -1 month_start = -1 day_start = -1 return result_to_string(years) if is_only_week_interval?(years) 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 = {} end_res = if day == 0 if month == 0 { day: 30, month: 11 } else { day: MONTH_END_DAY[month - 1] - 1, month: month - 1 } end else { 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
304 305 306 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 304 def has_overwritten_weekday? !@date.empty? && !@date[0].weekdays_over.empty? end |
#include_time?(time) ⇒ Boolean
338 339 340 341 342 343 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 338 def include_time?(time) @time.each do |t| return true if t.start == time.start && t.end == time.end end false end |
#is_full_month?(r) ⇒ Boolean
241 242 243 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 241 def is_full_month?(r) is_same_month?(r) && starts_month?(r) && ends_month?(r) end |
#is_full_year?(r) ⇒ Boolean
237 238 239 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 237 def is_full_year?(r) starts_year?(r) && ends_year?(r) end |
#is_off? ⇒ Boolean
300 301 302 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 300 def is_off? @time.empty? || (@time.length == 1 && time[0].start.nil?) end |
#is_only_week_interval?(r) ⇒ Boolean
226 227 228 229 230 231 232 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 226 def is_only_week_interval?(r) r.all? do |year, intervals| intervals.all? do |interval| is_week_interval?(interval) end end end |
#is_same_day?(r) ⇒ Boolean
253 254 255 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 253 def is_same_day?(r) r[:start][:day] == r[:end][:day] end |
#is_same_month?(r) ⇒ Boolean
249 250 251 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 249 def is_same_month?(r) r[:start][:month] == r[:end][:month] end |
#is_same_year?(r) ⇒ Boolean
245 246 247 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 245 def is_same_year?(r) r[:start][:year] == r[:end][:year] end |
#is_week_interval?(interval) ⇒ Boolean
233 234 235 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 233 def is_week_interval?(interval) !interval.is_a?(Array) && interval.key?(:start) && interval[:start].key?(:week) 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 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 151 def result_to_string(result) str_result = '' if is_only_week_interval?(result) local_str = result.keys == ['always'] ? 'week ' : result.keys.length == 1 ? result.keys.first.to_s + ' week ' : result.keys.min.to_s + '-' + result.keys.max.to_s + ' week ' intervals = result[result.keys.first] intervals.each do |interval| local_str += "," if !(local_str[local_str.length - 1] =~ /^[0-9]$/).nil? local_str += if interval[:modifier].nil? if interval[:start][:week] == interval[:end][:week] interval[:start][:week].to_s else "#{interval[:start][:week].to_s + '-' + interval[:end][:week].to_s}" end else "#{interval[:start][:week].to_s + '-' + interval[:end][:week].to_s}/#{interval[:modifier].to_s}" end end str_result += local_str else result.each do |selector, intervals| if selector == 'always' intervals.each do |interval| str_result += ',' if !str_result.empty? if is_full_year?(interval) elsif starts_year?(interval) && ends_year?(interval) str_result += "#{interval[:start][:year]}-#{interval[:end][:year]}" elsif is_full_month?(interval) str_result += (OSM_MONTHS[interval[:start][:month]]).to_s 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 elsif starts_month?(interval) && ends_month?(interval) str_result += "#{OSM_MONTHS[interval[:start][:month]]}-#{OSM_MONTHS[interval[:end][:month]]}" 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| str_result += ',' if !str_result.empty? if starts_year?(interval) && ends_year?(interval) str_result += "#{interval[:start][:year]}-#{interval[:end][:year]}" 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 local_str = "#{selector} " intervals.each do |interval| if is_full_year?(interval) elsif is_same_month?(interval) if is_same_day?(interval) local_str += "#{local_str.length > 5 ? ',' : ''}#{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day] + 1 < 10 ? "0#{interval[:start][:day] + 1}" : interval[:start][:day] + 1}" else local_str += "#{local_str.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 local_str += "#{local_str.length > 5 ? ',' : ''}#{OSM_MONTHS[interval[:start][:month]]} #{interval[:start][:day] + 1 < 10 ? "0#{interval[:start][:day] + 1}" : interval[:start][:day] + 1}-#{selector} #{OSM_MONTHS[interval[:end][:month]]} #{interval[:end][:day] + 1 < 10 ? "0#{interval[:end][:day] + 1}" : interval[:end][:day] + 1}" end str_result += "#{!str_result.empty? ? ',' : ''}#{local_str}" end end end end str_result.strip end |
#same_date?(o) ⇒ Boolean
284 285 286 287 288 289 290 291 292 293 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 284 def same_date?(o) if o.nil? || o.date.length != @date.length false else @date.each_with_index do |d, i| return false if !d.wide_interval.equals(o.date[i].wide_interval) end true end end |
#same_time?(o) ⇒ Boolean
273 274 275 276 277 278 279 280 281 282 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 273 def same_time?(o) if o.nil? || o.time.length != @time.length || @is_defined_off != o.is_defined_off false else @time.each_with_index do |t, i| return false if !t.equals(o.time[i]) end true end end |
#starts_month?(r) ⇒ Boolean
257 258 259 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 257 def starts_month?(r) r[:start][:day] == 0 end |
#starts_year?(r) ⇒ Boolean
265 266 267 |
# File 'lib/opening_hours_converter/opening_hours_rule.rb', line 265 def starts_year?(r) r[:start][:month] == 0 && starts_month?(r) end |