Class: Packages::SemVer

Inherits:
Object
  • Object
show all
Defined in:
app/models/packages/sem_ver.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major = 0, minor = 0, patch = 0, prerelease = nil, build = nil, prefixed: false) ⇒ SemVer

TODO: Move logic into the SemanticVersionable concern gitlab.com/gitlab-org/gitlab/-/issues/455670



9
10
11
12
13
14
15
16
# File 'app/models/packages/sem_ver.rb', line 9

def initialize(major = 0, minor = 0, patch = 0, prerelease = nil, build = nil, prefixed: false)
  @major = major
  @minor = minor
  @patch = patch
  @prerelease = prerelease
  @build = build
  @prefixed = prefixed
end

Instance Attribute Details

#buildObject

Returns the value of attribute build.



4
5
6
# File 'app/models/packages/sem_ver.rb', line 4

def build
  @build
end

#majorObject

Returns the value of attribute major.



4
5
6
# File 'app/models/packages/sem_ver.rb', line 4

def major
  @major
end

#minorObject

Returns the value of attribute minor.



4
5
6
# File 'app/models/packages/sem_ver.rb', line 4

def minor
  @minor
end

#patchObject

Returns the value of attribute patch.



4
5
6
# File 'app/models/packages/sem_ver.rb', line 4

def patch
  @patch
end

#prereleaseObject

Returns the value of attribute prerelease.



4
5
6
# File 'app/models/packages/sem_ver.rb', line 4

def prerelease
  @prerelease
end

Class Method Details

.match(str, prefixed: false) ⇒ Object



39
40
41
42
43
44
45
# File 'app/models/packages/sem_ver.rb', line 39

def self.match(str, prefixed: false)
  return unless str&.start_with?('v') == prefixed

  str = str[1..] if prefixed

  Gitlab::Regex.semver_regex.match(str)
end

.match?(str, prefixed: false) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/models/packages/sem_ver.rb', line 47

def self.match?(str, prefixed: false)
  !match(str, prefixed: prefixed).nil?
end

.parse(str, prefixed: false) ⇒ Object



51
52
53
54
55
56
# File 'app/models/packages/sem_ver.rb', line 51

def self.parse(str, prefixed: false)
  m = match str, prefixed: prefixed
  return unless m

  new(m[1].to_i, m[2].to_i, m[3].to_i, m[4], m[5], prefixed: prefixed)
end

Instance Method Details

#==(other) ⇒ Object



22
23
24
25
26
27
28
29
# File 'app/models/packages/sem_ver.rb', line 22

def ==(other)
  self.class == other.class &&
    self.major == other.major &&
    self.minor == other.minor &&
    self.patch == other.patch &&
    self.prerelease == other.prerelease &&
    self.build == other.build
end

#prefixed?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'app/models/packages/sem_ver.rb', line 18

def prefixed?
  @prefixed
end

#to_sObject



31
32
33
34
35
36
37
# File 'app/models/packages/sem_ver.rb', line 31

def to_s
  s = "#{prefixed? ? 'v' : ''}#{major || 0}.#{minor || 0}.#{patch || 0}"
  s += "-#{prerelease}" if prerelease
  s += "+#{build}" if build

  s
end