From 27092cabc4bda0904e1616bb2e5d4d949dbd3178 Mon Sep 17 00:00:00 2001 From: Jack Nagel Date: Thu, 2 Apr 2015 20:22:12 -0400 Subject: Switch PkgVersion to use composition 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. --- Library/Homebrew/pkg_version.rb | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) (limited to 'Library/Homebrew/pkg_version.rb') diff --git a/Library/Homebrew/pkg_version.rb b/Library/Homebrew/pkg_version.rb index 4e9e351ff..62072273a 100644 --- a/Library/Homebrew/pkg_version.rb +++ b/Library/Homebrew/pkg_version.rb @@ -1,36 +1,45 @@ -require 'version' +require "version" -class PkgVersion < Version - attr_reader :version, :revision +class PkgVersion + include Comparable RX = /\A(.+?)(?:_(\d+))?\z/ def self.parse(path) _, version, revision = *path.match(RX) - new(version, revision) + version = Version.new(version) + new(version, revision.to_i) end def initialize(version, revision) - super(version) + @version = version + @revision = version.head? ? 0 : revision + end - if head? - @revision = 0 - else - @revision = revision.to_i - end + def head? + version.head? end def to_s if revision > 0 "#{version}_#{revision}" else - version + version.to_s end end alias_method :to_str, :to_s def <=>(other) - return unless Version === other - super.nonzero? || revision <=> other.revision + 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 -- cgit v1.2.3