Class: SemVer

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/semverly/sem_ver.rb

Overview

The SemVer class provides parsing and sorting for version strings that comply with the Semantic Versioning 2.0.0 specification.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major, minor = 0, patch = 0, prerelease = nil, metadata = nil) ⇒ SemVer

Creates a new SemVer instance.

Initialization requires at least one argument: the major version number. The minor and patch versions are optional and are 0 if not supplied. The prerelease and metadata parts are also optional and blank if not supplied.



26
27
28
29
30
31
32
# File 'lib/semverly/sem_ver.rb', line 26

def initialize(major, minor = 0, patch = 0, prerelease = nil,  = nil)
  self.major = major
  self.minor = minor
  self.patch = patch
  self.prerelease = prerelease
  self. = 
end

Instance Attribute Details

#majorObject

The major version (x in x.y.z).



7
8
9
# File 'lib/semverly/sem_ver.rb', line 7

def major
  @major
end

#metadataObject

Version metadata (optional).



19
20
21
# File 'lib/semverly/sem_ver.rb', line 19

def 
  @metadata
end

#minorObject

The minor version (y in x.y.z).



10
11
12
# File 'lib/semverly/sem_ver.rb', line 10

def minor
  @minor
end

#patchObject

The patch version (z in x.y.z).



13
14
15
# File 'lib/semverly/sem_ver.rb', line 13

def patch
  @patch
end

#prereleaseObject

Pre-release version tag (optional).



16
17
18
# File 'lib/semverly/sem_ver.rb', line 16

def prerelease
  @prerelease
end

Class Method Details

.parse(string) ⇒ SemVer

Parses a SemVer-compliant string into its component parts.

Parameters:

  • string (String)

    the version identifier to parse

Returns:

  • (SemVer)

    A SemVer instance or nil if the string is not SemVer compliant.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/semverly/sem_ver.rb', line 38

def self.parse(string)
  re = Regexp.new('\Av?(?<major>\d+)
                       (\.(?<minor>\d+))?
                       (\.(?<patch>\d+))?
                       (-(?<prerelease>[0-9A-Za-z.-]+))?
                       (\+(?<metadata>[0-9A-Za-z.-]+))?\z', Regexp::EXTENDED)
  matches = re.match(string)
  return nil if matches.nil?

  major = matches['major'].to_i
  minor = matches['minor'].to_i
  patch = matches['patch'].to_i

  if prerelease = matches['prerelease']
    return nil if prerelease.split('.').any? { |part| part.nil? || part == '' }
  end

  if  = matches['metadata']
    return nil if .split('.').any? { |part| part.nil? || part == '' }
  end

  new(major, minor, patch, prerelease, )
end

Instance Method Details

#<=>(other) ⇒ Fixnum

Compares two SemVer versions.

Parameters:

  • other (SemVer)

    another SemVer instance to compare with the receiver

Returns:

  • (Fixnum)

    -1, 0 or +1 depending on if this instance is less than, equal to or greater than other.



76
77
78
79
80
# File 'lib/semverly/sem_ver.rb', line 76

def <=>(other)
  result = [self.major, self.minor, self.patch] <=> [other.major, other.minor, other.patch]
  result = compare_prerelease(other) if result == 0
  result
end

#to_sString

Generates a version string from SemVer parts.

Returns:

  • (String)

    The version identifier.



65
66
67
68
69
70
# File 'lib/semverly/sem_ver.rb', line 65

def to_s
  s = "#{major}.#{minor}.#{patch}"
  s << "-#{prerelease}" unless prerelease.nil?
  s << "+#{}" unless .nil?
  s
end