Class: Vump::Semver

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

Overview

Representation of version notation according to semver.org/

Instance Method Summary collapse

Constructor Details

#initialize(string = nil) ⇒ Semver

Returns a new instance of Semver.



4
5
6
7
8
# File 'lib/vump/semver/semver.rb', line 4

def initialize(string = nil)
  @pre = @build = false
  @major = @minor = @patch = 0
  load string if string
end

Instance Method Details

#bump_majorObject



43
44
45
46
# File 'lib/vump/semver/semver.rb', line 43

def bump_major
  reset :@minor
  @major += 1
end

#bump_minorObject



38
39
40
41
# File 'lib/vump/semver/semver.rb', line 38

def bump_minor
  reset :@patch
  @minor += 1
end

#bump_patchObject



33
34
35
36
# File 'lib/vump/semver/semver.rb', line 33

def bump_patch
  reset :@pre
  @patch += 1
end

#load(string) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/vump/semver/semver.rb', line 10

def load(string)
  # <numeral>[-<sufix>]
  version, sufix = string
                   .match(/([\d\.]+)(?:\-)?(.*)?/)
                   .captures
  # <sufix>:= [<pre>][+<build>]
  @pre, @build = sufix.split('+', 2).map { |s| s.empty? ? false : s }
  # <numeral>:= <major>.<minor>.<patch>
  @major, @minor, @patch = version
                           .match(/(\d+)\.(\d+)\.(\d+)/)
                           .captures
                           .map(&:to_i)
end

#reset(what) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/vump/semver/semver.rb', line 24

def reset(what)
  levels = %i[@build @pre @patch @minor @major]
  # tag to false, version to 0
  reset_to = %i[@build @pre].include?(what) ? false : 0
  instance_variable_set(what, reset_to)
  # reset lesser sections
  reset levels[levels.index(what) - 1] if levels.index(what) != 0
end

#to_sObject



48
49
50
51
52
53
# File 'lib/vump/semver/semver.rb', line 48

def to_s
  str = "#{@major}.#{@minor}.#{@patch}"
  str << "-#{@pre}" if @pre
  str << "+#{@build}" if @build
  str
end