aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/pkg_version.rb
blob: b68d78cf8e8132e2001b697c6847a794787aca49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require "version"

class PkgVersion
  include Comparable

  RX = /\A(.+?)(?:_(\d+))?\z/

  attr_reader :version, :revision

  def self.parse(path)
    _, version, revision = *path.match(RX)
    version = Version.create(version)
    new(version, revision.to_i)
  end

  def initialize(version, revision)
    @version = version
    @revision = revision
  end

  def head?
    version.head?
  end

  def to_s
    if revision.positive?
      "#{version}_#{revision}"
    else
      version.to_s
    end
  end
  alias to_str to_s

  def <=>(other)
    return unless other.is_a?(PkgVersion)
    (version <=> other.version).nonzero? || revision <=> other.revision
  end
  alias eql? ==

  def hash
    version.hash ^ revision.hash
  end
end