Class: Cpe23::Version

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Version

Returns a new instance of Version.



8
9
10
11
12
13
14
15
16
# File 'lib/cpe23/version_wildcard.rb', line 8

def initialize(str)
  @parts = str.split('.')
  wildcard_index = @parts.index '*'
  if wildcard_index.nil?
    @parts << '*'
  elsif wildcard_index < @parts.size - 1
    raise 'Wildcard must be at the end of a version'
  end
end

Instance Attribute Details

#partsObject (readonly)

Returns the value of attribute parts.



6
7
8
# File 'lib/cpe23/version_wildcard.rb', line 6

def parts
  @parts
end

Instance Method Details

#<=>(other) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cpe23/version_wildcard.rb', line 18

def <=>(other)
  unless other.is_a? Version
    begin
      other = Version.new(other)
    rescue StandardError
      return nil
    end
  end
  @parts.zip(other.parts).each do |a, b|
    break if a == '*' || b == '*'

    # Compare parts numerically if they are numeric
    if int?(a) && int?(b)
      a = a.to_i
      b = b.to_i
    end
    return -1 if a.to_i < b.to_i
    return 1 if a.to_i > b.to_i
  end
  0
end