Class: SemanticRelease::Semver

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/semantic_release/semver.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major: 0, minor: 0, patch: 0, file: nil) ⇒ Semver

Returns a new instance of Semver.



11
12
13
14
15
16
# File 'lib/semantic_release/semver.rb', line 11

def initialize(major: 0, minor: 0, patch: 0, file: nil)
  @major = major
  @minor = minor
  @patch = patch
  @file = file
end

Instance Attribute Details

#majorObject (readonly)

Returns the value of attribute major.



9
10
11
# File 'lib/semantic_release/semver.rb', line 9

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



9
10
11
# File 'lib/semantic_release/semver.rb', line 9

def minor
  @minor
end

#patchObject (readonly)

Returns the value of attribute patch.



9
10
11
# File 'lib/semantic_release/semver.rb', line 9

def patch
  @patch
end

Class Method Details

.load(file) ⇒ Object



18
19
20
21
# File 'lib/semantic_release/semver.rb', line 18

def self.load(file)
  data = JSON.parse(File.read(file))
  new(major: data["major"], minor: data["minor"], patch: data["patch"], file: file)
end

Instance Method Details

#<=>(other) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/semantic_release/semver.rb', line 61

def <=>(other)
  i[major minor patch].each do |method|
    comparison = (send(method) <=> other.send(method))
    return comparison unless comparison.zero?
  end
  0
end

#increment(term) ⇒ Object



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

def increment(term)
  case term.to_sym
  when :major then increment_major
  when :minor then increment_minor
  when :patch then increment_patch
  else raise ArgumentError, "Bad term"
  end
  self
end

#increment_majorObject



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

def increment_major
  @major += 1
  @minor = 0
  @patch = 0
end

#increment_minorObject



44
45
46
47
# File 'lib/semantic_release/semver.rb', line 44

def increment_minor
  @minor += 1
  @patch = 0
end

#increment_patchObject



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

def increment_patch
  @patch += 1
end

#save(file = nil) ⇒ Object



23
24
25
26
# File 'lib/semantic_release/semver.rb', line 23

def save(file = nil)
  f = file || @file
  File.write(f, JSON.generate(to_h))
end

#to_hObject



57
58
59
# File 'lib/semantic_release/semver.rb', line 57

def to_h
  { major: @major, minor: @minor, patch: @patch }
end

#to_sObject



53
54
55
# File 'lib/semantic_release/semver.rb', line 53

def to_s
  [@major, @minor, @patch].join(".")
end