Class: OpeningHoursConverter::OpeningHoursDate

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/opening_hours_converter/opening_hours_date.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::YEAR_DAYS_MAX

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wide, wide_type, weekdays) ⇒ OpeningHoursDate

Returns a new instance of OpeningHoursDate.



8
9
10
11
12
13
14
15
16
17
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 8

def initialize(wide, wide_type, weekdays)
  if wide.nil? || wide_type.nil? || weekdays.nil?
    raise ArgumentError
  end

  @wide = wide
  @wide_type = wide_type
  @weekdays = weekdays.sort
  @weekdays_over = []
end

Instance Attribute Details

#weekdaysObject

Returns the value of attribute weekdays.



6
7
8
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 6

def weekdays
  @weekdays
end

#weekdays_overObject

Returns the value of attribute weekdays_over.



6
7
8
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 6

def weekdays_over
  @weekdays_over
end

#wideObject

Returns the value of attribute wide.



6
7
8
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 6

def wide
  @wide
end

#wide_typeObject

Returns the value of attribute wide_type.



6
7
8
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 6

def wide_type
  @wide_type
end

Instance Method Details

#add_overwritten_weekday(weekday) ⇒ Object



107
108
109
110
111
112
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 107

def add_overwritten_weekday(weekday)
  unless @weekdays_over.include?(weekday) && @weekdays_over.include?(weekday)
    @weekdays_over << weekday
    @weekdays_over.sort!
  end
end

#add_weekday(weekday) ⇒ Object



100
101
102
103
104
105
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 100

def add_weekday(weekday)
  if !@weekdays.include?(weekday) && !@weekdays_over.include?(weekday)
    @weekdays << weekday
    @weekdays.sort!
  end
end

#equals(o) ⇒ Object



122
123
124
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 122

def equals(o)
  o.instance_of?(OpeningHoursConverter::OpeningHoursDate) && @wide_type == o.wide_type && @wide == o.wide && o.same_weekdays?(@weekdays)
end

#get_weekdaysObject



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
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
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 19

def get_weekdays

  result = ""
  wd = @weekdays.concat(@weekdays_over).sort.uniq

  if wd.length > 0 && wd.include?(6) && wd.include?(0) && (wd.include?(5) || wd.include?(1))
    start_we = 6
    i = wd.length - 2
    stop_looking = false

    while !stop_looking && i >= 0
      if wd[i] == wd[i+1] - 1
        start_we = wd[i]
        i -= 1
      else
        stop_looking = true
      end
    end

    i = 1
    stop_looking = false
    end_we = 0

    while !stop_looking && i < wd.length
      if wd[i-1] == wd[i] - 1
        end_we = wd[i]
        i += 1
      else
        stop_looking = true
      end
    end

    length = 7 - start_we + end_we + 1

    if length >= 3 && start_we > end_we
      if result.length > 0
        result += ","
      end
      result += "#{OSM_DAYS[start_we]}-#{OSM_DAYS[end_we]}"

      j=0
      while j < wd.length
        if wd[j] <= end_we || wd[j] >= start_we
          wd.slice!(j, 1)
        else
          j+=1
        end
      end
    end
  end

  if wd.length > 1 || (wd.length == 1 && wd[0] != -1)
    result += result.length > 0 ? ",#{OSM_DAYS[wd[0]]}" : OSM_DAYS[wd[0]]
    first_in_row = wd[0]
    for i in 1...wd.length
      if wd[i-1] != wd[i] - 1
        if first_in_row != wd[i-1]
          if wd[i-1] - first_in_row == 1
            result += ",#{OSM_DAYS[wd[i-1]]}"
          else
            result += "-#{OSM_DAYS[wd[i-1]]}"
          end
        end
        result += ",#{OSM_DAYS[wd[i]]}"
        first_in_row = wd[i]
      elsif i == wd.length - 1
        if wd[i] - first_in_row == 1
          result += ",#{OSM_DAYS[wd[i]]}"
        else
          result += "-#{OSM_DAYS[wd[i]]}"
        end
      end
    end
  end

  if result == "Mo-Su"
    result = ""
  end
  return result
end

#same_kind_as?(date) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 114

def same_kind_as?(date)
  @wide_type == date.wide_type && date.same_weekdays?(@weekdays)
end

#same_weekdays?(weekdays) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/opening_hours_converter/opening_hours_date.rb', line 118

def same_weekdays?(weekdays)
  weekdays == @weekdays
end