Class: Semantic::Version

Inherits:
Numeric
  • Object
show all
Includes:
Comparable
Defined in:
lib/puppet/vendor/semantic/lib/semantic/version.rb

Overview

Note:

Semantic::Version subclasses Numeric so that it has sane Range semantics in Ruby 1.9+.

Defined Under Namespace

Classes: ValidationFailure

Constant Summary collapse

LOOSE_REGEX =
/
  \A
  (\d+)[.](\d+)[.](\d+) # Major . Minor . Patch
  (?: [-](.*?))?        # Prerelease
  (?: [+](.*?))?        # Build
  \Z
/x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major, minor, patch, prerelease = nil, build = nil) ⇒ Version

Returns a new instance of Version.



76
77
78
79
80
81
82
# File 'lib/puppet/vendor/semantic/lib/semantic/version.rb', line 76

def initialize(major, minor, patch, prerelease = nil, build = nil)
  @major      = major
  @minor      = minor
  @patch      = patch
  @prerelease = prerelease
  @build      = build
end

Instance Attribute Details

#majorObject (readonly)

Returns the value of attribute major.



74
75
76
# File 'lib/puppet/vendor/semantic/lib/semantic/version.rb', line 74

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



74
75
76
# File 'lib/puppet/vendor/semantic/lib/semantic/version.rb', line 74

def minor
  @minor
end

#patchObject (readonly)

Returns the value of attribute patch.



74
75
76
# File 'lib/puppet/vendor/semantic/lib/semantic/version.rb', line 74

def patch
  @patch
end

Class Method Details

.parse(ver) ⇒ Version

Parse a Semantic Version string.

Parameters:

  • ver (String)

    the version string to parse

Returns:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/puppet/vendor/semantic/lib/semantic/version.rb', line 25

def parse(ver)
  match, major, minor, patch, prerelease, build = *ver.match(LOOSE_REGEX)

  if match.nil?
    raise 'Version numbers MUST begin with three dot-separated numbers'
  elsif [major, minor, patch].any? { |x| x =~ /^0\d+/ }
    raise 'Version numbers MUST NOT contain leading zeroes'
  end

  prerelease = parse_prerelease(prerelease) if prerelease
  build = (build) if build

  self.new(major.to_i, minor.to_i, patch.to_i, prerelease, build)
end

Instance Method Details

#<=>(other) ⇒ Object



108
109
110
111
112
113
# File 'lib/puppet/vendor/semantic/lib/semantic/version.rb', line 108

def <=>(other)
  return self.major <=> other.major unless self.major == other.major
  return self.minor <=> other.minor unless self.minor == other.minor
  return self.patch <=> other.patch unless self.patch == other.patch
  return compare_prerelease(other)
end

#buildObject



104
105
106
# File 'lib/puppet/vendor/semantic/lib/semantic/version.rb', line 104

def build
  @build && @build.join('.')
end

#hashObject



121
122
123
# File 'lib/puppet/vendor/semantic/lib/semantic/version.rb', line 121

def hash
  self.to_s.hash
end

#next(part) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/puppet/vendor/semantic/lib/semantic/version.rb', line 84

def next(part)
  case part
  when :major
    self.class.new(@major.next, 0, 0)
  when :minor
    self.class.new(@major, @minor.next, 0)
  when :patch
    self.class.new(@major, @minor, @patch.next)
  end
end

#prereleaseObject



95
96
97
# File 'lib/puppet/vendor/semantic/lib/semantic/version.rb', line 95

def prerelease
  @prerelease && @prerelease.join('.')
end

#stable?Boolean

Returns true if this is a stable release.

Returns:

  • (Boolean)

    true if this is a stable release



100
101
102
# File 'lib/puppet/vendor/semantic/lib/semantic/version.rb', line 100

def stable?
  @prerelease.nil?
end

#to_sObject



115
116
117
118
119
# File 'lib/puppet/vendor/semantic/lib/semantic/version.rb', line 115

def to_s
  "#{major}.#{minor}.#{patch}" +
  (@prerelease.nil? || prerelease.empty? ? '' : "-" + prerelease) +
  (@build.nil?      || build.empty?      ? '' : "+" + build     )
end