Class: SemanticPuppet::Version

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

Overview

Note:

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

Defined Under Namespace

Classes: ValidationFailure

Constant Summary collapse

REGEX_NUMERIC =

Version string matching regexes

"(0|[1-9]\\d*)[.](0|[1-9]\\d*)[.](0|[1-9]\\d*)"
REGEX_PRE =

Major . Minor . Patch

"(?:[-](.*?))?"
REGEX_BUILD =

Prerelease

"(?:[+](.*?))?"
REGEX_FULL =

Build

REGEX_NUMERIC + REGEX_PRE + REGEX_BUILD
MIN =

The lowest precedence Version possible

self.new(0, 0, 0, []).freeze
MAX =

The highest precedence Version possible

self.new((1.0/0.0), 0, 0).freeze

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.



79
80
81
82
83
84
85
# File 'lib/puppet/vendor/semantic_puppet/lib/semantic_puppet/version.rb', line 79

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.



77
78
79
# File 'lib/puppet/vendor/semantic_puppet/lib/semantic_puppet/version.rb', line 77

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



77
78
79
# File 'lib/puppet/vendor/semantic_puppet/lib/semantic_puppet/version.rb', line 77

def minor
  @minor
end

#patchObject (readonly)

Returns the value of attribute patch.



77
78
79
# File 'lib/puppet/vendor/semantic_puppet/lib/semantic_puppet/version.rb', line 77

def patch
  @patch
end

Class Method Details

.parse(ver) ⇒ Version

Parse a Semantic Version string.

Parameters:

  • ver (String)

    the version string to parse

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/puppet/vendor/semantic_puppet/lib/semantic_puppet/version.rb', line 17

def parse(ver)
  match, major, minor, patch, prerelease, build = *ver.match(/\A#{REGEX_FULL}\Z/)

  if match.nil?
    raise _("Unable to parse '%{version}' as a semantic version identifier") % {version: ver}
  end

  prerelease = parse_prerelease(prerelease) if prerelease
  # Build metadata is not yet supported in semantic_puppet, but we hope to.
  # The following code prevents build metadata for now.
  #build = parse_build_metadata(build) if build
  if !build.nil?
    raise _("'%{version}' MUST NOT include build identifiers") % {version: ver}
  end

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

.valid?(ver) ⇒ bool

Validate a Semantic Version string.

Parameters:

  • ver (String)

    the version string to validate

Returns:

  • (bool)

    whether or not the string represents a valid Semantic Version



39
40
41
# File 'lib/puppet/vendor/semantic_puppet/lib/semantic_puppet/version.rb', line 39

def valid?(ver)
  !!(ver =~ /\A#{REGEX_FULL}\Z/)
end

Instance Method Details

#<=>(other) ⇒ Object



111
112
113
114
115
116
# File 'lib/puppet/vendor/semantic_puppet/lib/semantic_puppet/version.rb', line 111

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



107
108
109
# File 'lib/puppet/vendor/semantic_puppet/lib/semantic_puppet/version.rb', line 107

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

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
# File 'lib/puppet/vendor/semantic_puppet/lib/semantic_puppet/version.rb', line 118

def eql?(other)
  other.is_a?(Version) &&
    @major.eql?(other.major) &&
    @minor.eql?(other.minor) &&
    @patch.eql?(other.patch) &&
    @prerelease.eql?(other.instance_variable_get(:@prerelease)) &&
    @build.eql?(other.instance_variable_get(:@build))
end

#hashObject



134
135
136
# File 'lib/puppet/vendor/semantic_puppet/lib/semantic_puppet/version.rb', line 134

def hash
  self.to_s.hash
end

#next(part) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/puppet/vendor/semantic_puppet/lib/semantic_puppet/version.rb', line 87

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



98
99
100
# File 'lib/puppet/vendor/semantic_puppet/lib/semantic_puppet/version.rb', line 98

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



103
104
105
# File 'lib/puppet/vendor/semantic_puppet/lib/semantic_puppet/version.rb', line 103

def stable?
  @prerelease.nil?
end

#to_sObject



128
129
130
131
132
# File 'lib/puppet/vendor/semantic_puppet/lib/semantic_puppet/version.rb', line 128

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