Class: Dependabot::Nuget::Version

Inherits:
Gem::Version
  • Object
show all
Defined in:
lib/dependabot/nuget/version.rb

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Version

Returns a new instance of Version.



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

def initialize(version)
  @version_string = version.to_s

  version = version.to_s.split("+").first if version.to_s.include?("+")

  super
end

Class Method Details

.correct?(version) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/dependabot/nuget/version.rb', line 16

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

  version.to_s.match?(ANCHORED_VERSION_PATTERN)
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

rubocop:enable Metrics/PerceivedComplexity rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/AbcSize



94
95
96
97
98
99
100
101
# File 'lib/dependabot/nuget/version.rb', line 94

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+$/)

  lhs.upcase <=> rhs.upcase
end

#compare_prerelease_part(other) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/AbcSize



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dependabot/nuget/version.rb', line 55

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

  other_release_str = other.to_s.split("-").first&.split("+")&.first || ""
  other_prerelease_string = other.to_s.
                            sub(other_release_str, "").
                            sub("-", "").
                            split("+").
                            first
  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 = prerelease_string.split(".")
  other_split_prerelease_string = 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



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

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

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

#inspectObject

:nodoc:



34
35
36
# File 'lib/dependabot/nuget/version.rb', line 34

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

#to_sObject



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

def to_s
  @version_string
end