Class: SemVer
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
-
#major ⇒ Object
The major version (x in
x.y.z
). -
#metadata ⇒ Object
Version metadata (optional).
-
#minor ⇒ Object
The minor version (y in
x.y.z
). -
#patch ⇒ Object
The patch version (z in
x.y.z
). -
#prerelease ⇒ Object
Pre-release version tag (optional).
Class Method Summary collapse
-
.parse(string) ⇒ SemVer
Parses a SemVer-compliant string into its component parts.
Instance Method Summary collapse
-
#<=>(other) ⇒ Fixnum
Compares two SemVer versions.
-
#initialize(major, minor = 0, patch = 0, prerelease = nil, metadata = nil) ⇒ SemVer
constructor
Creates a new SemVer instance.
-
#to_s ⇒ String
Generates a version string from SemVer parts.
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
#major ⇒ Object
The major version (x in x.y.z
).
7 8 9 |
# File 'lib/semverly/sem_ver.rb', line 7 def major @major end |
#metadata ⇒ Object
Version metadata (optional).
19 20 21 |
# File 'lib/semverly/sem_ver.rb', line 19 def @metadata end |
#minor ⇒ Object
The minor version (y in x.y.z
).
10 11 12 |
# File 'lib/semverly/sem_ver.rb', line 10 def minor @minor end |
#patch ⇒ Object
The patch version (z in x.y.z
).
13 14 15 |
# File 'lib/semverly/sem_ver.rb', line 13 def patch @patch end |
#prerelease ⇒ Object
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.
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.
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_s ⇒ String
Generates a version string from SemVer parts.
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 |