Class: Workhours::Week

Inherits:
Object
  • Object
show all
Defined in:
lib/workhours/week.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Week

Returns a new instance of Week.

Raises:



12
13
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
# File 'lib/workhours/week.rb', line 12

def initialize(options = {})
  DEFAULTS.each_pair do |k, v|
    instance_variable_set("@#{k}", v.dup)
  end
  options.each_pair do |k, v|
    instance_variable_set("@#{k}", v.dup)
  end

  @export_hours = {}

  if hours.empty?
    week.each do |wday|
      hours.push Workhours::Period.new(wday, open, close)
    end
  else
    @export_hours = hours.dup
    @hours = hours.map do |h|
      if h.is_a?(Workhours::Period)
        h
      else
        pr = h.split(' ')
        times = pr[1].split('-')
        beginning = Tod::TimeOfDay.parse(times[0])
        ending = Tod::TimeOfDay.parse(times[1])
        if ending < beginning
          if ending.second_of_day == 0
            Workhours::Period.new(pr[0], times[0], "0:00")
          else
            [Workhours::Period.new(pr[0], times[0], "0:00"), Workhours::Period.new(Workhours.next_day(pr[0]), "0:00", times[1])]
          end
        else
          Workhours::Period.new(pr[0], times[0], times[1])
        end
      end
    end.flatten
  end
  raise NoHoursError.new if @hours.empty?
end

Instance Method Details

#closes_at(time = Time.now) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/workhours/week.rb', line 103

def closes_at(time = Time.now)
  if is_closed?(time)
    nil
  else
    next_closing_time(time)
  end
end

#export(holidays: true) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/workhours/week.rb', line 51

def export(holidays: true)
  ret = {}
  if @export_hours.empty?
    (DEFAULTS.keys - [:hours]).each do |k|
      ret[k] = send(k)
    end
  else
    ret[:hours] = @export_hours
  end

  if holidays
    ret[:holidays] = @holidays
  end
  ret
end

#hours_active(time) ⇒ Object



71
72
73
# File 'lib/workhours/week.rb', line 71

def hours_active(time)
  hours.select { |h| h.is_active?(time) }
end

#hours_on(date) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/workhours/week.rb', line 81

def hours_on(date)
  if is_holiday?(date)
    []
  else
    hours.select { |h| h.is_today?(date) }.sort_by { |h| h.beginning }
  end
end

#hours_overlap?Boolean

Returns:

  • (Boolean)


155
156
157
158
159
160
161
162
163
164
165
# File 'lib/workhours/week.rb', line 155

def hours_overlap?
  hours.each do |h1|
    hours.each do |h2|
      next if h1 == h2
      if h1.overlaps?(h2)
        return [h1, h2]
      end
    end
  end
  false
end

#inspectObject



67
68
69
# File 'lib/workhours/week.rb', line 67

def inspect
  "<Workhours::Week #{export.inspect}>"
end

#is_closed?(time = Time.now) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/workhours/week.rb', line 78

def is_closed?(time = Time.now)
  !is_open?(time)
end

#is_holiday?(date) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/workhours/week.rb', line 92

def is_holiday?(date)
  holidays.include?(date.to_date)
end

#is_open?(time = Time.now) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/workhours/week.rb', line 75

def is_open?(time = Time.now)
  !hours_active(time).first.nil? && !is_holiday?(time)
end

#is_open_on?(date) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/workhours/week.rb', line 88

def is_open_on?(date)
  hours_on(date).first.nil? && !is_holiday?(date)
end

#next_closing_time(time) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/workhours/week.rb', line 134

def next_closing_time(time)
  active = hours_active(time).first
  tmp_time = active.ending_time(time)
  counter = 0
  loop do
    counter += 1; raise NoClosingError.new if counter > 1000
    found = false
    hours.each do |h|
      next if h == active
      if h.is_active?(tmp_time)
        active = h
        tmp_time = h.ending_time(tmp_time)
        found = true
      end
    end
    unless found
      return tmp_time
    end
  end
end

#next_open_time(time) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/workhours/week.rb', line 111

def next_open_time(time)
  date = time.to_date
  counter = 0
  loop do
    hours = hours_on(date)
    if counter == 0 && !hours.empty?
      after = hours.select { |h| h.beginning > time.to_time_of_day || h.ending > time.to_time_of_day }
      if after.empty?
        hours = []
      else
        return date.at(after[0].beginning)
      end
    end

    if hours.empty?
      date += 1
    else
      return date.at(hours[0].beginning)
    end
    counter += 1
  end
end

#opens_at(time = Time.now) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/workhours/week.rb', line 96

def opens_at(time = Time.now)
  if is_open?(time)
    nil
  else
    next_open_time(time)
  end
end

#week_intObject



167
168
169
170
171
# File 'lib/workhours/week.rb', line 167

def week_int
  week.map do |d|
    Workhours.wday_to_int(d)
  end
end