brew/Library/Homebrew/pkg_version.rb

57 lines
1.0 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "version"
2020-08-17 20:08:45 +02:00
# Combination of a version and a revision.
#
# @api private
class PkgVersion
include Comparable
extend Forwardable
2020-08-17 20:08:45 +02:00
REGEX = /\A(.+?)(?:_(\d+))?\z/.freeze
private_constant :REGEX
2016-07-13 10:11:59 +03:00
attr_reader :version, :revision
2020-08-17 20:08:45 +02:00
delegate [:major, :minor, :patch, :major_minor, :major_minor_patch] => :version
def self.parse(path)
2020-08-17 20:08:45 +02:00
_, version, revision = *path.match(REGEX)
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
2017-09-24 20:12:58 +01:00
if revision.positive?
"#{version}_#{revision}"
else
version.to_s
end
end
2016-09-23 18:13:48 +02:00
alias to_str to_s
def <=>(other)
2016-09-20 22:03:08 +02:00
return unless other.is_a?(PkgVersion)
2018-09-17 02:45:00 +02:00
2020-03-04 11:28:28 +00:00
version_comparison = (version <=> other.version)
return if version_comparison.nil?
2020-03-04 11:28:28 +00:00
version_comparison.nonzero? || revision <=> other.revision
end
2016-09-23 18:13:48 +02:00
alias eql? ==
def hash
version.hash ^ revision.hash
end
end