class Mongo::ServerVersion

Simple class for comparing server versions.

Public Class Methods

new(version) click to toggle source
# File lib/mongo/utils/server_version.rb, line 21
def initialize(version)
  @version = version
end

Public Instance Methods

<=>(new) click to toggle source

Implements comparable.

# File lib/mongo/utils/server_version.rb, line 26
def <=>(new)
  local, new  = self.to_a, to_array(new)
  for n in 0...local.size do
    break if elements_include_mods?(local[n], new[n])
    if local[n] < new[n].to_i
      result = -1
      break;
    elsif local[n] > new[n].to_i
      result = 1
      break;
    end
  end
  result || 0
end
to_a() click to toggle source

Return an array representation of this server version.

# File lib/mongo/utils/server_version.rb, line 42
def to_a
  to_array(@version)
end
to_s() click to toggle source

Return a string representation of this server version.

# File lib/mongo/utils/server_version.rb, line 47
def to_s
  @version
end

Private Instance Methods

elements_include_mods?(*elements) click to toggle source

Returns true if any elements include mod symbols (-, +)

# File lib/mongo/utils/server_version.rb, line 54
def elements_include_mods?(*elements)
  elements.any? { |n| n =~ /[\-\+]/ }
end
to_array(version) click to toggle source

Converts argument to an array of integers, appending any mods as the final element.

# File lib/mongo/utils/server_version.rb, line 60
def to_array(version)
  array = version.split(".").map {|n| (n =~ /^\d+$/) ? n.to_i : n }
  if array.last =~ /(\d+)([\-\+])/
    array[array.length-1] = $1.to_i
    array << $2
  end
  array
end