PHP 8.2.31
Preview: requirement.rb Size: 6.20 KB
/proc/thread-self/root/opt/alt/ruby22/lib64/ruby/2.2.0/rubygems/requirement.rb

require "rubygems/version"
require "rubygems/deprecate"

# If we're being loaded after yaml was already required, then
# load our yaml + workarounds now.
Gem.load_yaml if defined? ::YAML

##
# A Requirement is a set of one or more version restrictions. It supports a
# few (<tt>=, !=, >, <, >=, <=, ~></tt>) different restriction operators.
#
# See Gem::Version for a description on how versions and requirements work
# together in RubyGems.

class Gem::Requirement
  OPS = { #:nodoc:
    "="  =>  lambda { |v, r| v == r },
    "!=" =>  lambda { |v, r| v != r },
    ">"  =>  lambda { |v, r| v >  r },
    "<"  =>  lambda { |v, r| v <  r },
    ">=" =>  lambda { |v, r| v >= r },
    "<=" =>  lambda { |v, r| v <= r },
    "~>" =>  lambda { |v, r| v >= r && v.release < r.bump }
  }

  quoted  = OPS.keys.map { |k| Regexp.quote k }.join "|"
  PATTERN_RAW = "\\s*(#{quoted})?\\s*(#{Gem::Version::VERSION_PATTERN})\\s*" # :nodoc:

  ##
  # A regular expression that matches a requirement

  PATTERN = /\A#{PATTERN_RAW}\z/

  ##
  # The default requirement matches any version

  DefaultRequirement = [">=", Gem::Version.new(0)]

  ##
  # Raised when a bad requirement is encountered

  class BadRequirementError < ArgumentError; end

  ##
  # Factory method to create a Gem::Requirement object.  Input may be
  # a Version, a String, or nil.  Intended to simplify client code.
  #
  # If the input is "weird", the default version requirement is
  # returned.

  def self.create input
    case input
    when Gem::Requirement then
      input
    when Gem::Version, Array then
      new input
    else
      if input.respond_to? :to_str then
        new [input.to_str]
      else
        default
      end
    end
  end

  ##
  # A default "version requirement" can surely _only_ be '>= 0'.

  def self.default
    new '>= 0'
  end

  ##
  # Parse +obj+, returning an <tt>[op, version]</tt> pair. +obj+ can
  # be a String or a Gem::Version.
  #
  # If +obj+ is a String, it can be either a full requirement
  # specification, like <tt>">= 1.2"</tt>, or a simple version number,
  # like <tt>"1.2"</tt>.
  #
  #     parse("> 1.0")                 # => [">", "1.0"]
  #     parse("1.0")                   # => ["=", "1.0"]
  #     parse(Gem::Version.new("1.0")) # => ["=,  "1.0"]

  def self.parse obj
    return ["=", obj] if Gem::Version === obj

    unless PATTERN =~ obj.to_s
      raise BadRequirementError, "Illformed requirement [#{obj.inspect}]"
    end

    if $1 == ">=" && $2 == "0"
      DefaultRequirement
    else
      [$1 || "=", Gem::Version.new($2)]
    end
  end

  ##
  # An array of requirement pairs. The first element of the pair is
  # the op, and the second is the Gem::Version.

  attr_reader :requirements #:nodoc:

  ##
  # Constructs a requirement from +requirements+. Requirements can be
  # Strings, Gem::Versions, or Arrays of those. +nil+ and duplicate
  # requirements are ignored. An empty set of +requirements+ is the
  # same as <tt>">= 0"</tt>.

  def initialize *requirements
    requirements = requirements.flatten
    requirements.compact!
    requirements.uniq!

    if requirements.empty?
      @requirements = [DefaultRequirement]
    else
      @requirements = requirements.map! { |r| self.class.parse r }
    end
  end

  ##
  # Concatenates the +new+ requirements onto this requirement.

  def concat new
    new = new.flatten
    new.compact!
    new.uniq!
    new = new.map { |r| self.class.parse r }

    @requirements.concat new
  end

  ##
  # Formats this requirement for use in a Gem::RequestSet::Lockfile.

  def for_lockfile # :nodoc:
    return if [DefaultRequirement] == @requirements

    list = requirements.sort_by { |_, version|
      version
    }.map { |op, version|
      "#{op} #{version}"
    }.uniq

    " (#{list.join ', '})"
  end

  ##
  # true if this gem has no requirements.

  def none?
    if @requirements.size == 1
      @requirements[0] == DefaultRequirement
    else
      false
    end
  end

  ##
  # true if the requirement is for only an exact version

  def exact?
    return false unless @requirements.size == 1
    @requirements[0][0] == "="
  end

  def as_list # :nodoc:
    requirements.map { |op, version| "#{op} #{version}" }.sort
  end

  def hash # :nodoc:
    requirements.hash
  end

  def marshal_dump # :nodoc:
    fix_syck_default_key_in_requirements

    [@requirements]
  end

  def marshal_load array # :nodoc:
    @requirements = array[0]

    fix_syck_default_key_in_requirements
  end

  def yaml_initialize(tag, vals) # :nodoc:
    vals.each do |ivar, val|
      instance_variable_set "@#{ivar}", val
    end

    Gem.load_yaml
    fix_syck_default_key_in_requirements
  end

  def init_with coder # :nodoc:
    yaml_initialize coder.tag, coder.map
  end

  def to_yaml_properties # :nodoc:
    ["@requirements"]
  end

  def encode_with coder # :nodoc:
    coder.add 'requirements', @requirements
  end

  ##
  # A requirement is a prerelease if any of the versions inside of it
  # are prereleases

  def prerelease?
    requirements.any? { |r| r.last.prerelease? }
  end

  def pretty_print q # :nodoc:
    q.group 1, 'Gem::Requirement.new(', ')' do
      q.pp as_list
    end
  end

  ##
  # True if +version+ satisfies this Requirement.

  def satisfied_by? version
    raise ArgumentError, "Need a Gem::Version: #{version.inspect}" unless
      Gem::Version === version
    # #28965: syck has a bug with unquoted '=' YAML.loading as YAML::DefaultKey
    requirements.all? { |op, rv| (OPS[op] || OPS["="]).call version, rv }
  end

  alias :=== :satisfied_by?
  alias :=~ :satisfied_by?

  ##
  # True if the requirement will not always match the latest version.

  def specific?
    return true if @requirements.length > 1 # GIGO, > 1, > 2 is silly

    not %w[> >=].include? @requirements.first.first # grab the operator
  end

  def to_s # :nodoc:
    as_list.join ", "
  end

  def == other # :nodoc:
    Gem::Requirement === other and to_s == other.to_s
  end

  private

  def fix_syck_default_key_in_requirements # :nodoc:
    Gem.load_yaml

    # Fixup the Syck DefaultKey bug
    @requirements.each do |r|
      if r[0].kind_of? Gem::SyckDefaultKey
        r[0] = "="
      end
    end
  end
