Class: AptControl::Version

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major, minor, bugfix, debian) ⇒ Version

Returns a new instance of Version.



26
27
28
29
30
31
# File 'lib/apt_control.rb', line 26

def initialize(major, minor, bugfix, debian)
  @major = major && major.to_i
  @minor = minor && minor.to_i
  @bugfix = bugfix && bugfix.to_i
  @debian = debian
end

Instance Attribute Details

#bugfixObject (readonly)

Returns the value of attribute bugfix.



19
20
21
# File 'lib/apt_control.rb', line 19

def bugfix
  @bugfix
end

#debianObject (readonly)

Returns the value of attribute debian.



19
20
21
# File 'lib/apt_control.rb', line 19

def debian
  @debian
end

#majorObject (readonly)

Returns the value of attribute major.



19
20
21
# File 'lib/apt_control.rb', line 19

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



19
20
21
# File 'lib/apt_control.rb', line 19

def minor
  @minor
end

Class Method Details

.parse(string) ⇒ Object



21
22
23
24
# File 'lib/apt_control.rb', line 21

def self.parse(string)
  match = /([0-9]+)(?:\.([0-9]+))?(?:\.([0-9]+))?(?:-(.+))?/.match(string)
  match && new(*(1..4).map { |i| match[i] }) or raise "could not parse #{string}"
end

Instance Method Details

#<=>(rhs) ⇒ Object



37
38
39
# File 'lib/apt_control.rb', line 37

def <=>(rhs)
  self.to_a.compact <=> rhs.to_a.compact
end

#==(rhs) ⇒ Object



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

def ==(rhs)
  self.to_a == rhs.to_a
end

#=~(rhs) ⇒ Object



45
46
47
# File 'lib/apt_control.rb', line 45

def =~(rhs)
  self.to_a[0...3] == rhs.to_a[0...3]
end

#satisfies_exactly(rhs) ⇒ Object

operator

returns true if this version satisfies the given rule and version spec, where all parts of the version given match our parts. Not commutative, as 1.3.1.4 satisfies 1.3, but 1.3 does not satisfy 1.3.1.4



53
54
55
56
57
58
# File 'lib/apt_control.rb', line 53

def satisfies_exactly(rhs)
  rhs.to_a.compact.zip(self.to_a).each do |rhs_part, lhs_part|
    return false unless rhs_part == lhs_part
  end
  return true
end

#satisfies_loosely(rhs) ⇒ Object

>= operator returns true if this version is greater than or equal to the given version



62
63
64
65
66
# File 'lib/apt_control.rb', line 62

def satisfies_loosely(rhs)
  return true if satisfies_exactly(rhs)
  return true if (self.to_a.compact <=> rhs.to_a.compact) >= 0
  return false
end

#satisfies_pessimisticly(rhs) ⇒ Object

~> operator



69
70
71
72
73
74
75
76
77
# File 'lib/apt_control.rb', line 69

def satisfies_pessimisticly(rhs)

  return false unless self.to_a[0...2] == rhs.to_a[0...2]

  lhs_half = self.to_a[2..-1]
  rhs_half = rhs.to_a[2..-1]

  (lhs_half.compact <=> rhs_half.compact) >= 0
end

#to_aObject



33
34
35
# File 'lib/apt_control.rb', line 33

def to_a
  [@major, @minor, @bugfix, @debian]
end

#to_sObject



79
80
81
82
83
84
85
# File 'lib/apt_control.rb', line 79

def to_s
  [
    "#{major}.#{minor}",
    bugfix && ".#{bugfix}",
    debian && "-#{debian}"
  ].compact.join
end