2010-05-07 19:33:27 +00:00
|
|
|
# encoding: UTF-8
|
|
|
|
|
2009-10-26 19:34:45 +00:00
|
|
|
# --
|
2010-02-19 22:41:36 +00:00
|
|
|
# Copyright (C) 2008-2010 10gen Inc.
|
2009-10-26 19:34:45 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
# ++
|
2009-10-26 18:54:33 +00:00
|
|
|
module Mongo
|
|
|
|
# Simple class for comparing server versions.
|
|
|
|
class ServerVersion
|
|
|
|
include Comparable
|
|
|
|
|
|
|
|
def initialize(version)
|
|
|
|
@version = version
|
|
|
|
end
|
|
|
|
|
|
|
|
# Implements comparable.
|
|
|
|
def <=>(new)
|
|
|
|
local, new = self.to_a, to_array(new)
|
2009-12-16 19:03:15 +00:00
|
|
|
for n in 0...local.size do
|
2009-10-26 18:54:33 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
# Return an array representation of this server version.
|
|
|
|
def to_a
|
|
|
|
to_array(@version)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return a string representation of this server version.
|
|
|
|
def to_s
|
|
|
|
@version
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Returns true if any elements include mod symbols (-, +)
|
|
|
|
def elements_include_mods?(*elements)
|
2009-12-16 19:03:15 +00:00
|
|
|
elements.any? { |n| n =~ /[\-\+]/ }
|
2009-10-26 18:54:33 +00:00
|
|
|
end
|
|
|
|
|
2009-12-16 19:03:15 +00:00
|
|
|
# Converts argument to an array of integers,
|
2009-10-26 18:54:33 +00:00
|
|
|
# appending any mods as the final element.
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|