Class: Dependabot::SemVersion2

Inherits:
Object
  • Object
show all
Extended by:
T::Helpers, T::Sig
Includes:
Comparable
Defined in:
lib/dependabot/sem_version2.rb

Constant Summary collapse

SEMVER2_REGEX =
/^
  (0|[1-9]\d*)\. # major
  (0|[1-9]\d*)\. # minor
  (0|[1-9]\d*)   # patch
  (?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))? # pre release
  (?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))? # build metadata
$/x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ SemVersion2

Returns a new instance of SemVersion2.



38
39
40
41
42
43
44
45
# File 'lib/dependabot/sem_version2.rb', line 38

def initialize(version)
  tokens = parse(version)
  @major = T.let(T.must(tokens[:major]), String)
  @minor = T.let(T.must(tokens[:minor]), String)
  @patch = T.let(T.must(tokens[:patch]), String)
  @build = T.let(tokens[:build], T.nilable(String))
  @prerelease = T.let(tokens[:prerelease], T.nilable(String))
end

Instance Attribute Details

#buildObject

Returns the value of attribute build.



32
33
34
# File 'lib/dependabot/sem_version2.rb', line 32

def build
  @build
end

#majorObject

Returns the value of attribute major.



23
24
25
# File 'lib/dependabot/sem_version2.rb', line 23

def major
  @major
end

#minorObject

Returns the value of attribute minor.



26
27
28
# File 'lib/dependabot/sem_version2.rb', line 26

def minor
  @minor
end

#patchObject

Returns the value of attribute patch.



29
30
31
# File 'lib/dependabot/sem_version2.rb', line 29

def patch
  @patch
end

#prereleaseObject

Returns the value of attribute prerelease.



35
36
37
# File 'lib/dependabot/sem_version2.rb', line 35

def prerelease
  @prerelease
end

Class Method Details

.correct?(version) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/dependabot/sem_version2.rb', line 85

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

  version.match?(SEMVER2_REGEX)
end

Instance Method Details

#<=>(other) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dependabot/sem_version2.rb', line 71

def <=>(other)
  result = major.to_i <=> other.major.to_i
  return result unless result.zero?

  result = minor.to_i <=> other.minor.to_i
  return result unless result.zero?

  result = patch.to_i <=> other.patch.to_i
  return result unless result.zero?

  compare_prereleases(prerelease, other.prerelease)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/dependabot/sem_version2.rb', line 66

def eql?(other)
  other.is_a?(self.class) && to_s == other.to_s
end

#inspectObject



61
62
63
# File 'lib/dependabot/sem_version2.rb', line 61

def inspect
  "#<#{self.class} #{self}>"
end

#prerelease?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/dependabot/sem_version2.rb', line 48

def prerelease?
  !!prerelease
end

#to_sObject



53
54
55
56
57
58
# File 'lib/dependabot/sem_version2.rb', line 53

def to_s
  value = [major, minor, patch].join(".")
  value += "-#{prerelease}" if prerelease
  value += "+#{build}" if build
  value
end