Class: SemVer

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

Defined Under Namespace

Classes: InvalidVersion

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ SemVer

Returns a new instance of SemVer.



10
11
12
# File 'lib/sem_ver.rb', line 10

def initialize(spec)
  @spec = spec
end

Class Method Details

.parse(*args) ⇒ Object



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

def self.parse(*args)
  new(*args)
end

Instance Method Details

#<=>(other) ⇒ Object

Raises:



34
35
36
37
# File 'lib/sem_ver.rb', line 34

def <=>(other)
  raise InvalidVersion.new(to_s) unless valid? && other.valid?
  parts <=> other.parts
end

#inspectObject



46
47
48
# File 'lib/sem_ver.rb', line 46

def inspect
  "<#{'Invalid' unless valid?}Version: #{to_s}>"
end

#majorObject



14
15
16
# File 'lib/sem_ver.rb', line 14

def major
  match && match[1].to_i
end

#minorObject



18
19
20
# File 'lib/sem_ver.rb', line 18

def minor
  match && match[2].to_i
end

#patchObject



22
23
24
# File 'lib/sem_ver.rb', line 22

def patch
  match && match[3].to_i
end

#prereleaseObject



26
27
28
# File 'lib/sem_ver.rb', line 26

def prerelease
  match && match[5]
end

#to_sObject



39
40
41
42
43
44
# File 'lib/sem_ver.rb', line 39

def to_s
  return @spec unless valid?
  "#{major}.#{minor}.#{patch}".tap do |v|
    v << "-#{prerelease}" if prerelease
  end
end

#valid?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/sem_ver.rb', line 30

def valid?
  !!match
end