
Comparing PkgVersion and Version objects can produce nonsensical results. For example, equality is not symmetric: irb(main):002:0> PkgVersion.new("1.0", 0) == Version.new("1.0") => false irb(main):003:0> Version.new("1.0") == PkgVersion.new("1.0", 0) => true Rather than attempt to deal with subclass-superclass equality, let's use composition and punt on the problem altogether.
46 lines
775 B
Ruby
46 lines
775 B
Ruby
require "version"
|
|
|
|
class PkgVersion
|
|
include Comparable
|
|
|
|
RX = /\A(.+?)(?:_(\d+))?\z/
|
|
|
|
def self.parse(path)
|
|
_, version, revision = *path.match(RX)
|
|
version = Version.new(version)
|
|
new(version, revision.to_i)
|
|
end
|
|
|
|
def initialize(version, revision)
|
|
@version = version
|
|
@revision = version.head? ? 0 : revision
|
|
end
|
|
|
|
def head?
|
|
version.head?
|
|
end
|
|
|
|
def to_s
|
|
if revision > 0
|
|
"#{version}_#{revision}"
|
|
else
|
|
version.to_s
|
|
end
|
|
end
|
|
alias_method :to_str, :to_s
|
|
|
|
def <=>(other)
|
|
return unless PkgVersion === other
|
|
(version <=> other.version).nonzero? || revision <=> other.revision
|
|
end
|
|
alias_method :eql?, :==
|
|
|
|
def hash
|
|
version.hash ^ revision.hash
|
|
end
|
|
|
|
protected
|
|
|
|
attr_reader :version, :revision
|
|
end
|