Class: Vump::Semver

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string = nil) ⇒ Semver

Returns a new instance of Semver.



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

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

Instance Attribute Details

#buildObject

Returns the value of attribute build.



3
4
5
# File 'lib/vump/semver/semver.rb', line 3

def build
  @build
end

#majorObject

Returns the value of attribute major.



3
4
5
# File 'lib/vump/semver/semver.rb', line 3

def major
  @major
end

#minorObject

Returns the value of attribute minor.



3
4
5
# File 'lib/vump/semver/semver.rb', line 3

def minor
  @minor
end

#patchObject

Returns the value of attribute patch.



3
4
5
# File 'lib/vump/semver/semver.rb', line 3

def patch
  @patch
end

#preObject

Returns the value of attribute pre.



3
4
5
# File 'lib/vump/semver/semver.rb', line 3

def pre
  @pre
end

Instance Method Details

#bump(what) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/vump/semver/semver.rb', line 53

def bump(what)
  case what
  when :patch
    bump_patch
  when :minor
    bump_minor
  when :major
    bump_major
  end
end

#bump_majorObject



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

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

#bump_minorObject



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

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

#bump_patchObject



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

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

#load(string) ⇒ Object



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

def load(string)
  # <numeral>[-<pre>][+<build>]
  version, @pre, @build = string
    .match(/([\d\.]+)(?:\-)?([^\+]*)(?:\+)?(.*)?/)
    .to_a
    .drop(1)
  # <numeral>:= <major>.<minor>.<patch>
  @major, @minor, @patch = version
    .match(/(\d+)\.(\d+)\.(\d+)/)
    .to_a
    .drop(1)
    .map(&:to_i)
end

#reset(what) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/vump/semver/semver.rb', line 29

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



64
65
66
67
68
69
# File 'lib/vump/semver/semver.rb', line 64

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

#valid?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/vump/semver/semver.rb', line 25

def valid?
  [@major, @minor, @patch].all? { |v| v.is_a?(Numeric) }
end