Class: Gem::Requirement
Relationships & Source Files | |
Namespace Children | |
Exceptions:
| |
Inherits: | Object |
Defined in: | lib/rubygems/requirement.rb |
Overview
A Requirement is a set of one or more version restrictions. It supports a few (=, !=, >, <, >=, <=, ~>
) different restriction operators.
See Version for a description on how versions and requirements work together in RubyGems.
Constant Summary
-
DefaultRequirement =
The default requirement matches any version
[">=", Gem::Version.new(0)]
-
PATTERN =
A regular expression that matches a requirement
/\A#{PATTERN_RAW}\z/
Class Method Summary
-
.create(input)
Factory method to create a
Requirement
object. -
.default
A default “version requirement” can surely only be '>= 0'.
-
.new(*requirements) ⇒ Requirement
constructor
Constructs a requirement from #requirements.
-
.parse(obj)
Parse
obj
, returning an[op, version]
pair.
Instance Attribute Summary
-
#exact? ⇒ Boolean
readonly
true if the requirement is for only an exact version.
-
#none? ⇒ Boolean
readonly
true if this gem has no requirements.
-
#prerelease? ⇒ Boolean
readonly
A requirement is a prerelease if any of the versions inside of it are prereleases.
-
#specific? ⇒ Boolean
readonly
True if the requirement will not always match the latest version.
Instance Method Summary
-
#===(version)
Alias for #satisfied_by?.
-
#=~(version)
Alias for #satisfied_by?.
-
#concat(new)
Concatenates the .new requirements onto this requirement.
-
#satisfied_by?(version) ⇒ Boolean
(also: #===, #=~)
True if
version
satisfies thisRequirement
.
Constructor Details
.new(*requirements) ⇒ Requirement
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 'lib/rubygems/requirement.rb', line 123
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
Class Method Details
.create(input)
Factory method to create a 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.
.default
A default “version requirement” can surely only be '>= 0'.
# File 'lib/rubygems/requirement.rb', line 74
def self.default new '>= 0' end
.parse(obj)
Parse obj
, returning an [op, version]
pair. obj
can be a String or a 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 'lib/rubygems/requirement.rb', line 97
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
Instance Attribute Details
#exact? ⇒ Boolean
(readonly)
true if the requirement is for only an exact version
# File 'lib/rubygems/requirement.rb', line 176
def exact? return false unless @requirements.size == 1 @requirements[0][0] == "=" end
#none? ⇒ Boolean
(readonly)
true if this gem has no requirements.
# File 'lib/rubygems/requirement.rb', line 165
def none? if @requirements.size == 1 @requirements[0] == DefaultRequirement else false end end
#prerelease? ⇒ Boolean
(readonly)
A requirement is a prerelease if any of the versions inside of it are prereleases
# File 'lib/rubygems/requirement.rb', line 226
def prerelease? requirements.any? { |r| r.last.prerelease? } end
#specific? ⇒ Boolean
(readonly)
True if the requirement will not always match the latest version.
# File 'lib/rubygems/requirement.rb', line 252
def specific? return true if @requirements.length > 1 # GIGO, > 1, > 2 is silly not %w[> >=].include? @requirements.first.first # grab the operator end
Instance Method Details
#===(version)
Alias for #satisfied_by?.
# File 'lib/rubygems/requirement.rb', line 246
alias :=== :satisfied_by?
#=~(version)
Alias for #satisfied_by?.
# File 'lib/rubygems/requirement.rb', line 247
alias :=~ :satisfied_by?
#concat(new)
Concatenates the .new requirements onto this requirement.
#satisfied_by?(version) ⇒ Boolean
Also known as: #===, #=~
True if version
satisfies this Requirement
.
# File 'lib/rubygems/requirement.rb', line 239
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