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'
}.freeze
VERSION =
/^(\d+)(?:\.(\d+))?(?:\.(\d+))?$/
REQUIRE =
/^(~>|>|>=|=|!=|<=|<) (\d+(?:\.\d+)?(?:\.\d+)?)$/

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Version

Returns a new instance of Version.



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

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

Instance Method Details

#!=(other) ⇒ Object



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

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

#<=>(other) ⇒ Object



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

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

#==(other) ⇒ Object



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

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

#bumpObject



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

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

#cloneObject



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

def clone
  Version.new(to_s)
end

#satisfies?(str) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#sizeObject



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

def size
  nums.size
end

#to_aObject



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

def to_a
  nums
end

#to_sObject



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

def to_s
  nums.join('.')
end

#trunc(size) ⇒ Object



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

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