Sha256: 407295e44a74b940f9eeee0b2e88f5d0625cfe50e6bbe2b12dee62a9bb2ab863

Contents?: true

Size: 1.13 KB

Versions: 10

Compression:

Stored size: 1.13 KB

Contents

class Range

  # Uses the Range#umbrella method to determine
  # if another Range is _anywhere_ within this Range.
  #
  #   (1..3).within?(0..4)  #=> true
  #
  # CREDIT: Trans

  def within?(rng)
    case rng.umbrella(self)
    when [0,0], [-1,0], [0,-1], [-1,-1]
      return true
    else
      return false
    end
  end

  # Returns a two element array of the relationship
  # between two Ranges.
  #
  # Diagram:
  #
  #     Relationship     Returns
  #
  #   self |-----------|
  #   r    |-----------|    [0,0]
  #
  #   self |-----------|
  #   r     |---------|     [-1,-1]
  #
  #   self  |---------|
  #   r    |-----------|    [1,1]
  #
  #   self |-----------|
  #   r     |----------|    [-1,0]
  #
  #   self |-----------|
  #   r     |-----------|   [-1,1]
  #
  #   etc.
  #
  # Example:
  #
  #   (0..3).umbrella(1..2)  #=>  [-1,-1]
  #
  # CREDIT: Trans, Chris Kappler

  def umbrella(r)
    s = first <=> r.first
    e = r.last <=> last

    if e == 0
      if r.exclude_end? and exclude_end?
        e = r.max <=> max
      else
        e = (r.exclude_end? ? 0 : 1) <=> (exclude_end? ? 0 : 1)
      end
    end

    return s,e
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/range/within.rb
facets-2.8.3 lib/core/facets/range/within.rb
facets-2.8.2 lib/core/facets/range/within.rb
facets-2.8.1 lib/core/facets/range/within.rb
facets-2.8.0 lib/core/facets/range/within.rb
facets-2.7.0 lib/core/facets/range/within.rb
facets-2.6.0 lib/core/facets/range/within.rb
facets-2.5.0 lib/core/facets/range/within.rb
facets-2.5.1 lib/core/facets/range/within.rb
facets-2.5.2 lib/core/facets/range/within.rb