Class: IPT::Version

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

Overview

Basic version implementation

A version has one of the following forms:

* MAJOR
* MAJOR.MINOR
* MAJOR.MINOR.PATCH
* MAJOR.MINOR.PATCH.BUILD

Usage

version = Version.parse("2.3.9")
version.major # => 2

puts version # => "2.3.9"

Constant Summary collapse

PARTS =
[:major, :minor, :patch, :build]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Version.



32
33
34
# File 'lib/ipt/version.rb', line 32

def initialize(major, minor = nil, patch = nil, build = nil)
  @major, @minor, @patch, @build = major.to_i, minor ? minor.to_i : nil, patch ? patch.to_i : nil, build ? build.to_i : nil
end

Class Method Details

.parse(str) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
# File 'lib/ipt/version.rb', line 22

def parse(str)
  raise ArgumentError.new("Invalid version string: #{str}") unless str && str =~ /^[0-9]+(\.[0-9]+)?/
  Version.new(*str.split("."))
end

Instance Method Details

#<=>(other) ⇒ Object

Comparable



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ipt/version.rb', line 61

def <=>(other)
  return nil unless other
  PARTS.each do |part|
    return nil unless other.respond_to?(part)
    vo = other.send(part)
    so = self.send(part)
    return -1 if so.nil? && vo
    return 1 if vo.nil? && so
    return 0 if vo.nil? && so.nil?
    c = (so <=> vo)
    return c unless c == 0
  end
  0
end

#==(other) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/ipt/version.rb', line 48

def ==(other)
  return false unless other.is_a?(IPT::Version)
  PARTS.each do |part|
    return false unless self.send(part).eql?(other.send(part))
  end
  true
end

#decrease!(what = nil) ⇒ Object



40
41
42
# File 'lib/ipt/version.rb', line 40

def decrease!(what = nil)
  change(what){|old| old - 1}
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/ipt/version.rb', line 56

def eql?(other)
  self == other
end

#increase!(what = nil) ⇒ Object



36
37
38
# File 'lib/ipt/version.rb', line 36

def increase!(what = nil)
  change(what){|old| old + 1}
end

#to_sObject



44
45
46
# File 'lib/ipt/version.rb', line 44

def to_s
  PARTS.map{|part| self.send(part)}.compact.join('.')
end