Class: Dependabot::Hex::Version

Inherits:
Version
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/hex/version.rb

Constant Summary collapse

VERSION_PATTERN =
T.let(Gem::Version::VERSION_PATTERN + '(\+[0-9a-zA-Z\-.]+)?', String)
ANCHORED_VERSION_PATTERN =
T.let(/\A\s*(#{VERSION_PATTERN})?\s*\z/, Regexp)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Version

Returns a new instance of Version.



32
33
34
35
36
37
38
39
# File 'lib/dependabot/hex/version.rb', line 32

def initialize(version)
  @version_string = T.let(version.to_s, String)

  version, @build_info = version.to_s.split("+") if version.to_s.include?("+")
  @build_info = T.let(@build_info, T.nilable(String))

  super
end

Instance Attribute Details

#build_infoObject (readonly)

Returns the value of attribute build_info.



19
20
21
# File 'lib/dependabot/hex/version.rb', line 19

def build_info
  @build_info
end

Class Method Details

.correct?(version) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/dependabot/hex/version.rb', line 25

def self.correct?(version)
  return false if version.nil?

  version.to_s.match?(ANCHORED_VERSION_PATTERN)
end

Instance Method Details

#<=>(other) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dependabot/hex/version.rb', line 52

def <=>(other)
  version_comparison = super
  return version_comparison unless version_comparison&.zero?

  return build_info.nil? ? 0 : 1 unless other.is_a?(Hex::Version)

  # Build information comparison
  lhsegments = build_info.to_s.split(".").map(&:downcase)
  rhsegments = other.build_info.to_s.split(".").map(&:downcase)
  limit = [lhsegments.count, rhsegments.count].min

  lhs = ["1", *lhsegments.first(limit)].join(".")
  rhs = ["1", *rhsegments.first(limit)].join(".")

  local_comparison = Gem::Version.new(lhs) <=> Gem::Version.new(rhs)

  return local_comparison unless local_comparison&.zero?

  lhsegments.count <=> rhsegments.count
end

#inspectObject

:nodoc:



47
48
49
# File 'lib/dependabot/hex/version.rb', line 47

def inspect # :nodoc:
  "#<#{self.class} #{@version_string}>"
end

#to_sObject



42
43
44
# File 'lib/dependabot/hex/version.rb', line 42

def to_s
  @version_string
end