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
-
DefaultPrereleaseRequirement =
The default requirement matches any version
[">=", Gem::Version.new("0.a")].freeze
-
DefaultRequirement =
The default requirement matches any non-prerelease version
[">=", Gem::Version.new(0)].freeze
-
OPS =
Internal use only
# File 'lib/rubygems/requirement.rb', line 13{ # :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 }, }.freeze
-
PATTERN =
A regular expression that matches a requirement
/\A#{PATTERN_RAW}\z/
-
PATTERN_RAW =
Internal use only
# File 'lib/rubygems/requirement.rb', line 26"\\s*(#{quoted})?\\s*(#{Gem::Version::VERSION_PATTERN})\\s*".freeze
-
SOURCE_SET_REQUIREMENT =
Internal use only
# File 'lib/rubygems/requirement.rb', line 23Struct.new(:for_lockfile).new "!"
Class Method Summary
-
.create(*inputs)
Factory method to create a
Requirement
object. - .default
- .default_prerelease
-
.new(*requirements) ⇒ Requirement
constructor
Constructs a requirement from #requirements.
-
.parse(obj)
Parse
obj
, returning an[op, version]
pair. -
.source_set
Internal use only
A source set requirement, used for Gemfiles and lockfiles.
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.
-
#requirements
readonly
Internal use only
An array of requirement pairs.
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
. - #_sorted_requirements protected
- #_tilde_requirements protected
- #==(other) Internal use only
- #as_list Internal use only
- #encode_with(coder) Internal use only
-
#for_lockfile
Internal use only
Formats this requirement for use in a
RequestSet::Lockfile
. - #hash Internal use only
- #init_with(coder) Internal use only
- #marshal_dump Internal use only
- #marshal_load(array) Internal use only
- #pretty_print(q) Internal use only
- #to_s Internal use only
- #to_yaml_properties Internal use only
- #yaml_initialize(tag, vals) Internal use only
- #initialize_copy(other) protected Internal use only
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 131
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(*inputs)
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.
# File 'lib/rubygems/requirement.rb', line 55
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
.default
[ GitHub ]# File 'lib/rubygems/requirement.rb', line 76
def self.default new ">= 0" end
.default_prerelease
[ GitHub ]# File 'lib/rubygems/requirement.rb', line 80
def self.default_prerelease new ">= 0.a" 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 103
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 elsif $1 == ">=" && $2 == "0.a" DefaultPrereleaseRequirement else [-($1 || "="), Gem::Version.new($2)] end end
.source_set
A source set requirement, used for Gemfiles and lockfiles
# File 'lib/rubygems/requirement.rb', line 87
def self.source_set # :nodoc: SOURCE_SET_REQUIREMENT end
Instance Attribute Details
#exact? ⇒ Boolean
(readonly)
true if the requirement is for only an exact version
# File 'lib/rubygems/requirement.rb', line 184
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 173
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 229
def prerelease? requirements.any? {|r| r.last.prerelease? } end
#requirements (readonly)
An array of requirement pairs. The first element of the pair is the op, and the second is the Version
.
# File 'lib/rubygems/requirement.rb', line 123
attr_reader :requirements # :nodoc:
#specific? ⇒ Boolean
(readonly)
True if the requirement will not always match the latest version.
# File 'lib/rubygems/requirement.rb', line 254
def specific? return true if @requirements.length > 1 # GIGO, > 1, > 2 is silly !%w[> >=].include? @requirements.first.first # grab the operator end
Instance Method Details
#==(other)
# File 'lib/rubygems/requirement.rb', line 264
def ==(other) # :nodoc: return unless Gem::Requirement === other # An == check is always necessary return false unless _sorted_requirements == other._sorted_requirements # An == check is sufficient unless any requirements use ~> return true unless _tilde_requirements.any? # If any requirements use ~> we use the stricter `#eql?` that also checks # that version precision is the same _tilde_requirements.eql?(other._tilde_requirements) end
#===(version)
Alias for #satisfied_by?.
# File 'lib/rubygems/requirement.rb', line 248
alias_method :===, :satisfied_by?
#=~(version)
Alias for #satisfied_by?.
# File 'lib/rubygems/requirement.rb', line 249
alias_method :=~, :satisfied_by?
#_sorted_requirements (protected)
[ GitHub ]# File 'lib/rubygems/requirement.rb', line 280
def _sorted_requirements @_sorted_requirements ||= requirements.sort_by(&:to_s) end
#_tilde_requirements (protected)
[ GitHub ]# File 'lib/rubygems/requirement.rb', line 284
def _tilde_requirements @_tilde_requirements ||= _sorted_requirements.select {|r| r.first == "~>" } end
#as_list
# File 'lib/rubygems/requirement.rb', line 189
def as_list # :nodoc: requirements.map {|op, version| "#{op} #{version}" } end
#concat(new)
Concatenates the .new requirements onto this requirement.
#encode_with(coder)
# File 'lib/rubygems/requirement.rb', line 221
def encode_with(coder) # :nodoc: coder.add "requirements", @requirements end
#for_lockfile
Formats this requirement for use in a RequestSet::Lockfile
.
# File 'lib/rubygems/requirement.rb', line 158
def for_lockfile # :nodoc: return if @requirements == [DefaultRequirement] list = requirements.sort_by do |_, version| version end.map do |op, version| "#{op} #{version}" end.uniq " (#{list.join ", "})" end
#hash
# File 'lib/rubygems/requirement.rb', line 193
def hash # :nodoc: requirements.map {|r| r.first == "~>" ? [r[0], r[1].to_s] : r }.sort.hash end
#init_with(coder)
# File 'lib/rubygems/requirement.rb', line 213
def init_with(coder) # :nodoc: yaml_initialize coder.tag, coder.map end
#initialize_copy(other) (protected)
# File 'lib/rubygems/requirement.rb', line 288
def initialize_copy(other) # :nodoc: @requirements = other.requirements.dup super end
#marshal_dump
# File 'lib/rubygems/requirement.rb', line 197
def marshal_dump # :nodoc: [@requirements] end
#marshal_load(array)
# File 'lib/rubygems/requirement.rb', line 201
def marshal_load(array) # :nodoc: @requirements = array[0] raise TypeError, "wrong @requirements" unless Array === @requirements end
#pretty_print(q)
# File 'lib/rubygems/requirement.rb', line 233
def pretty_print(q) # :nodoc: q.group 1, "Gem::Requirement.new(", ")" do q.pp as_list end end
#satisfied_by?(version) ⇒ Boolean
Also known as: #===, #=~
True if version
satisfies this Requirement
.
# File 'lib/rubygems/requirement.rb', line 242
def satisfied_by?(version) raise ArgumentError, "Need a Gem::Version: #{version.inspect}" unless Gem::Version === version requirements.all? {|op, rv| OPS[op].call version, rv } end
#to_s
# File 'lib/rubygems/requirement.rb', line 260
def to_s # :nodoc: as_list.join ", " end
#to_yaml_properties
# File 'lib/rubygems/requirement.rb', line 217
def to_yaml_properties # :nodoc: ["@requirements"] end
#yaml_initialize(tag, vals)
# File 'lib/rubygems/requirement.rb', line 207
def yaml_initialize(tag, vals) # :nodoc: vals.each do |ivar, val| instance_variable_set "@#{ivar}", val end end