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

Parameters:

  • version_string (String)

    a semantic version number



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

def initialize(version_string)
  raise "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.



4
5
6
# File 'lib/searchlink/semver.rb', line 4

def maj
  @maj
end

#minObject

Returns the value of attribute min.



4
5
6
# File 'lib/searchlink/semver.rb', line 4

def min
  @min
end

#patchObject

Returns the value of attribute patch.



4
5
6
# File 'lib/searchlink/semver.rb', line 4

def patch
  @patch
end

#preObject

Returns the value of attribute pre.



4
5
6
# File 'lib/searchlink/semver.rb', line 4

def pre
  @pre
end

Instance Method Details

#<(other) ⇒ Object

See Also:



81
82
83
# File 'lib/searchlink/semver.rb', line 81

def <(other)
  older_than(other)
end

#==(other) ⇒ Object

See Also:



121
122
123
# File 'lib/searchlink/semver.rb', line 121

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

#>(other) ⇒ Object

See Also:



101
102
103
# File 'lib/searchlink/semver.rb', line 101

def >(other)
  newer_than(other)
end

#equal?(other) ⇒ Boolean

Test if self is equal to other

Parameters:

  • other (String, SemVer)

    The other semantic version number

Returns:

  • (Boolean)

    values are equal



112
113
114
115
116
# File 'lib/searchlink/semver.rb', line 112

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



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

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

Parameters:

  • other (String, SemVer)

    The semantic version number or SemVer object

Returns:

  • (Boolean)

    true if semver is newer



93
94
95
96
# File 'lib/searchlink/semver.rb', line 93

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

Parameters:

  • other (String, SemVer)

    The semantic version number or SemVer object

Returns:

  • (Boolean)

    true if semver is older



50
51
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
# File 'lib/searchlink/semver.rb', line 50

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



135
136
137
138
# File 'lib/searchlink/semver.rb', line 135

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