Class: T2Server::Server::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/t2-server/server.rb

Overview

Represents a Taverna Server version number in a way that can be compared to other version numbers or strings.

This class mixes in Comparable so all the usual comparison operators work as expected.

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Version

:call-seq:

new(version_string) -> Version

Create a new Version object from the supplied version string.



398
399
400
401
# File 'lib/t2-server/server.rb', line 398

def initialize(version)
  @string = parse_version(version)
  @array = []
end

Instance Method Details

#<=>(other) ⇒ Object

:call-seq:

version <=> other -> -1, 0 or +1

Returns -1, 0 or +1 depending of whether version is less than, equal to or greater than other.

This is the basis for the tests in Comparable.



438
439
440
441
442
443
444
445
446
447
# File 'lib/t2-server/server.rb', line 438

def <=>(other)
  other = Version.new(other) if other.instance_of?(String)
  self.to_a.zip(other.to_a).each do |c|
    comp = c[0] <=> c[1]
    return comp unless comp == 0
  end

  # If we get here then we know we have equal version numbers.
  0
end

#to_aObject

:call-seq:

to_a -> Array

Convert this Version object into an array of numbers representing the components of the version number. The order of the components is:

  • Major

  • Minor

  • Patch

For example:

Version.new("2.5.1").to_a == [2, 5, 1]


422
423
424
425
426
427
428
429
# File 'lib/t2-server/server.rb', line 422

def to_a
  if @array.empty?
    comps = @string.split(".")
    @array = comps.map { |v| v.to_i }
  end

  @array
end

#to_sObject

:call-seq:

to_s -> String

Convert this Version object back into a String.



407
408
409
# File 'lib/t2-server/server.rb', line 407

def to_s
  @string
end