A Requirement
is a set of one or more version restrictions. It supports a few (=, !=, >, <, >=, <=, ~>
) different restriction operators.
See Gem::Version
for a description on how versions and requirements work together in RubyGems.
A regular expression that matches a requirement
The default requirement matches any version
# File tmp/rubies/ruby-2.6.10/lib/rubygems/requirement.rb, line 54
def self.create(*inputs)
return new inputs if inputs.length > 1
input = inputs.shift
case input
when Gem::Requirement then
input
when Gem::Version, Array then
new input
when '!' then
source_set
else
if input.respond_to? :to_str
new [input.to_str]
else
default
end
end
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.
# File tmp/rubies/ruby-2.6.10/lib/rubygems/requirement.rb, line 78
def self.default
new '>= 0'
end
A default “version requirement” can surely only be ‘>= 0’.
# File tmp/rubies/ruby-2.6.10/lib/rubygems/requirement.rb, line 127
def initialize(*requirements)
requirements = requirements.flatten
requirements.compact!
requirements.uniq!
if requirements.empty?
@requirements = [DefaultRequirement]
else
@requirements = requirements.map! { |r| self.class.parse r }
sort_requirements!
end
end
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 ">= 0"
.
# File tmp/rubies/ruby-2.6.10/lib/rubygems/requirement.rb, line 101
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
Parse obj
, returning an [op, version]
pair. obj
can be a String
or a Gem::Version
.
If obj
is a String
, it can be either a full requirement specification, like ">= 1.2"
, or a simple version number, like "1.2"
.
parse("> 1.0") # => [">", Gem::Version.new("1.0")] parse("1.0") # => ["=", Gem::Version.new("1.0")] parse(Gem::Version.new("1.0")) # => ["=, Gem::Version.new("1.0")]
# File tmp/rubies/ruby-2.6.10/lib/rubygems/requirement.rb, line 284
def _tilde_requirements
requirements.select { |r| r.first == "~>" }
end
# File tmp/rubies/ruby-2.6.10/lib/rubygems/requirement.rb, line 143
def concat(new)
new = new.flatten
new.compact!
new.uniq!
new = new.map { |r| self.class.parse r }
@requirements.concat new
sort_requirements!
end
Concatenates the new
requirements onto this requirement.
# File tmp/rubies/ruby-2.6.10/lib/bundler/rubygems_ext.rb, line 183
def exact?
return false unless @requirements.size == 1
@requirements[0][0] == "="
end
Backport of performance enhancement added to RubyGems 2.2
# File tmp/rubies/ruby-2.6.10/lib/bundler/rubygems_ext.rb, line 174
def none?
# note that it might be tempting to replace with with RubyGems 2.0's
# improved implementation. Don't. It requires `DefaultRequirement` to be
# defined, and more importantantly, these overrides are not used when the
# running RubyGems defines these methods
to_s == ">= 0"
end
Backport of performance enhancement added to RubyGems 1.4
# File tmp/rubies/ruby-2.6.10/lib/rubygems/requirement.rb, line 232
def prerelease?
requirements.any? { |r| r.last.prerelease? }
end
A requirement is a prerelease if any of the versions inside of it are prereleases
# File tmp/rubies/ruby-2.6.10/lib/rubygems/requirement.rb, line 245
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
True if version
satisfies this Requirement
.
# File tmp/rubies/ruby-2.6.10/lib/rubygems/requirement.rb, line 258
def specific?
return true if @requirements.length > 1 # GIGO, > 1, > 2 is silly
not %w[> >=].include? @requirements.first.first # grab the operator
end
True if the requirement will not always match the latest version.