Class: Tins::StringVersion::Version
- Includes:
- Comparable
- Defined in:
- lib/tins/string_version.rb
Overview
Represents a version string with semantic comparison capabilities
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compares this version with another.
-
#==(other) ⇒ Boolean
Checks equality with another version.
-
#[](level) ⇒ Integer
Gets a version component by index or symbol.
-
#[]=(level, value) ⇒ Integer
Sets a version component by index or symbol.
-
#array ⇒ Array<Integer>
(also: #to_a)
Converts the version to an array of integers.
-
#build ⇒ Integer
(also: #patch)
Returns the build version component.
-
#build=(new_level) ⇒ Integer
(also: #patch=)
Sets the build version component.
-
#bump(level = array.size - 1) ⇒ self
Increments a specified version component and resets subsequent components.
-
#initialize(string) ⇒ Version
constructor
Creates a new version object from a string representation.
-
#initialize_copy(source) ⇒ void
Creates a copy of this version object.
-
#level_of(specifier) ⇒ Integer
Converts a symbolic level to its numeric index.
-
#major ⇒ Integer
Returns the major version component.
-
#major=(new_level) ⇒ Integer
Sets the major version component.
-
#minor ⇒ Integer
Returns the minor version component.
-
#minor=(new_level) ⇒ Integer
Sets the minor version component.
-
#pred! ⇒ self
Decrements the last version component.
-
#revision ⇒ Integer
Returns the revision version component.
-
#revision=(new_level) ⇒ Integer
Sets the revision version component.
-
#succ! ⇒ self
Increments the last version component.
-
#to_s ⇒ String
(also: #inspect)
Returns the string representation of this version.
Constructor Details
#initialize(string) ⇒ Version
Creates a new version object from a string representation
54 55 56 57 58 |
# File 'lib/tins/string_version.rb', line 54 def initialize(string) string =~ /\A\d+(\.\d+)*\z/ or raise ArgumentError, "#{string.inspect} isn't a version number" @version = string.frozen? ? string.dup : string end |
Instance Method Details
#<=>(other) ⇒ Integer
Compares this version with another
220 221 222 223 224 225 |
# File 'lib/tins/string_version.rb', line 220 def <=>(other) pairs = array.zip(other.array) pairs.map! { |a, b| [ a.to_i, b.to_i ] } a, b = pairs.transpose a <=> b end |
#==(other) ⇒ Boolean
Checks equality with another version
231 232 233 |
# File 'lib/tins/string_version.rb', line 231 def ==(other) (self <=> other).zero? end |
#[](level) ⇒ Integer
Gets a version component by index or symbol
179 180 181 |
# File 'lib/tins/string_version.rb', line 179 def [](level) array[level_of(level)] end |
#[]=(level, value) ⇒ Integer
Sets a version component by index or symbol
189 190 191 192 193 194 195 196 197 198 |
# File 'lib/tins/string_version.rb', line 189 def []=(level, value) level = level_of(level) value = value.to_i value >= 0 or raise ArgumentError, "version numbers can't contain negative numbers like #{value}" a = array a[level] = value a.map!(&:to_i) @version.replace a * ?. end |
#array ⇒ Array<Integer> Also known as: to_a
Converts the version to an array of integers
238 239 240 |
# File 'lib/tins/string_version.rb', line 238 def array @version.split(?.).map(&:to_i) end |
#build ⇒ Integer Also known as: patch
Returns the build version component
105 106 107 |
# File 'lib/tins/string_version.rb', line 105 def build self[2] end |
#build=(new_level) ⇒ Integer Also known as: patch=
Sets the build version component
119 120 121 |
# File 'lib/tins/string_version.rb', line 119 def build=(new_level) self[2] = new_level end |
#bump(level = array.size - 1) ⇒ self
Increments a specified version component and resets subsequent components
154 155 156 157 158 159 160 161 |
# File 'lib/tins/string_version.rb', line 154 def bump(level = array.size - 1) level = level_of(level) self[level] += 1 for l in level.succ..3 self[l] &&= 0 end self end |
#initialize_copy(source) ⇒ void
This method returns an undefined value.
Creates a copy of this version object
256 257 258 259 |
# File 'lib/tins/string_version.rb', line 256 def initialize_copy(source) super @version = source.instance_variable_get(:@version).dup end |
#level_of(specifier) ⇒ Integer
Converts a symbolic level to its numeric index
167 168 169 170 171 172 173 |
# File 'lib/tins/string_version.rb', line 167 def level_of(specifier) if specifier.respond_to?(:to_sym) LEVELS.fetch(specifier) else specifier end end |
#major ⇒ Integer
Returns the major version component
65 66 67 |
# File 'lib/tins/string_version.rb', line 65 def major self[0] end |
#major=(new_level) ⇒ Integer
Sets the major version component
76 77 78 |
# File 'lib/tins/string_version.rb', line 76 def major=(new_level) self[0] = new_level end |
#minor ⇒ Integer
Returns the minor version component
85 86 87 |
# File 'lib/tins/string_version.rb', line 85 def minor self[1] end |
#minor=(new_level) ⇒ Integer
Sets the minor version component
96 97 98 |
# File 'lib/tins/string_version.rb', line 96 def minor=(new_level) self[1] = new_level end |
#pred! ⇒ self
Decrements the last version component
211 212 213 214 |
# File 'lib/tins/string_version.rb', line 211 def pred! self[-1] -= 1 self end |
#revision ⇒ Integer
Returns the revision version component
131 132 133 |
# File 'lib/tins/string_version.rb', line 131 def revision self[3] end |
#revision=(new_level) ⇒ Integer
Sets the revision version component
142 143 144 |
# File 'lib/tins/string_version.rb', line 142 def revision=(new_level) self[3] = new_level end |
#succ! ⇒ self
Increments the last version component
203 204 205 206 |
# File 'lib/tins/string_version.rb', line 203 def succ! self[-1] += 1 self end |
#to_s ⇒ String Also known as: inspect
Returns the string representation of this version
248 249 250 |
# File 'lib/tins/string_version.rb', line 248 def to_s @version end |