end

class Gem::Version
  # This is needed for compatibility with older yaml
  # gemspecs.

  Requirement = Gem::Requirement # :nodoc:
end

Directory Contents

Dirs: 11 × Files: 59

Name Size Perms Modified Actions
commands DIR
- drwxr-xr-x 2024-03-03 22:43:19
Edit Download
core_ext DIR
- drwxr-xr-x 2024-03-03 22:43:19
Edit Download
ext DIR
- drwxr-xr-x 2024-03-03 22:43:19
Edit Download
package DIR
- drwxr-xr-x 2024-03-03 22:43:19
Edit Download
request DIR
- drwxr-xr-x 2024-03-03 22:43:19
Edit Download
- drwxr-xr-x 2024-03-03 22:43:19
Edit Download
resolver DIR
- drwxr-xr-x 2024-03-03 22:43:19
Edit Download
security DIR
- drwxr-xr-x 2024-03-03 22:43:19
Edit Download
source DIR
- drwxr-xr-x 2024-03-03 22:43:19
Edit Download
ssl_certs DIR
- drwxr-xr-x 2024-03-03 22:43:19
Edit Download
util DIR
- drwxr-xr-x 2024-03-03 22:43:19
Edit Download
2.99 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
6.66 KB lrw-r--r-- 2014-12-07 00:53:01
Edit Download
13.55 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
4.63 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
1.63 KB lrw-r--r-- 2014-12-25 03:37:54
Edit Download
12.57 KB lrw-r--r-- 2017-12-14 13:50:12
Edit Download
4.01 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
8.48 KB lrw-r--r-- 2014-11-17 03:55:02
Edit Download
13.81 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
5.44 KB lrw-r--r-- 2014-12-07 00:53:01
Edit Download
1.70 KB lrw-r--r-- 2014-01-07 01:19:28
Edit Download
3.04 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
3.12 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
6.20 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
431 B lrw-r--r-- 2013-10-16 06:00:39
Edit Download
3.94 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
1.96 KB lrw-r--r-- 2013-09-14 08:59:02
Edit Download
13.03 KB lrw-r--r-- 2014-11-17 03:55:02
Edit Download
22.48 KB lrw-r--r-- 2017-09-10 01:10:24
Edit Download
4.23 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
307 B lrw-r--r-- 2013-07-09 23:21:36
Edit Download
281 B lrw-r--r-- 2012-11-29 06:52:18
Edit Download
5.85 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
3.37 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
1.35 KB lrw-r--r-- 2012-11-29 06:52:18
Edit Download
2.37 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
14.87 KB lrw-r--r-- 2018-02-16 16:27:56
Edit Download
3.77 KB lrw-r--r-- 2013-08-26 20:24:51
Edit Download
1.82 KB lrw-r--r-- 2013-09-14 08:59:02
Edit Download
6.13 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
269 B lrw-r--r-- 2013-10-20 01:33:19
Edit Download
763 B lrw-r--r-- 2013-07-09 22:34:58
Edit Download
7.71 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
11.09 KB lrw-r--r-- 2017-09-10 01:10:24
Edit Download
6.73 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
9.40 KB lrw-r--r-- 2014-12-07 00:53:01
Edit Download
6.20 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
13.35 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
1.11 KB lrw-r--r-- 2017-12-14 13:50:12
Edit Download
20.96 KB lrw-r--r-- 2014-01-20 04:57:31
Edit Download
22.93 KB lrw-r--r-- 2018-02-16 16:27:56
Edit Download
5.21 KB lrw-r--r-- 2014-12-07 00:53:01
Edit Download
2.47 KB lrw-r--r-- 2013-11-19 00:34:13
Edit Download
111 B lrw-r--r-- 2013-09-14 08:59:02
Edit Download
93 B lrw-r--r-- 2013-09-14 08:59:02
Edit Download
72.88 KB lrw-r--r-- 2018-02-16 16:27:56
Edit Download
6.31 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
3.56 KB lrw-r--r-- 2014-12-07 00:53:01
Edit Download
2.09 KB lrw-r--r-- 2013-12-19 22:09:19
Edit Download
37.49 KB lrw-r--r-- 2016-04-22 09:15:03
Edit Download
8.44 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
1.90 KB lrw-r--r-- 2017-09-10 01:10:24
Edit Download
8.72 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
762 B lrw-r--r-- 2013-10-20 01:33:19
Edit Download
14.64 KB lrw-r--r-- 2014-11-17 03:55:02
Edit Download
2.59 KB lrw-r--r-- 2014-08-03 13:33:06
Edit Download
4.18 KB lrw-r--r-- 2013-11-30 23:27:52
Edit Download
10.92 KB lrw-r--r-- 2014-09-14 03:30:02
Edit Download
1.83 KB lrw-r--r-- 2013-09-14 08:59:02
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).