Class: Version Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A formula's version.

Direct Known Subclasses

HeadVersion, OS::Mac::Version

Defined Under Namespace

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

Constant Summary collapse

NULL_TOKEN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Represents the absence of a token.

NullToken.new.freeze
NULL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Represents the absence of a version.

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

  # For OS::Mac::Version compatibility
  def requires_nehalem_cpu?
    false
  end
  alias_method :requires_sse4?, :requires_nehalem_cpu?
  alias_method :requires_sse41?, :requires_nehalem_cpu?
  alias_method :requires_sse42?, :requires_nehalem_cpu?
  alias_method :requires_popcnt?, :requires_nehalem_cpu?

  def major
    NULL_TOKEN
  end

  def minor
    NULL_TOKEN
  end

  def patch
    NULL_TOKEN
  end

  def major_minor
    self
  end

  def major_minor_patch
    self
  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>"
  end
end.new.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val, detected_from_url: false) ⇒ Version

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Version.

Raises:

  • (TypeError)

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

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

  @version = val.to_str
  @detected_from_url = detected_from_url
end

Class Method Details

.create(val) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (TypeError)

276
277
278
279
280
281
282
283
284
# File 'Library/Homebrew/version.rb', line 276

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

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

.detect(url, **specs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


272
273
274
# File 'Library/Homebrew/version.rb', line 272

def self.detect(url, **specs)
  parse(specs.fetch(:tag, url), detected_from_url: true)
end

.formula_optionally_versioned_regex(name, full: true) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


11
12
13
# File 'Library/Homebrew/version.rb', line 11

def self.formula_optionally_versioned_regex(name, full: true)
  /#{"^" if full}#{Regexp.escape(name)}(@\d[\d.]*)?#{"$" if full}/
end

.parse(spec, detected_from_url: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


286
287
288
289
# File 'Library/Homebrew/version.rb', line 286

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

Instance Method Details

#<=>(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'Library/Homebrew/version.rb', line 452

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 1 if other.respond_to?(:null?) && other.null?

  other = Version.new(other.to_s) if other.is_a? Token
  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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

440
441
442
# File 'Library/Homebrew/version.rb', line 440

def detected_from_url?
  @detected_from_url
end

#empty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

520
521
522
# File 'Library/Homebrew/version.rb', line 520

def empty?
  version.empty?
end

#hashObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


524
525
526
# File 'Library/Homebrew/version.rb', line 524

def hash
  version.hash
end

#head?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

444
445
446
# File 'Library/Homebrew/version.rb', line 444

def head?
  false
end

#majorObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


500
501
502
# File 'Library/Homebrew/version.rb', line 500

def major
  tokens.first
end

#major_minorObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


512
513
514
# File 'Library/Homebrew/version.rb', line 512

def major_minor
  Version.new([major, minor].compact.join("."))
end

#major_minor_patchObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


516
517
518
# File 'Library/Homebrew/version.rb', line 516

def major_minor_patch
  Version.new([major, minor, patch].compact.join("."))
end

#minorObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


504
505
506
# File 'Library/Homebrew/version.rb', line 504

def minor
  tokens.second
end

#null?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

448
449
450
# File 'Library/Homebrew/version.rb', line 448

def null?
  false
end

#patchObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


508
509
510
# File 'Library/Homebrew/version.rb', line 508

def patch
  tokens.third
end

#to_fObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


528
529
530
# File 'Library/Homebrew/version.rb', line 528

def to_f
  version.to_f
end

#to_iObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


532
533
534
# File 'Library/Homebrew/version.rb', line 532

def to_i
  version.to_i
end

#to_sObject Also known as: to_str

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


536
537
538
# File 'Library/Homebrew/version.rb', line 536

def to_s
  version.dup
end