Class: Schedule::AvailabilityRange
- Inherits:
-
Object
- Object
- Schedule::AvailabilityRange
- Defined in:
- lib/schedule/availability_range.rb
Instance Method Summary collapse
- #free_ranges(align_to:, duration:) ⇒ Object
-
#initialize(start, end_, day_start:, day_end:, granularity: 5.minutes) ⇒ AvailabilityRange
constructor
A new instance of AvailabilityRange.
- #mark!(start, end_) ⇒ Object
Constructor Details
#initialize(start, end_, day_start:, day_end:, granularity: 5.minutes) ⇒ AvailabilityRange
Returns a new instance of AvailabilityRange.
7 8 9 10 11 12 13 14 |
# File 'lib/schedule/availability_range.rb', line 7 def initialize(start, end_, day_start:, day_end:, granularity: 5.minutes) @start = start @granularity = granularity @slots = Array.new(n_slots(start, end_)) do |i| dt = slot_to_datetime(i) (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
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/schedule/availability_range.rb', line 22 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
16 17 18 19 20 |
# File 'lib/schedule/availability_range.rb', line 16 def mark!(start, end_) slot_range(start, end_).each do |i| @slots[i] = false end end |