Class: Dependabot::Nuget::Version

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

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Version

Returns a new instance of Version.



28
29
30
31
32
33
# File 'lib/dependabot/nuget/version.rb', line 28

def initialize(version)
  version = version.to_s.split("+").first || ""
  @version_string = T.let(version, String)

  super
end

Class Method Details

.correct?(version) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/dependabot/nuget/version.rb', line 21

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

  version.to_s.match?(ANCHORED_VERSION_PATTERN)
end

.new(version) ⇒ Object



36
37
38
# File 'lib/dependabot/nuget/version.rb', line 36

def self.new(version)
  T.cast(super, Dependabot::Nuget::Version)
end

Instance Method Details

#<=>(other) ⇒ Object



51
52
53
54
55
56
# File 'lib/dependabot/nuget/version.rb', line 51

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

  compare_prerelease_part(other)
end

#compare_dot_separated_part(lhs, rhs) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/dependabot/nuget/version.rb', line 101

def compare_dot_separated_part(lhs, rhs)
  return -1 if lhs.nil?
  return 1 if rhs.nil?

  return lhs.to_i <=> rhs.to_i if lhs.match?(/^\d+$/) && rhs.match?(/^\d+$/)

  T.must(lhs.upcase <=> rhs.upcase)
end

#compare_prerelease_part(other) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dependabot/nuget/version.rb', line 68

def compare_prerelease_part(other)
  release_str = @version_string.split("-").first || ""
  prerelease_string = @version_string
                      .sub(release_str, "")
                      .sub("-", "")
  prerelease_string = nil if prerelease_string == ""

  other_release_str = other.to_s.split("-").first || ""
  other_prerelease_string = other.to_s
                                 .sub(other_release_str, "")
                                 .sub("-", "")
  other_prerelease_string = nil if other_prerelease_string == ""

  return -1 if prerelease_string && !other_prerelease_string
  return 1 if !prerelease_string && other_prerelease_string
  return 0 if !prerelease_string && !other_prerelease_string

  split_prerelease_string = T.must(prerelease_string).split(".")
  other_split_prerelease_string = T.must(other_prerelease_string).split(".")

  length = [split_prerelease_string.length, other_split_prerelease_string.length].max - 1
  (0..length).to_a.each do |index|
    lhs = split_prerelease_string[index]
    rhs = other_split_prerelease_string[index]
    result = compare_dot_separated_part(lhs, rhs)
    return result unless result.zero?
  end

  0
end

#compare_release(other) ⇒ Object



59
60
61
62
63
64
# File 'lib/dependabot/nuget/version.rb', line 59

def compare_release(other)
  release_str = @version_string.split("-").first || ""
  other_release_str = other.to_s.split("-").first || ""

  T.must(Gem::Version.new(release_str) <=> Gem::Version.new(other_release_str))
end

#inspectObject

:nodoc:



46
47
48
# File 'lib/dependabot/nuget/version.rb', line 46

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

#to_sObject



41
42
43
# File 'lib/dependabot/nuget/version.rb', line 41

def to_s
  @version_string
end