Class: Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
Library/Homebrew/version.rb,
Library/Homebrew/version/null.rb

Direct Known Subclasses

HeadVersion, OS::Mac::Version, FromURL

Defined Under Namespace

Classes: AlphaToken, BetaToken, CompositeToken, FromURL, NullToken, NumericToken, PatchToken, PreToken, RCToken, StringToken, Token

Constant Summary collapse

NULL_TOKEN =
NullToken.new
SCAN_PATTERN =
Regexp.union(
  AlphaToken::PATTERN,
  BetaToken::PATTERN,
  PreToken::PATTERN,
  RCToken::PATTERN,
  PatchToken::PATTERN,
  NumericToken::PATTERN,
  StringToken::PATTERN,
)
NULL =
Class.new do
  include Comparable

  def <=>(_other)
    -1
  end

  def eql?(_other)
    # Makes sure that the same instance of Version::NULL
    # will never equal itself; normally Comparable#==
    # will return true for this regardless of the return
    # value of #<=>
    false
  end

  def detected_from_url?
    false
  end

  def head?
    false
  end

  def null?
    true
  end

  def to_f
    Float::NAN
  end

  def to_i
    0
  end

  def to_s
    ""
  end
  alias_method :to_str, :to_s

  def inspect
    "#<Version::NULL>".freeze
  end
end.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val) ⇒ Version

Returns a new instance of Version



363
364
365
366
367
368
# File 'Library/Homebrew/version.rb', line 363

def initialize(val)
  unless val.respond_to?(:to_str)
    raise TypeError, "Version value must be a string; got a #{val.class} (#{val})"
  end
  @version = val.to_str
end

Class Method Details

.create(val) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
# File 'Library/Homebrew/version.rb', line 204

def self.create(val)
  unless val.respond_to?(:to_str)
    raise TypeError, "Version value must be a string; got a #{val.class} (#{val})"
  end

  if val.to_str.start_with?("HEAD")
    HeadVersion.new(val)
  else
    Version.new(val)
  end
end

.detect(url, specs) ⇒ Object



196
197
198
199
200
201
202
# File 'Library/Homebrew/version.rb', line 196

def self.detect(url, specs)
  if specs.key?(:tag)
    FromURL.new(specs[:tag][/((?:\d+\.)*\d+)/, 1])
  else
    FromURL.parse(url)
  end
end

.parse(spec) ⇒ Object



216
217
218
219
# File 'Library/Homebrew/version.rb', line 216

def self.parse(spec)
  version = _parse(spec)
  version.nil? ? NULL : new(version)
end

Instance Method Details

#<=>(other) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'Library/Homebrew/version.rb', line 382

def <=>(other)
  # Needed to retain API compatibility with older string comparisons
  # for compiler versions, etc.
  other = Version.new(other) if other.is_a? String
  # Used by the *_build_version comparisons, which formerly returned Fixnum
  other = Version.new(other.to_s) if other.is_a? Integer
  return 1 if other.nil?

  return unless other.is_a?(Version)
  return 0 if version == other.version
  return 1 if head? && !other.head?
  return -1 if !head? && other.head?
  return 0 if head? && other.head?

  ltokens = tokens
  rtokens = other.tokens
  max = max(ltokens.length, rtokens.length)
  l = r = 0

  while l < max
    a = ltokens[l] || NULL_TOKEN
    b = rtokens[r] || NULL_TOKEN

    if a == b
      l += 1
      r += 1
      next
    elsif a.numeric? && b.numeric?
      return a <=> b
    elsif a.numeric?
      return 1 if a > NULL_TOKEN
      l += 1
    elsif b.numeric?
      return -1 if b > NULL_TOKEN
      r += 1
    else
      return a <=> b
    end
  end

  0
end

#detected_from_url?Boolean

Returns:

  • (Boolean)


370
371
372
# File 'Library/Homebrew/version.rb', line 370

def detected_from_url?
  false
end

#hashObject



426
427
428
# File 'Library/Homebrew/version.rb', line 426

def hash
  version.hash
end

#head?Boolean

Returns:

  • (Boolean)


374
375
376
# File 'Library/Homebrew/version.rb', line 374

def head?
  false
end

#null?Boolean

Returns:

  • (Boolean)


378
379
380
# File 'Library/Homebrew/version.rb', line 378

def null?
  false
end

#to_fObject



430
431
432
# File 'Library/Homebrew/version.rb', line 430

def to_f
  version.to_f
end

#to_iObject



434
435
436
# File 'Library/Homebrew/version.rb', line 434

def to_i
  version.to_i
end

#to_sObject Also known as: to_str



438
439
440
# File 'Library/Homebrew/version.rb', line 438

def to_s
  version.dup
end