Class: Capsium::Package::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/capsium/package/version.rb,
sig/capsium/package/version.rbs

Overview

A semantic version (MAJOR.MINOR.PATCH[+build]) with semver precedence ordering.

Constant Summary collapse

PATTERN =

Returns:

  • (Regexp)
/\A(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Version.



22
23
24
25
26
27
# File 'lib/capsium/package/version.rb', line 22

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

Instance Attribute Details

#majorInteger (readonly)

Returns the value of attribute major.

Returns:

  • (Integer)


13
14
15
# File 'lib/capsium/package/version.rb', line 13

def major
  @major
end

#minorInteger (readonly)

Returns the value of attribute minor.

Returns:

  • (Integer)


13
14
15
# File 'lib/capsium/package/version.rb', line 13

def minor
  @minor
end

#patchInteger (readonly)

Returns the value of attribute patch.

Returns:

  • (Integer)


13
14
15
# File 'lib/capsium/package/version.rb', line 13

def patch
  @patch
end

#prereleaseString? (readonly)

Returns the value of attribute prerelease.

Returns:

  • (String, nil)


13
14
15
# File 'lib/capsium/package/version.rb', line 13

def prerelease
  @prerelease
end

Class Method Details

.parse(string) ⇒ Version

Parameters:

  • string (String)

Returns:

Raises:



15
16
17
18
19
20
# File 'lib/capsium/package/version.rb', line 15

def self.parse(string)
  match = PATTERN.match(string.to_s.strip)
  raise Error, "Invalid semver version: #{string.inspect}" unless match

  new(match[1].to_i, match[2].to_i, match[3].to_i, match[4])
end

Instance Method Details

#<=>(other) ⇒ Integer

Parameters:

Returns:

  • (Integer)


29
30
31
32
33
34
# File 'lib/capsium/package/version.rb', line 29

def <=>(other)
  core = [major, minor, patch] <=> [other.major, other.minor, other.patch]
  return core unless core.zero?

  compare_prerelease(other)
end

#to_sString

Returns:

  • (String)


36
37
38
# File 'lib/capsium/package/version.rb', line 36

def to_s
  "#{major}.#{minor}.#{patch}#{"-#{prerelease}" if prerelease}"
end