2012-07-09 22:51:10 -05:00
|
|
|
class Version
|
|
|
|
include Comparable
|
|
|
|
|
|
|
|
def initialize val
|
|
|
|
return val if val.is_a? Version or val.nil?
|
|
|
|
@version = val.to_s.strip
|
|
|
|
end
|
|
|
|
|
2012-07-09 23:00:40 -05:00
|
|
|
def head?
|
|
|
|
@version == 'HEAD'
|
|
|
|
end
|
|
|
|
|
2012-07-09 22:51:10 -05:00
|
|
|
def nums
|
|
|
|
@version.scan(/\d+/).map { |d| d.to_i }
|
|
|
|
end
|
|
|
|
|
|
|
|
def <=>(other)
|
2012-07-09 23:00:40 -05:00
|
|
|
return nil unless other.is_a? Version
|
|
|
|
return 0 if self.head? and other.head?
|
|
|
|
return 1 if self.head? and not other.head?
|
|
|
|
return -1 if not self.head? and other.head?
|
|
|
|
return 1 if other.nil?
|
|
|
|
|
|
|
|
snums = self.nums
|
|
|
|
onums = other.nums
|
|
|
|
|
|
|
|
count = [snums.length, onums.length].max
|
|
|
|
|
|
|
|
snums.fill(0, snums.length, count - snums.length)
|
|
|
|
onums.fill(0, onums.length, count - onums.length)
|
|
|
|
|
|
|
|
snums <=> onums
|
2012-07-09 22:51:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
@version
|
|
|
|
end
|
|
|
|
alias_method :to_str, :to_s
|
|
|
|
|
|
|
|
def self.parse spec
|
|
|
|
Pathname.new(spec.to_s).version
|
|
|
|
end
|
|
|
|
end
|