class Semantic::VersionRange

Constants

EMPTY_RANGE

A range that matches no versions

MAX_VERSION

The highest precedence Version possible

MIN_VERSION

The lowest precedence Version possible

NOT_A_VERSION_RANGE

Public Instance Methods

&(other) click to toggle source
Alias for: intersection
inspect() click to toggle source
Alias for: to_s
intersection(other) click to toggle source

Computes the intersection of a pair of ranges. If the ranges have no useful intersection, an empty range is returned.

@param other [VersionRange] the range to intersect with @return [VersionRange] the common subset

# File lib/puppet/vendor/semantic/lib/semantic/version_range.rb, line 279
def intersection(other)
  raise NOT_A_VERSION_RANGE unless other.kind_of?(VersionRange)

  if self.begin < other.begin
    return other.intersection(self)
  end

  unless include?(other.begin) || other.include?(self.begin)
    return EMPTY_RANGE
  end

  endpoint = ends_before?(other) ? self : other
  VersionRange.new(self.begin, endpoint.end, endpoint.exclude_end?)
end
Also aliased as: &
to_s() click to toggle source

Returns a string representation of this range, prefering simple common expressions for comprehension.

@return [String] a range expression representing this VersionRange

# File lib/puppet/vendor/semantic/lib/semantic/version_range.rb, line 299
def to_s
  start, finish  = self.begin, self.end
  inclusive = exclude_end? ? '' : '='

  case
  when EMPTY_RANGE == self
    "<0.0.0"
  when exact_version?, patch_version?
    "#{ start }"
  when minor_version?
    "#{ start }".sub(/.0$/, '.x')
  when major_version?
    "#{ start }".sub(/.0.0$/, '.x')
  when open_end? && start.to_s =~ /-.*[.]0$/
    ">#{ start }".sub(/.0$/, '')
  when open_end?
    ">=#{ start }"
  when open_begin?
    "<#{ inclusive }#{ finish }"
  else
    ">=#{ start } <#{ inclusive }#{ finish }"
  end
end
Also aliased as: inspect

Public Class Methods

parse(range_str) click to toggle source

Parses a version range string into a comparable {VersionRange} instance.

Currently parsed version range string may take any of the following: forms:

  • Regular Semantic Version strings

    • ex. `“1.0.0”`, `“1.2.3-pre”`

  • Partial Semantic Version strings

    • ex. `“1.0.x”`, `“1”`, `“2.X”`

  • Inequalities

    • ex. `“> 1.0.0”`, `“<3.2.0”`, `“>=4.0.0”`

  • Approximate Versions

    • ex. `“~1.0.0”`, `“~ 3.2.0”`, `“~4.0.0”`

  • Inclusive Ranges

    • ex. `“1.0.0 - 1.3.9”`

  • Range Intersections

    • ex. `“>1.0.0 <=2.3.0”`

@param range_str [String] the version range string to parse @return [VersionRange] a new {VersionRange} instance

# File lib/puppet/vendor/semantic/lib/semantic/version_range.rb, line 26
def parse(range_str)
  partial = '\d+(?:[.]\d+)?(?:[.][x]|[.]\d+(?:[-][0-9a-z.-]*)?)?'
  exact   = '\d+[.]\d+[.]\d+(?:[-][0-9a-z.-]*)?'

  range = range_str.gsub(/([(><=~])[ ]+/, '\1')
  range = range.gsub(/ - /, '#').strip

  return case range
  when /\A(#{partial})\Z/
    parse_loose_version_expression($1)
  when /\A([><][=]?)(#{exact})\Z/
    parse_inequality_expression($1, $2)
  when /\A~(#{partial})\Z/
    parse_reasonably_close_expression($1)
  when /\A(#{exact})#(#{exact})\Z/
    parse_inclusive_range_expression($1, $2)
  when /[ ]+/
    parse_intersection_expression(range)
  else
    raise ArgumentError
  end

rescue ArgumentError
  raise ArgumentError, "Unparsable version range: #{range_str.inspect}"
end