module VersionRangesIntersection

Public Instance Methods

intersect?(other) click to toggle source

Checks whether two ranges overlap

# File lib/usedby/version_ranges_intersection.rb, line 8
def intersect?(other)
  case self <=> other
  when -1
    return (right <=> other.left) == 1
  when 1
    return (other.right <=> left) == 1
  end

  true
end
intersection(other) click to toggle source

Returns a new range which is the intersection, or else nil

# File lib/usedby/version_ranges_intersection.rb, line 20
def intersection(other)
  return nil unless intersect?(other)

  inter = clone
  case inter <=> other
  when -1
    inter.left = other.left
  when 1
    inter.right = other.right
  end

  inter
end
reduce(reqrs = []) click to toggle source
# File lib/usedby/version_ranges_intersection.rb, line 37
def reduce(reqrs = [])
  reduced_reqrs = reqrs.clone
  i = 0
  while i < reduced_reqrs.size - 1
    intersecting_range = reduced_reqrs[i].intersection(reduced_reqrs[i + 1])
    if intersecting_range
      reduced_reqrs[i + 1] = intersecting_range
      reduced_reqrs.delete_at(i)
    else
      i += 1
    end
  end

  reduced_reqrs
end