Class: Schedule::AvailabilityRange

Inherits:
Object
  • Object
show all
Defined in:
lib/schedule/availability_range.rb

Instance Method Summary collapse

Constructor Details

#initialize(start, end_, day_start:, day_end:, min_delay:, granularity: 5.minutes) ⇒ AvailabilityRange

Returns a new instance of AvailabilityRange.



7
8
9
10
11
12
13
14
15
16
# File 'lib/schedule/availability_range.rb', line 7

def initialize(start, end_, day_start:, day_end:, min_delay:, granularity: 5.minutes)
  @start = start
  @granularity = granularity
  @slots = Array.new(n_slots(start, end_)) do |i|
    dt = slot_to_datetime(i)
    ((dt - DateTime.now).days >= min_delay.hours) &&
      (dt.hour > day_start || (dt.hour == day_start && !dt.minute.zero?)) &&
      (dt.hour < day_end || (dt.hour == day_end && dt.minute.zero?))
  end
end

Instance Method Details

#free_ranges(align_to:, duration:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/schedule/availability_range.rb', line 24

def free_ranges(align_to:, duration:)
  ranges = []
  current_free = false

  @slots.each_with_index do |free, i|
    if current_free
      next if free
      ranges.last << i
      current_free = false
    else
      next unless free
      ranges << [i - 1]
      current_free = true
    end
  end

  ranges.map do |(s, e)|
    start = Time.at((slot_to_datetime(s).to_time.to_f / align_to.minutes).ceil * align_to.minutes)
    end_ = Time.at((slot_to_datetime(e).to_time.to_f / align_to.minutes).floor * align_to.minutes)
    (end_ - start) < duration.minutes ? nil : [start, end_]
  end.compact
end

#mark!(start, end_) ⇒ Object



18
19
20
21
22
# File 'lib/schedule/availability_range.rb', line 18

def mark!(start, end_)
  slot_range(start, end_).each do |i|
    @slots[i] = false
  end
end