Class: Version

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

Constant Summary collapse

InvalidVersion =
Class.new(ArgumentError)
InvalidRequire =
Class.new(ArgumentError)
MSGS =
{
  version: 'Unable to parse version: %p',
  require: 'Unable to parse requirement: %p',
}
VERSION =
/^(\d+)(?:\.(\d+))?(?:\.(\d+))?$/
REQUIRE =
/^(~>|>|>=|=|!=|<=|<) (\d+(?:\.\d+)?(?:\.\d+)?)$/

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Version

Returns a new instance of Version.



16
17
18
# File 'lib/dpl/support/version.rb', line 16

def initialize(str)
  @nums = split(str) || raise(InvalidVersion, MSGS[:version] % str)
end

Instance Method Details

#!=(other) ⇒ Object



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

def !=(other)
  trunc(other.size).to_a != other.to_a
end

#<=>(other) ⇒ Object



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

def <=>(other)
  to_a <=> other.to_a
end

#==(other) ⇒ Object



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

def ==(other)
  trunc(other.size).to_a == other.to_a
end

#bumpObject



54
55
56
57
58
59
# File 'lib/dpl/support/version.rb', line 54

def bump
  ix = nums[1] ? -2 : -1
  nums[ix] = nums[ix] + 1
  nums[-1] = nums[-1] = 0 if nums[1]
  self
end

#cloneObject



66
67
68
# File 'lib/dpl/support/version.rb', line 66

def clone
  Version.new(to_s)
end

#satisfies?(str) ⇒ Boolean

Returns:

  • (Boolean)


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

def satisfies?(str)
  send *parse(str) || raise(InvalidRequire, MSGS[:require] % str)
end

#sizeObject



24
25
26
# File 'lib/dpl/support/version.rb', line 24

def size
  nums.size
end

#to_aObject



28
29
30
# File 'lib/dpl/support/version.rb', line 28

def to_a
  nums
end

#to_sObject



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

def to_s
  nums.join('.')
end

#trunc(size) ⇒ Object



61
62
63
64
# File 'lib/dpl/support/version.rb', line 61

def trunc(size)
  @nums = nums[0..size - 1]
  self
end