[Version] Add a util class that can be used to compare versions.

This commit is contained in:
Eloy Durán
2014-02-26 13:27:29 +01:00
committed by Watson
parent 2725ead68c
commit f2ba642a51

View File

@@ -0,0 +1,47 @@
module Motion; module Util
class Version
include Comparable
def initialize(version)
@version = version.to_s
unless @version =~ /^[.0-9]+$/
raise ArgumentError, "A version may only contain periods and digits."
end
end
def to_s
@version
end
def segments
@segments ||= @version.split('.').map(&:to_i)
end
# This is pretty much vendored straight from RubyGems, except we dont
# care about string segments for our purposes.
#
# https://github.com/rubygems/rubygems/blob/81d806d818baeb5dcb6398ca631d772a003d078e/lib/rubygems/version.rb
#
def <=>(other)
return unless Version === other
return 0 if @version == other.to_s
lhsegments = segments
rhsegments = other.segments
lhsize = lhsegments.size
rhsize = rhsegments.size
limit = (lhsize > rhsize ? lhsize : rhsize) - 1
i = 0
while i <= limit
lhs, rhs = lhsegments[i] || 0, rhsegments[i] || 0
i += 1
next if lhs == rhs
return lhs <=> rhs
end
return 0
end
end
end; end