Class: Tagit::Version

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

Constant Summary collapse

VERSION_REGEX =
/^v([0-9]+)\.([0-9]+)(?:\.([0-9]+))?$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major, minor, patch = nil) ⇒ Version

Returns a new instance of Version.



10
11
12
13
# File 'lib/tagit/version.rb', line 10

def initialize(major, minor, patch = nil)
  @major, @minor= major.to_i, minor.to_i
  @patch = patch.to_i if patch
end

Instance Attribute Details

#majorObject (readonly)

Returns the value of attribute major.



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

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



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

def minor
  @minor
end

#patchObject (readonly)

Returns the value of attribute patch.



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

def patch
  @patch
end

Class Method Details

.allObject



24
25
26
27
28
29
30
# File 'lib/tagit/version.rb', line 24

def self.all
  lines = git_tags.split("\n")
  lines.inject([]) do |results, line|
    results << Version.new($1, $2, $3) if VERSION_REGEX.match(line)
    results
  end
end

.currentObject



20
21
22
# File 'lib/tagit/version.rb', line 20

def self.current
  all.sort.last
end

.from_s(str) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
# File 'lib/tagit/version.rb', line 15

def self.from_s(str)
  raise ArgumentError, "Not a conventional version string - '#{str}'" unless VERSION_REGEX.match(str)
  Version.new($1, $2, $3)
end

Instance Method Details

#<=>(other) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/tagit/version.rb', line 32

def <=> other
  return 1 if other.nil?

  [:major, :minor, :patch].each do |sym|
    compare = ((self.send(sym) || 0) <=> (other.send(sym) || 0))
    return compare unless compare == 0 && sym != :patch
  end
end

#to_sObject



41
42
43
# File 'lib/tagit/version.rb', line 41

def to_s
  "v#{major}.#{minor}#{".#{patch}" if patch}"
end