diff options
| author | Jack Nagel | 2015-04-02 20:22:12 -0400 |
|---|---|---|
| committer | Jack Nagel | 2015-04-02 20:22:12 -0400 |
| commit | 27092cabc4bda0904e1616bb2e5d4d949dbd3178 (patch) | |
| tree | 16304967d4773c1942db4dec9e7a98f5ce941a17 /Library/Homebrew/pkg_version.rb | |
| parent | 1b5fc1fb02691f0868f855443f1393568fe2b465 (diff) | |
| download | brew-27092cabc4bda0904e1616bb2e5d4d949dbd3178.tar.bz2 | |
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.
Diffstat (limited to 'Library/Homebrew/pkg_version.rb')
| -rw-r--r-- | Library/Homebrew/pkg_version.rb | 35 |
1 files changed, 22 insertions, 13 deletions
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 |
