Class: Dependabot::Python::Version

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Version

Returns a new instance of Version.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dependabot/python/version.rb', line 27

def initialize(version)
  @version_string = version.to_s
  version, @local_version = version.split("+")
  version ||= ""
  version = version.gsub(/^v/, "")
  version = normalise_prerelease(version)
  version, @post_release_version = version.split(/\.r(?=\d)/)
  version ||= ""
  @local_version = normalise_prerelease(@local_version) if @local_version
  super
end

Instance Attribute Details

#local_versionObject (readonly)

Returns the value of attribute local_version.



13
14
15
# File 'lib/dependabot/python/version.rb', line 13

def local_version
  @local_version
end

#post_release_versionObject (readonly)

Returns the value of attribute post_release_version.



14
15
16
# File 'lib/dependabot/python/version.rb', line 14

def post_release_version
  @post_release_version
end

Class Method Details

.correct?(version) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  version.to_s.match?(ANCHORED_VERSION_PATTERN)
end

Instance Method Details

#<=>(other) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/dependabot/python/version.rb', line 47

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

  unless post_version_comparison(other).zero?
    return post_version_comparison(other)
  end

  local_version_comparison(other)
end

#inspectObject

:nodoc:



43
44
45
# File 'lib/dependabot/python/version.rb', line 43

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

#local_version_comparison(other) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dependabot/python/version.rb', line 72

def local_version_comparison(other)
  unless other.is_a?(Python::Version)
    return local_version.nil? ? 0 : 1
  end

  # Local version comparison works differently in Python: `1.0.beta`
  # compares as greater than `1.0`. To accommodate, we make the
  # strings the same length before comparing.
  lhsegments = local_version.to_s.split(".").map(&:downcase)
  rhsegments = other.local_version.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

#post_version_comparison(other) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dependabot/python/version.rb', line 58

def post_version_comparison(other)
  unless other.is_a?(Python::Version) && other.post_release_version
    return post_release_version.nil? ? 0 : 1
  end

  return -1 if post_release_version.nil?

  # Post release versions should only ever be a single number, so we can
  # just string-comparison them.
  return 0 if post_release_version.to_i == other.post_release_version.to_i

  post_release_version.to_i > other.post_release_version.to_i ? 1 : -1
end

#to_sObject



39
40
41
# File 'lib/dependabot/python/version.rb', line 39

def to_s
  @version_string
end