Class: SL::SemVer

Inherits:
Object
  • Object
show all
Defined in:
lib/searchlink/semver.rb

Overview

Semantic versioning library

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_string) ⇒ SemVer

Initialize a Semantic Version object

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/searchlink/semver.rb', line 14

def initialize(version_string)
  raise VersionError, "Invalid semantic version number: #{version_string}" unless version_string.valid_version?

  @maj, @min, @patch = version_string.split(/\./)
  @pre = nil
  if @patch =~ /(-?[^0-9]+\d*)$/
    @pre = Regexp.last_match(1).sub(/^-/, "")
    @patch = @patch.sub(/(-?[^0-9]+\d*)$/, "")
  end

  @maj = @maj.to_i
  @min = @min.to_i
  @patch = @patch.to_i
end

Instance Attribute Details

#majObject

Returns the value of attribute maj.



6
7
8
# File 'lib/searchlink/semver.rb', line 6

def maj
  @maj
end

#minObject

Returns the value of attribute min.



6
7
8
# File 'lib/searchlink/semver.rb', line 6

def min
  @min
end

#patchObject

Returns the value of attribute patch.



6
7
8
# File 'lib/searchlink/semver.rb', line 6

def patch
  @patch
end

#preObject

Returns the value of attribute pre.



6
7
8
# File 'lib/searchlink/semver.rb', line 6

def pre
  @pre
end

Instance Method Details

#<(other) ⇒ Object

See Also:



83
84
85
# File 'lib/searchlink/semver.rb', line 83

def <(other)
  older_than(other)
end

#==(other) ⇒ Object

See Also:



123
124
125
# File 'lib/searchlink/semver.rb', line 123

def ==(other)
  equal?(other)
end

#>(other) ⇒ Object

See Also:



103
104
105
# File 'lib/searchlink/semver.rb', line 103

def >(other)
  newer_than(other)
end

#equal?(other) ⇒ Boolean

Test if self is equal to other



114
115
116
117
118
# File 'lib/searchlink/semver.rb', line 114

def equal?(other)
  v = other.is_a?(SemVer) ? other : SemVer.new(other)

  v.maj == @maj && v.min == @min && v.patch == @patch && v.pre == @pre
end

#inspectObject



127
128
129
130
131
132
133
134
135
# File 'lib/searchlink/semver.rb', line 127

def inspect
  {
    object_id: object_id,
    maj: @maj,
    min: @min,
    patch: @patch,
    pre: @pre
  }
end

#newer_than(other) ⇒ Boolean

Test if self is newer than a semantic version number



95
96
97
98
# File 'lib/searchlink/semver.rb', line 95

def newer_than(other)
  v = other.is_a?(SemVer) ? other : SemVer.new(other)
  v.older_than(self) && !v.equal?(self)
end

#older_than(other) ⇒ Boolean

Test if self is older than a semantic version number



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/searchlink/semver.rb', line 52

def older_than(other)
  latest = other.is_a?(SemVer) ? other : SemVer.new(other)

  return false if latest.equal?(self)

  if @maj > latest.maj
    false
  elsif @maj < latest.maj
    true
  elsif @min > latest.min
    false
  elsif @min < latest.min
    true
  elsif @patch > latest.patch
    false
  elsif @patch < latest.patch
    true
  else
    return false if @pre.nil? && latest.pre.nil?

    return true if @pre.nil? && !latest.pre.nil?

    return false if !@pre.nil? && latest.pre.nil?

    @pre < latest.pre
  end
end

#to_sObject



137
138
139
140
# File 'lib/searchlink/semver.rb', line 137

def to_s
  ver = [@maj, @min, @patch].join(".")
  @pre.nil? ? ver : "#{ver}-#{@pre}"
end