Class: Invoker::Version

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ Version

Returns a new instance of Version.



6
7
8
9
10
11
# File 'lib/invoker/version.rb', line 6

def initialize(number)
  t_major, t_minor, t_patch = number.split('.')
  @major = t_major.to_i
  @minor = t_minor.to_i
  @patch = t_patch.to_i
end

Instance Attribute Details

#majorObject (readonly)

Returns the value of attribute major.



4
5
6
# File 'lib/invoker/version.rb', line 4

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



4
5
6
# File 'lib/invoker/version.rb', line 4

def minor
  @minor
end

#patchObject (readonly)

Returns the value of attribute patch.



4
5
6
# File 'lib/invoker/version.rb', line 4

def patch
  @patch
end

Instance Method Details

#<=>(version) ⇒ Object



17
18
19
20
21
# File 'lib/invoker/version.rb', line 17

def <=>(version)
  (major.to_i <=> version.major.to_i).nonzero? ||
    (minor.to_i <=> version.minor.to_i).nonzero? ||
    patch.to_i <=> version.patch.to_i
end

#matches?(operator, number) ⇒ Boolean

Returns:

  • (Boolean)


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

def matches?(operator, number)
  version = Version.new(number)
  self == version

  return self == version if operator == '='
  return self > version  if operator == '>'
  return self < version  if operator == '<'
  return version <= self && version.next > self if operator  == '~>'
end

#nextObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/invoker/version.rb', line 33

def next
  next_splits = to_a

  if next_splits.length == 1
    next_splits[0] += 1
  else
    next_splits[-2] += 1
    next_splits[-1] = 0
  end

  Version.new(next_splits.join('.'))
end

#to_aObject



13
14
15
# File 'lib/invoker/version.rb', line 13

def to_a
  [major, minor, patch].compact
end