class DateRangeOverlap

Public Class Methods

overlap_range(range1, range2) click to toggle source

Finds the overlap range and return it

@param [Range] range1 the first range to compare @param [Range] range2 the second range to compare @return [Range, nil] the overlap range if exists.

nil, otherwise
# File lib/date_range_overlap.rb, line 32
      def self.overlap_range(range1, range2)

              return nil unless self.overlaps?(range1, range2)

              return [@range1.first, @range2.first].max..[@range1.last,@range2.last].min

end
overlaps?(range1, range2) click to toggle source

Tells if the two ranges passed as parameters overlap each other

@param [Range] range1 the first range to compare @param [Range] range2 the second range to compare @return [Boolean] true if the two ranges overlap each

other. false, otherwise.
# File lib/date_range_overlap.rb, line 14
def self.overlaps?(range1, range2)
        @range1 = range1.first < range1.last ? 
                                range1 : range1.last..range1.first
        @range2 = range2.first < range2.last ? 
                                range2 : range2.last..range2.first

        return true if ((@range1.first - @range2.last) * 
                                        (@range2.first - @range1.last)) >= 0
        return false
end