Class: HtmlConditionalComment::VersionVector

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/html-conditional-comment/version_vector.rb

Constant Summary collapse

DOT =
/\./
DIGIT =
/\d/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ VersionVector

Returns a new instance of VersionVector.



10
11
12
# File 'lib/html-conditional-comment/version_vector.rb', line 10

def initialize(string)
  @string = string.to_s() unless string.nil?()
end

Instance Attribute Details

#stringObject

Returns the value of attribute string.



8
9
10
# File 'lib/html-conditional-comment/version_vector.rb', line 8

def string
  @string
end

Instance Method Details

#<=>(other) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/html-conditional-comment/version_vector.rb', line 14

def <=>(other)
  #Force comparison class
  other = VersionVector.new(other) unless other.is_a?(VersionVector)

  return 0 if @string.nil?() || other.string.nil?()
  return 0 if @string == other.string

  #Normalize version array sizes
  left, right = self.to_a(), other.to_a()
  size = [left.size(), right.size()].min()
  left.slice!(size..-1)
  right.slice!(size..-1)

  #Compare based on number
  left.join.to_f() <=> right.join.to_f()
end

#to_aObject

Split string into array of version numbers, major can be multiple digits, minor can only be a single digit



35
36
37
38
39
40
# File 'lib/html-conditional-comment/version_vector.rb', line 35

def to_a()
  major, minors = @string.split(DOT)
  versions = (minors || '').scan(DIGIT)
  versions.unshift(major, '.')
  versions
end