Class: Udb::VersionSpec
- Inherits:
-
Object
- Object
- Udb::VersionSpec
- Extended by:
- T::Sig
- Includes:
- Comparable
- Defined in:
- lib/udb/version_spec.rb
Overview
Represents an RVI version specifier
Version specs have the form:
MAJOR[.MINOR[.PATCH[-pre]]]
Where MAJOR, MINOR, and PATCH are integers and “pre” is an optional string
Notably, these DO NOT represent a Semantic Version (semver.og).
Rather, versions are treated as follows:
* Versions are assumed to be backward compatible by default.
For example,
- 2.0 is compatible with 1.0
- 1.1 is compatible with 1.0
- 0.9 is *not* compatible with 1.0
* A version can be explicitly marked as "breaking" in the architecture definition
Breaking versions are not backward compatible with any smaller versions
For example, if version 2.2 is Breaking,
- 3.0 is compatible with 2.2
- 2.3 is compatible with 2.2
- 3.0 is *not* compatible with 2.0
- 2.2 is *not* compatible with 2.0
- 2.1 is compatible with 2.0
Constant Summary collapse
- VERSION_REGEX =
/([0-9]+)(?:\.([0-9]+)(?:\.([0-9]+)(?:-(pre))?)?)?/
Instance Attribute Summary collapse
-
#major ⇒ Integer
readonly
Major version number.
-
#minor ⇒ Integer
readonly
Minor version number.
-
#patch ⇒ Integer
readonly
Patch version number.
-
#pre ⇒ Boolean
readonly
Whether or not this is a pre-release.
Instance Method Summary collapse
- #<=>(other) ⇒ Integer?
-
#canonical ⇒ String
The version, in canonical form.
- #decrement_patch ⇒ VersionSpec
- #eql?(other) ⇒ Boolean
- #hash ⇒ Integer
- #increment_patch ⇒ VersionSpec
- #initialize(version_str) constructor
- #inspect ⇒ String
-
#to_rvi_s ⇒ String
The version formatted like RVI docs.
-
#to_s ⇒ String
The exact string used during construction.
Constructor Details
#initialize(version_str)
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/udb/version_spec.rb', line 56 def initialize(version_str) if version_str =~ /^\s*#{VERSION_REGEX}\s*$/ m = T.must(::Regexp.last_match) @major = m[1].to_i @minor_given = !m[2].nil? @minor = @minor_given ? m[2].to_i : 0 @patch_given = !m[3].nil? @patch = @patch_given ? m[3].to_i : 0 @pre = !m[4].nil? @major.freeze @minor.freeze @patch.freeze @pre.freeze else raise ArgumentError, "#{version_str} is not a valid Version spec" end @version_str = version_str end |
Instance Attribute Details
#major ⇒ Integer (readonly)
Returns Major version number.
44 45 46 |
# File 'lib/udb/version_spec.rb', line 44 def major @major end |
#minor ⇒ Integer (readonly)
Returns Minor version number.
47 48 49 |
# File 'lib/udb/version_spec.rb', line 47 def minor @minor end |
#patch ⇒ Integer (readonly)
Returns Patch version number.
50 51 52 |
# File 'lib/udb/version_spec.rb', line 50 def patch @patch end |
#pre ⇒ Boolean (readonly)
Returns Whether or not this is a pre-release.
53 54 55 |
# File 'lib/udb/version_spec.rb', line 53 def pre @pre end |
Instance Method Details
#<=>(other) ⇒ Integer?
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/udb/version_spec.rb', line 104 def <=>(other) if other.is_a?(String) VersionSpec.new(other) <=> self elsif other.is_a?(VersionSpec) if @major != other.major @major <=> other.major elsif @minor != other.minor @minor <=> other.minor elsif @patch != other.patch @patch <=> other.patch elsif @pre != other.pre @pre ? 1 : -1 else 0 end else nil end end |
#canonical ⇒ String
Returns The version, in canonical form.
82 83 84 |
# File 'lib/udb/version_spec.rb', line 82 def canonical "#{@major}.#{@minor}.#{@patch}#{@pre ? '-pre' : ''}" end |
#decrement_patch ⇒ VersionSpec
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/udb/version_spec.rb', line 146 def decrement_patch copy = dup if @patch > 0 copy.instance_variable_set(:@patch, @patch - 1) elsif @minor > 0 copy.instance_variable_set(:@minor, @minor - 1) copy.instance_variable_set(:@patch, 9999) elsif @major > 0 copy.instance_variable_set(:@major, @major - 1) copy.instance_variable_set(:@minor, 9999) copy.instance_variable_set(:@patch, 9999) else raise "Cannot decrement version 0" end copy end |
#eql?(other) ⇒ Boolean
125 126 127 128 129 130 131 |
# File 'lib/udb/version_spec.rb', line 125 def eql?(other) if other.is_a?(VersionSpec) self.hash == other.hash else false end end |
#hash ⇒ Integer
134 135 136 |
# File 'lib/udb/version_spec.rb', line 134 def hash [@major, @minor, @patch, @pre].hash end |
#increment_patch ⇒ VersionSpec
139 140 141 142 143 |
# File 'lib/udb/version_spec.rb', line 139 def increment_patch copy = dup copy.instance_variable_set(:@patch, @patch + 1) copy end |
#inspect ⇒ String
76 77 78 |
# File 'lib/udb/version_spec.rb', line 76 def inspect "VersionSpec[str: #{@version_str}; major: #{@major}, minor: #{@minor}, patch: #{@patch}, pre: #{@pre}]" end |
#to_rvi_s ⇒ String
Returns The version formatted like RVI docs.
91 92 93 94 95 96 97 |
# File 'lib/udb/version_spec.rb', line 91 def to_rvi_s s = @major.to_s s += "p#{@minor}" if @minor_given s += "p#{@patch}" if @patch_given s += "-pre" if @pre s end |
#to_s ⇒ String
Returns The exact string used during construction.
101 |
# File 'lib/udb/version_spec.rb', line 101 def to_s = @version_str |