Class: Semantic::VersionRange

Inherits:
Range show all
Defined in:
lib/puppet/vendor/semantic/lib/semantic/version_range.rb

Constant Summary collapse

EMPTY_RANGE =

A range that matches no versions

VersionRange.parse('< 0.0.0').freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(range_str) ⇒ VersionRange

Parses a version range string into a comparable Semantic::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”`

Parameters:

  • range_str (String)

    the version range string to parse

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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/i
    parse_loose_version_expression($1)
  when /\A([><][=]?)(#{exact})\Z/i
    parse_inequality_expression($1, $2)
  when /\A~(#{partial})\Z/i
    parse_reasonably_close_expression($1)
  when /\A(#{exact})#(#{exact})\Z/i
    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

Instance Method Details

#intersection(other) ⇒ VersionRange Also known as: &

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

Parameters:

Returns:

Raises:

  • (NOT_A_VERSION_RANGE)


279
280
281
282
283
284
285
286
287
288
289
290
291
292
# 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

#to_sString Also known as: inspect

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

Returns:

  • (String)

    a range expression representing this VersionRange



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# 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