Class: OpeningHoursConverter::WideInterval

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

#initializeWideInterval

Returns a new instance of WideInterval.



8
9
10
11
12
# File 'lib/opening_hours_converter/wide_interval.rb', line 8

def initialize
  @start = nil
  @end = nil
  @type = nil
end

Instance Attribute Details

#endObject

Returns the value of attribute end.



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

def end
  @end
end

#startObject

Returns the value of attribute start.



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

def start
  @start
end

#typeObject

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

#alwaysObject



112
113
114
115
116
117
# File 'lib/opening_hours_converter/wide_interval.rb', line 112

def always
  @start = nil
  @end = nil
  @type = "always"
  self
end

#contains?(o) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
141
142
143
144
145
146
147
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/opening_hours_converter/wide_interval.rb', line 138

def contains?(o)
  result = false
  if self.equals(o)
    result = false
  elsif @type == "always"
    result = true
  elsif @type ==  "day"
    if o.type == "day"
      if o.start[:month] > @start[:month] || (o.start[:month] == @start[:month] && o.start[:day] >= @start[:day])

        if !o.end.nil?
          if !@end.nil? && (o.end[:month] < @end[:month] || (o.end[:month] == @end[:month] && o.end[:day] <= @end[:day]))
            result = true
          end
        else
          if !@end.nil? && (o.start[:month] < @end[:month] || (o.start[:month] == @end[:month] && o.start[:day] <= @end[:day]))
            result = true
          end
        end

      end
    elsif o.type == "month"
      if o.start[:month] > @start[:month] || (o.start[:month] == @start[:month] && @start[:day] == 1)
        if !o.end.nil? && !@end.nil? && (o.end[:month] < @end[:month] || (o.end[:month] == @end[:month] && @end[:day] == MONTH_END_DAY[@end.month-1]))
          result = true
        elsif o.end.nil? && (!@end.nil? && o.start[:month] < @end[:month])
          result = true
        end
      end
    end
  elsif @type == "week"
    if o.type == "week"
      if o.start[:week] >= @start[:week]
        if !o.end.nil? && !@end.nil? && o.end[:week] <= @end[:week]
          result = true
        elsif o.end.nil? && ((!@end.nil? && o.start[:week] <= @end[:week]) || o.start[:week] == @start[:week])
          result = true
        end
      end
    end
  elsif @type == "month"
    if o.type == "month"
      if o.start[:month] >= @start[:month]
        if !o.end.nil? && !@end.nil? && o.end[:month] <= @end[:month]
          result = true
        elsif o.end.nil? && ((!@end.nil? && o.start[:month] <= @end[:month]) || o.start[:month] == @start[:month])
          result = true
        end
      end
    elsif o.type == "day"
      if !o.end.nil?
        if @end.nil?
          if o.start[:month] == @start[:month] &&
            o.end[:month] == @start[:month] &&
            ((o.start[:day] >= 1 && o.end[:day] < MONTH_END_DAY[o.start[:month]-1]) ||
            (o.start[:day] > 1 && o.end[:day] <= MONTH_END_DAY[o.start[:month]-1]))
            result = true
          end
        else
          if o.start[:month] >= @start[:month] && o.end[:month] <= @end[:month]
            if ((o.start[:month] > @start[:month] && o.end[:month] < @end[:month]) ||
              (o.start[:month] == @start[:month] && o.end[:month] < @end[:month] && o.start.day > 1) ||
              (o.start[:month] > @start[:month] && o.end[:month] == @end[:month] && o.end[:day] < MONTH_END_DAY[o.end[:month]-1]) ||
              (o.start[:day] >= 1 && o.end[:day] < MONTH_END_DAY[o.end[:month]-1]) ||
              (o.start[:day] > 1 && o.end[:day] <= MONTH_END_DAY[o.end[:month]-1]))
              result = true
            end
          end
        end
      else
        if @end.nil?
          if @start[:month] == o.start[:month]
            result = true
          end
        else
          if o.start[:month] >= @start[:month] && o.start[:month] <= @end[:month]
            result = true
          end
        end
      end
    end
  end
  return result
end

#day(start_day, start_month, end_day = nil, end_month = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/opening_hours_converter/wide_interval.rb', line 76

def day(start_day, start_month, end_day=nil, end_month=nil)
  if start_day.nil? || start_month.nil?
    raise(ArgumentError, "start_day and start_month are required")
  end
  @start = { day: start_day, month: start_month }
  if (!end_day.nil? && !end_month.nil? && (end_day != start_day || end_month != start_month))
    @end = { day: end_day, month: end_month }
  end
  @type = "day"
  self
end

#ends_month?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/opening_hours_converter/wide_interval.rb', line 134

def ends_month?
  @type == "month" || @type == "always" || (@type == "day" && !@end.nil? && @end[:day] == MONTH_END_DAY[@end[:month] - 1])
end

