class Puppet::Util::Package::Version::Pip

Constants

VERSION_PATTERN

Attributes

key[R]

Public Class Methods

compare(version_a, version_b) click to toggle source
   # File lib/puppet/util/package/version/pip.rb
46 def self.compare(version_a, version_b)
47   version_a = parse(version_a) unless version_a.is_a?(self)
48   version_b = parse(version_b) unless version_b.is_a?(self)
49 
50   version_a <=> version_b
51 end
new(matched) click to toggle source
   # File lib/puppet/util/package/version/pip.rb
81 def initialize(matched)
82   @epoch_data   = matched[:epoch].to_i
83   @release_data = matched[:release].split('.').map(&:to_i)                                       if matched[:release]
84   @pre_data     = parse_letter_version(matched[:pre_l], matched[:pre_n])                         if matched[:pre_l]  || matched[:pre_n]
85   @post_data    = parse_letter_version(matched[:post_l], matched[:post_n1] || matched[:post_n2]) if matched[:post_l] || matched[:post_n1] || matched[:post_n2]
86   @dev_data     = parse_letter_version(matched[:dev_l], matched[:dev_n])                         if matched[:dev_l]  || matched[:dev_n]
87   @local_data   = parse_local_version(matched[:local])                                           if matched[:local]
88 
89   @key = compose_key(@epoch_data, @release_data, @pre_data, @post_data, @dev_data, @local_data)
90 end
parse(version) click to toggle source
   # File lib/puppet/util/package/version/pip.rb
37 def self.parse(version)
38   raise ValidationFailure, version.to_s unless version.is_a? String
39 
40   matched = version.match(Regexp.new(("^\\s*") + VERSION_PATTERN + ("\\s*$"), Regexp::EXTENDED | Regexp::MULTILINE | Regexp::IGNORECASE))
41   raise ValidationFailure, version unless matched
42 
43   new(matched)
44 end

Public Instance Methods

<=>(other) click to toggle source
   # File lib/puppet/util/package/version/pip.rb
72 def <=>(other)
73   raise ValidationFailure, other.to_s unless other.is_a?(self.class)
74   compare(key, other.key)
75 end
==(other)
Alias for: eql?
eql?(other) click to toggle source
   # File lib/puppet/util/package/version/pip.rb
67 def eql?(other)
68   other.is_a?(self.class) && key.eql?(other.key)
69 end
Also aliased as: ==
inspect()
Alias for: to_s
to_s() click to toggle source
   # File lib/puppet/util/package/version/pip.rb
53 def to_s
54   parts = []
55 
56   parts.push("#{@epoch_data}!")           if @epoch_data && @epoch_data != 0
57   parts.push(@release_data.join("."))     if @release_data
58   parts.push(@pre_data.join)              if @pre_data
59   parts.push(".post#{@post_data[1]}")     if @post_data
60   parts.push(".dev#{@dev_data[1]}")       if @dev_data
61   parts.push("+#{@local_data.join(".")}") if @local_data
62 
63   parts.join
64 end
Also aliased as: inspect

Private Instance Methods

compare(this, other) click to toggle source
    # File lib/puppet/util/package/version/pip.rb
144 def compare(this, other)
145   if (this.is_a? Array) && (other.is_a? Array)
146     this  << -Float::INFINITY if this.length < other.length
147     other << -Float::INFINITY if this.length > other.length
148 
149     this.each_with_index do |element, index|
150       return compare(element, other.at(index)) if element != other.at(index)
151     end
152   elsif (this.is_a? Array) && !(other.is_a? Array)
153     raise Puppet::Error, 'Cannot compare #{this} (Array) with #{other} (#{other.class}). Only ±Float::INFINITY accepted.' unless other.abs == Float::INFINITY
154     return other == -Float::INFINITY ? 1 : -1
155   elsif !(this.is_a? Array) && (other.is_a? Array)
156     raise Puppet::Error, 'Cannot compare #{this} (#{this.class}) with #{other} (Array). Only ±Float::INFINITY accepted.' unless this.abs == Float::INFINITY
157     return this == -Float::INFINITY ? -1 : 1
158   end
159   this <=> other
160 end
compose_key(epoch, release, pre, post, dev, local) click to toggle source
    # File lib/puppet/util/package/version/pip.rb
117 def compose_key(epoch, release, pre, post, dev, local)
118   release_key = release.reverse
119   release_key.each_with_index do |element, index|
120     break unless element == 0
121     release_key.delete_at(index) unless release_key.at(index + 1) != 0
122   end
123   release_key.reverse!
124 
125   if !pre && !post && dev
126     pre_key = -Float::INFINITY
127   else
128     pre_key = pre || Float::INFINITY
129   end
130 
131   post_key = post || -Float::INFINITY
132 
133   dev_key = dev || Float::INFINITY
134 
135   if !local
136     local_key = [[-Float::INFINITY, ""]]
137   else
138     local_key = local.map{|i| (i.is_a? Integer) ? [i, ""] : [-Float::INFINITY, i]}
139   end
140 
141   [epoch, release_key, pre_key, post_key, dev_key, local_key]
142 end
parse_letter_version(letter, number) click to toggle source
    # File lib/puppet/util/package/version/pip.rb
 92 def parse_letter_version(letter, number)
 93   if letter
 94     number = 0 if !number
 95     letter.downcase!
 96 
 97     if letter == "alpha"
 98       letter = "a"
 99     elsif letter == "beta"
100       letter = "b"
101     elsif ["c", "pre", "preview"].include?(letter)
102       letter = "rc"
103     elsif ["rev", "r"].include?(letter)
104       letter = "post"
105     end
106 
107     return [letter, number.to_i]
108   end
109 
110   ["post", number.to_i] if !letter && number
111 end
parse_local_version(local_version) click to toggle source
    # File lib/puppet/util/package/version/pip.rb
113 def parse_local_version(local_version)
114   local_version.split(/[\\._-]/).map{|part| part =~ /[0-9]+/ && part !~ /[a-zA-Z]+/ ? part.to_i : part.downcase} if local_version
115 end