Class: VersionBoss::Semver::Version
- Inherits:
-
Object
- Object
- VersionBoss::Semver::Version
- Includes:
- Comparable
- Defined in:
- lib/version_boss/semver/version.rb
Overview
Parse and compare semver version strings
This class will parse a semver version string that complies to Semantic Versioning 2.0.0.
Two Semver objects can be compared using the spaceship operator (<=>) according to the rules of Semantic Versioning 2.0.0.
See the Semantic Versioning 2.0.0 specification for more details.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#build_metadata ⇒ String
readonly
The build_metadata part of the version.
-
#major ⇒ String
readonly
The major part of the version.
-
#minor ⇒ String
readonly
The minor part of the version.
-
#patch ⇒ String
readonly
The patch part of the version.
-
#pre_release ⇒ String
readonly
The pre_release part of the version.
-
#pre_release_identifiers ⇒ Array<String>
readonly
The pre_release identifiers of the version.
-
#version ⇒ String
readonly
The complete version string.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compare two Semver objects.
-
#==(other) ⇒ Boolean
Two versions are equal if their version strings are equal.
-
#initialize(version) ⇒ Version
constructor
Create a new Semver object.
-
#to_s ⇒ String
The string representation of a Semver is its version string.
-
#valid? ⇒ Boolean
Determine if the version string is a valid semver.
Constructor Details
#initialize(version) ⇒ Version
Create a new Semver object
53 54 55 56 57 58 |
# File 'lib/version_boss/semver/version.rb', line 53 def initialize(version) assert_version_must_be_a_string(version) @version = version parse assert_valid_version end |
Instance Attribute Details
#build_metadata ⇒ String (readonly)
The build_metadata part of the version
Will be an empty string if the version has no build_metadata part.
172 173 174 |
# File 'lib/version_boss/semver/version.rb', line 172 def @build_metadata end |
#major ⇒ String (readonly)
The major part of the version
86 87 88 |
# File 'lib/version_boss/semver/version.rb', line 86 def major @major end |
#minor ⇒ String (readonly)
The minor part of the version
100 101 102 |
# File 'lib/version_boss/semver/version.rb', line 100 def minor @minor end |
#patch ⇒ String (readonly)
The patch part of the version
114 115 116 |
# File 'lib/version_boss/semver/version.rb', line 114 def patch @patch end |
#pre_release ⇒ String (readonly)
The pre_release part of the version
Will be an empty string if the version has no pre_release part.
134 135 136 |
# File 'lib/version_boss/semver/version.rb', line 134 def pre_release @pre_release end |
#pre_release_identifiers ⇒ Array<String> (readonly)
The pre_release identifiers of the version
152 153 154 |
# File 'lib/version_boss/semver/version.rb', line 152 def pre_release_identifiers @pre_release_identifiers end |
#version ⇒ String (readonly)
The complete version string
72 73 74 |
# File 'lib/version_boss/semver/version.rb', line 72 def version @version end |
Instance Method Details
#<=>(other) ⇒ Integer
Compare two Semver objects
See the Precedence Rules in the Semantic Versioning 2.0.0 Specification for more details.
210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/version_boss/semver/version.rb', line 210 def <=>(other) assert_other_is_a_semver(other) result = compare_core_parts(other) return result unless result.zero? && pre_release != other.pre_release return 1 if pre_release.empty? return -1 if other.pre_release.empty? compare_pre_release_part(other) end |
#==(other) ⇒ Boolean
Two versions are equal if their version strings are equal
247 248 249 |
# File 'lib/version_boss/semver/version.rb', line 247 def ==(other) version == other.to_s end |
#to_s ⇒ String
The string representation of a Semver is its version string
258 259 260 |
# File 'lib/version_boss/semver/version.rb', line 258 def to_s version end |
#valid? ⇒ Boolean
Determine if the version string is a valid semver
Override this method in a subclass to provide extra or custom validation.
233 234 235 236 |
# File 'lib/version_boss/semver/version.rb', line 233 def valid? # If major is set, then so is everything else !major.nil? end |