#equals(o) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/opening_hours_converter/wide_interval.rb', line 223

def equals(o)
  return false unless o.instance_of?(OpeningHoursConverter::WideInterval)
  return @type == "always" if o.type == "always"

  result = false
  case @type
  when "always"
    result = o.start.nil?
  when "day"
    result = ((o.type == "day" &&
      o.start[:month] == @start[:month] &&
      o.start[:day] == @start[:day] &&
      ((o.end.nil? && @end.nil?) ||
      (!o.end.nil? && !@end.nil? &&
        o.end[:month] == @end[:month] &&
        o.end[:day] == @end[:day]))) ||
      (o.type == "month" &&
      o.start[:month] == @start[:month] &&
      (o.is_full_month? && is_full_month?) ||
      (!o.end.nil? && !@end.nil? &&
        o.end[:month] == @end[:month] &&
        o.ends_month? && ends_month?)))
  when "week"
    result = (o.type == "week" &&
      o.start[:week] == @start[:week] &&
      (o.end == @end ||
        (!o.end.nil? && !@end.nil? && o.end[:week] == @end[:week])))
  when "month"
    result = (o.type == "day" &&
      o.start[:month] == @start[:month] &&
      (o.starts_month? &&
      (!o.end.nil? && @end.nil? && o.end[:month] == @start[:month] && o.ends_month?) ||
      (!o.end.nil? && !@end.nil? && o.end[:month] == @end[:month] && o.ends_month?))) ||
      (o.type == "month" &&
      o.start[:month] == @start[:month] &&
      ((o.end.nil? && @end.nil?) ||
      (!o.end.nil? && !@end.nil? && o.end[:month] == @end[:month])))
  end
  result
end

#get_time_for_humansObject



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

def get_time_for_humans
  result = ""

  case @type
  when "day"
    if !@end.nil?
      result = "toutes les semaines du #{@start[:day]} #{IRL_MONTHS[@start[:month] - 1]}"
      if @start[:month] != @end[:month]
        result += " au #{@end[:day]} #{IRL_MONTHS[@end[:month] - 1]}"
      end
    else
      result = "le #{@start[:day]} #{IRL_MONTHS[@start[:month] - 1]}"
    end
  when "week"
    if !@end.nil?
      result = "toutes les semaines de la semaine #{@start[:week]} à la semaine #{@end[:week]}"
    else
      result = "la semaine #{@start[:week]}"
    end
  when "month"
    if !@end.nil?
      result = "toutes les semaines de #{IRL_MONTHS[@start[:month] - 1]} à #{IRL_MONTHS[@endd[:month] - 1]}"
    else
      result = "toutes les semaines de #{IRL_MONTHS[@start[:month] - 1]}"
    end
  when "always"
    result = "toutes les semaines"
  end
  return result
end

#get_time_selectorObject



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

def get_time_selector
  result = ""

  case @type
  when "day"
    result = "#{OSM_MONTHS[@start[:month]-1]} #{@start[:day] < 10 ? "0" : ""}#{@start[:day]}"
    if !@end.nil?
      if @start[:month] == @end[:month]
        result += "-#{@end[:day] < 10 ? "0" : ""}#{@end[:day]}"
      else
        result += "-#{OSM_MONTHS[@end[:month]-1]} #{@end[:day] < 10 ? "0" : ""}#{@end[:day]}"
      end
    end
  when "week"
    result = "week #{@start[:week] < 10 ? "0" : ""}#{@start[:week]}"
    if !@end.nil?
      result += "-#{@end[:week] < 10 ? "0" : ""}#{@end[:week]}"
    end
  when "month"
    result = "#{OSM_MONTHS[@start[:month]-1]}"
    if !@end.nil?
      result += "-#{OSM_MONTHS[@end[:month]-1]}"
    end
  when "always"
    result = ""
  else
    result = ""
  end
  result
end

#is_full_month?Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
126
127
128
# File 'lib/opening_hours_converter/wide_interval.rb', line 119

def is_full_month?
  if @type == "month" && @end.nil?
    true
  elsif @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

#month(start_month, end_month = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/opening_hours_converter/wide_interval.rb', line 100

def month(start_month, end_month=nil)
  if start_month.nil?
    raise(ArgumentError, "start_month is required")
  end
  @start = { month: start_month }
  unless end_month.nil? || end_month == start_month
    @end = { month: end_month }
  end
  @type = "month"
  self
end

#starts_month?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/opening_hours_converter/wide_interval.rb', line 130

def starts_month?
  @type == "month" || @type == "always" || (@type == "day" && @start[:day] == 1)
end

#week(start_week, end_week = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/opening_hours_converter/wide_interval.rb', line 88

def week(start_week, end_week=nil)
  if start_week.nil?
    raise(ArgumentError, "start_week is required")
  end
  @start = { week: start_week }
  unless end_week.nil? || end_week == start_week
    @end = { week: end_week }
  end
  @type = "week"
  self
end