class Availabiliter::DateRange

DateRange is a Range with only dates DateRange start_date is always the earliest date and end_date the latest date

Public Class Methods

new(start_date, end_date) click to toggle source
Calls superclass method
# File lib/availabiliter/date_range.rb, line 14
def initialize(start_date, end_date)
  super
  raise ArgumentError, "bad value for DateRange" unless valid?
end

Public Instance Methods

adjacent?(other) click to toggle source

adjacent == touches but doesn't overlap other DateRange

# File lib/availabiliter/date_range.rb, line 34
def adjacent?(other)
  return other.end_date == yesterday if end_date.nil?

  other.end_date == yesterday || other.start_date == tomorrow
end
furthest(other) click to toggle source
# File lib/availabiliter/date_range.rb, line 55
def furthest(other)
  return self if end_date.nil? || other.nil?
  return other if other.end_date.nil?

  [self, other].max_by(&:end_date)
end
independent?(other) click to toggle source
# File lib/availabiliter/date_range.rb, line 19
def independent?(other)
  return true if other.nil?

  !overlaps?(other) && !adjacent?(other)
end
next_availability(next_date_range) click to toggle source
# File lib/availabiliter/date_range.rb, line 44
def next_availability(next_date_range)
  return if end_date.nil?
  return tomorrow..nil if next_date_range.nil?
  return unless independent?(next_date_range)

  gap_start = tomorrow
  gap_end = next_date_range.yesterday

  gap_start..gap_end
end
overlaps?(other) click to toggle source
# File lib/availabiliter/date_range.rb, line 40
def overlaps?(other)
  cover?(other.begin) || other.cover?(first)
end
tomorrow() click to toggle source
# File lib/availabiliter/date_range.rb, line 25
def tomorrow
  end_date&.next_day
end
yesterday() click to toggle source
# File lib/availabiliter/date_range.rb, line 29
def yesterday
  start_date.prev_day
end

Private Instance Methods

valid?() click to toggle source
# File lib/availabiliter/date_range.rb, line 64
def valid?
  start_date.instance_of?(Date) && (end_date.nil? || start_date < end_date)
end