Class: DebVersion::DebianVersion

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ DebianVersion

Returns a new instance of DebianVersion.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/deb_version/compare.rb', line 9

def initialize(version)
  @version = version
  @epoch = 0
  @debian_revision = ""

  if @version.include? ":"
    @epoch, @version = @version.split(":")
    @epoch = @epoch.to_i
  end

  if @version.include? "-"
    @upstream_version, _, @debian_revision = @version.rpartition("-")
  else
    @upstream_version = @version
  end
end

Instance Attribute Details

#debian_revisionObject (readonly)

Returns the value of attribute debian_revision.



5
6
7
# File 'lib/deb_version/compare.rb', line 5

def debian_revision
  @debian_revision
end

#epochObject (readonly)

Returns the value of attribute epoch.



5
6
7
# File 'lib/deb_version/compare.rb', line 5

def epoch
  @epoch
end

#upstream_versionObject (readonly)

Returns the value of attribute upstream_version.



5
6
7
# File 'lib/deb_version/compare.rb', line 5

def upstream_version
  @upstream_version
end

Instance Method Details

#<=>(other) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/deb_version/compare.rb', line 74

def <=>(other)
  return epoch <=> other.epoch if epoch != other.epoch

  result = compare_strings(upstream_version, other.upstream_version)

  return result if result != 0

  return compare_strings(debian_revision, other.debian_revision) if debian_revision || other.debian_revision

  0
end

#compare_strings(version1, version2) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/deb_version/compare.rb', line 42

def compare_strings(version1, version2)
  v1 = version1.chars
  v2 = version2.chars
  while !v1.empty? || !v2.empty?
    p1 = get_non_digit_prefix(v1)
    p2 = get_non_digit_prefix(v2)
    if p1 != p2
      loop do
        c1 = p1.shift
        c2 = p2.shift
        break if c1.nil? && c2.nil?

        o1 = DebVersion::ORDER_MAPPING.fetch(c1 || "")
        o2 = DebVersion::ORDER_MAPPING.fetch(c2 || "")
        if o1 < o2
          return -1
        elsif o1 > o2
          return 1
        end
      end
    end
    d1 = get_digit_prefix(v1)
    d2 = get_digit_prefix(v2)
    if d1 < d2
      return -1
    elsif d1 > d2
      return 1
    end
  end
  0
end

#get_digit_prefix(characters) ⇒ Object



30
31
32
33
34
# File 'lib/deb_version/compare.rb', line 30

def get_digit_prefix(characters)
  value = 0
  value = (value * 10) + characters.shift.to_i while characters && DebVersion::DIGITS.include?(characters[0])
  value
end

#get_non_digit_prefix(characters) ⇒ Object



36
37
38
39
40
# File 'lib/deb_version/compare.rb', line 36

def get_non_digit_prefix(characters)
  prefix = []
  prefix << characters.shift while characters.size.positive? && !DebVersion::DIGITS.include?(characters[0])
  prefix
end

#to_aObject



26
27
28
# File 'lib/deb_version/compare.rb', line 26

def to_a
  [@epoch, @upstream_version, @debian_revision]
end