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
Returns the build version component.
-
#build=(new_level) ⇒ Integer
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
51 52 53 54 55 |
# File 'lib/tins/string_version.rb', line 51 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
211 212 213 214 215 216 |
# File 'lib/tins/string_version.rb', line 211 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
222 223 224 |
# File 'lib/tins/string_version.rb', line 222 def ==(other) (self <=> other).zero? end |
#[](level) ⇒ Integer
Gets a version component by index or symbol
170 171 172 |
# File 'lib/tins/string_version.rb', line 170 def [](level) array[level_of(level)] end |
#[]=(level, value) ⇒ Integer
Sets a version component by index or symbol
180 181 182 183 184 185 186 187 188 189 |
# File 'lib/tins/string_version.rb', line 180 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
229 230 231 |
# File 'lib/tins/string_version.rb', line 229 def array @version.split(?.).map(&:to_i) end |
#build ⇒ Integer
Returns the build version component
102 103 104 |
# File 'lib/tins/string_version.rb', line 102 def build self[2] end |
#build=(new_level) ⇒ Integer
Sets the build version component
113 114 115 |
# File 'lib/tins/string_version.rb', line 113 def build=(new_level) self[2] = new_level end |
#bump(level = array.size - 1) ⇒ self
Increments a specified version component and resets subsequent components
145 146 147 148 149 150 151 152 |
# File 'lib/tins/string_version.rb', line 145 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
247 248 249 250 |
# File 'lib/tins/string_version.rb', line 247 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
158 159 160 161 162 163 164 |
# File 'lib/tins/string_version.rb', line 158 def level_of(specifier) if specifier.respond_to?(:to_sym) LEVELS.fetch(specifier) else specifier end end |
#major ⇒ Integer
Returns the major version component
62 63 64 |
# File 'lib/tins/string_version.rb', line 62 def major self[0] end |
#major=(new_level) ⇒ Integer
Sets the major version component
73 74 75 |
# File 'lib/tins/string_version.rb', line 73 def major=(new_level) self[0] = new_level end |
#minor ⇒ Integer
Returns the minor version component
82 83 84 |
# File 'lib/tins/string_version.rb', line 82 def minor self[1] end |
#minor=(new_level) ⇒ Integer
Sets the minor version component
93 94 95 |
# File 'lib/tins/string_version.rb', line 93 def minor=(new_level) self[1] = new_level end |
#pred! ⇒ self
Decrements the last version component
202 203 204 205 |
# File 'lib/tins/string_version.rb', line 202 def pred! self[-1] -= 1 self end |
#revision ⇒ Integer
Returns the revision version component
122 123 124 |
# File 'lib/tins/string_version.rb', line 122 def revision self[3] end |
#revision=(new_level) ⇒ Integer
Sets the revision version component
133 134 135 |
# File 'lib/tins/string_version.rb', line 133 def revision=(new_level) self[3] = new_level end |
#succ! ⇒ self
Increments the last version component
194 195 196 197 |
# File 'lib/tins/string_version.rb', line 194 def succ! self[-1] += 1 self end |
#to_s ⇒ String Also known as: inspect
Returns the string representation of this version
239 240 241 |
# File 'lib/tins/string_version.rb', line 239 def to_s @version end |