Class: Tins::StringVersion::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/tins/string_version.rb

Overview

Represents a version string with semantic comparison capabilities

Examples:

Creating a Version object

version = Tins::StringVersion::Version.new("1.2.3")

Accessing components

v = "1.2.3".version
puts v.major # => 1
puts v.minor # => 2
puts v.build # => 3

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Version

Creates a new version object from a string representation

Examples:

Tins::StringVersion::Version.new("1.2.3")
# => #<Tins::StringVersion::Version:0x00007f8b8c0b0a80 @version="1.2.3">

Parameters:

  • string (String)

    The version string (e.g., “1.2.3”)

Raises:

  • (ArgumentError)

    If the string doesn’t match a valid version pattern



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

Parameters:

Returns:

  • (Integer)

    -1 if self < other, 0 if equal, 1 if self > other



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

Parameters:

Returns:

  • (Boolean)

    true if versions are equal



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

Parameters:

  • level (Symbol, Integer)

    The level to retrieve

Returns:

  • (Integer)

    The version component value



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

Parameters:

  • level (Symbol, Integer)

    The level to set

  • value (Integer)

    The new value for the component

Returns:

  • (Integer)

    The updated value

Raises:

  • (ArgumentError)

    If value is negative



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

#arrayArray<Integer> Also known as: to_a

Converts the version to an array of integers

Returns:

  • (Array<Integer>)

    Array representation of the version



238
239
240
# File 'lib/tins/string_version.rb', line 238

def array
  @version.split(?.).map(&:to_i)
end

#buildInteger Also known as: patch

Returns the build version component

Examples:

"1.2.3".version.build # => 3

Returns:

  • (Integer)

    The build version number



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

Examples:

v = "1.2.3".version
v.build = 5 # sets build to 5

Parameters:

  • new_level (Integer)

    The new build version number

Returns:

  • (Integer)

    The updated build version number



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

Examples:

v = "1.2.3".version
v.bump(:minor) # => "1.3.0"
v.bump         # => "1.3.1" (bumps last component)

Parameters:

  • level (Symbol, Integer) (defaults to: array.size - 1)

    The level to bump (default: last component)

Returns:

  • (self)

    Returns self for chaining



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

Parameters:



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

Parameters:

  • specifier (Symbol, Integer)

    The level specification

Returns:

  • (Integer)

    The corresponding 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

#majorInteger

Returns the major version component

Examples:

"1.2.3".version.major # => 1

Returns:

  • (Integer)

    The major version number



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

Examples:

v = "1.2.3".version
v.major = 5 # sets major to 5

Parameters:

  • new_level (Integer)

    The new major version number

Returns:

  • (Integer)

    The updated major version number



76
77
78
# File 'lib/tins/string_version.rb', line 76

def major=(new_level)
  self[0] = new_level
end

#minorInteger

Returns the minor version component

Examples:

"1.2.3".version.minor # => 2

Returns:

  • (Integer)

    The minor version number



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

Examples:

v = "1.2.3".version
v.minor = 5 # sets minor to 5

Parameters:

  • new_level (Integer)

    The new minor version number

Returns:

  • (Integer)

    The updated minor version number



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

Returns:

  • (self)

    Returns self for chaining



211
212
213
214
# File 'lib/tins/string_version.rb', line 211

def pred!
  self[-1] -= 1
  self
end

#revisionInteger

Returns the revision version component

Examples:

"1.2.3".version.revision # => nil (no revision component)

Returns:

  • (Integer)

    The revision version number



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

Examples:

v = "1.2.3".version
v.revision = 5 # sets revision to 5

Parameters:

  • new_level (Integer)

    The new revision version number

Returns:

  • (Integer)

    The updated revision version number



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

Returns:

  • (self)

    Returns self for chaining



203
204
205
206
# File 'lib/tins/string_version.rb', line 203

def succ!
  self[-1] += 1
  self
end

#to_sString Also known as: inspect

Returns the string representation of this version

Returns:

  • (String)

    The version string



248
249
250
# File 'lib/tins/string_version.rb', line 248

def to_s
  @version
end