Sha256: 5248d6dcf7352c60c03bee76b19b4a999423861c25a555348d8dc790d7279614

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

module Torque
  module Range
    def intersection(other)
      raise ArgumentError, 'value must be a Range' unless other.kind_of?(Range)

      new_min = self.cover?(other.min) ? other.min : other.cover?(min) ? min : nil
      new_max = self.cover?(other.max) ? other.max : other.cover?(max) ? max : nil

      new_min && new_max ? new_min..new_max : nil
    end
    alias_method :&, :intersection

    def union(other)
      raise ArgumentError, 'value must be a Range' unless other.kind_of?(Range)

      ([min, other.min].min)..([max, other.max].max)
    end
    alias_method :|, :union

    def subtract(other)
      raise ArgumentError, 'value must be a Range' unless other.kind_of?(Range)
      return if other.eql?(self)

      other = intersection(other)
      return self if other.nil?

      min.eql?(other.min) ? other.max..max : min..other.min
    end
    alias_method :-, :subtract

    def add(other)
      raise ArgumentError, 'value must be a Range' unless other.kind_of?(Range)

      intersection(other) ? union(other) : self
    end
    alias_method :+, :add
  end

  ::Range.include(Range)
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
torque-postgresql-1.0.1 lib/torque/range.rb
torque-postgresql-1.0.0 lib/torque/range.rb