summaryrefslogtreecommitdiff
path: root/lib/rubygems/dependency.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rubygems/dependency.rb')
-rw-r--r--lib/rubygems/dependency.rb123
1 files changed, 69 insertions, 54 deletions
diff --git a/lib/rubygems/dependency.rb b/lib/rubygems/dependency.rb
index bbdab7ccfa..1e91f493a6 100644
--- a/lib/rubygems/dependency.rb
+++ b/lib/rubygems/dependency.rb
@@ -1,11 +1,9 @@
# frozen_string_literal: true
+
##
# The Dependency class holds a Gem name and a Gem::Requirement.
-require "rubygems/requirement"
-
class Gem::Dependency
-
##
# Valid dependency types.
#--
@@ -18,7 +16,7 @@ class Gem::Dependency
TYPES = [
:development,
:runtime,
- ]
+ ].freeze
##
# Dependency name or regular expression.
@@ -35,7 +33,7 @@ class Gem::Dependency
# argument can optionally be the dependency type, which defaults to
# <tt>:runtime</tt>.
- def initialize name, *requirements
+ def initialize(name, *requirements)
case name
when String then # ok
when Regexp then
@@ -48,10 +46,10 @@ class Gem::Dependency
end
type = Symbol === requirements.last ? requirements.pop : :runtime
- requirements = requirements.first if 1 == requirements.length # unpack
+ requirements = requirements.first if requirements.length == 1 # unpack
unless TYPES.include? type
- raise ArgumentError, "Valid types are #{TYPES.inspect}, " +
+ raise ArgumentError, "Valid types are #{TYPES.inspect}, " \
"not #{type.inspect}"
end
@@ -75,12 +73,10 @@ class Gem::Dependency
end
def inspect # :nodoc:
- if prerelease? then
- "<%s type=%p name=%p requirements=%p prerelease=ok>" %
- [self.class, self.type, self.name, requirement.to_s]
+ if prerelease?
+ format("<%s type=%p name=%p requirements=%p prerelease=ok>", self.class, type, name, requirement.to_s)
else
- "<%s type=%p name=%p requirements=%p>" %
- [self.class, self.type, self.name, requirement.to_s]
+ format("<%s type=%p name=%p requirements=%p>", self.class, type, name, requirement.to_s)
end
end
@@ -99,15 +95,15 @@ class Gem::Dependency
@requirement.none?
end
- def pretty_print q # :nodoc:
- q.group 1, 'Gem::Dependency.new(', ')' do
+ def pretty_print(q) # :nodoc:
+ q.group 1, "Gem::Dependency.new(", ")" do
q.pp name
- q.text ','
+ q.text ","
q.breakable
q.pp requirement
- q.text ','
+ q.text ","
q.breakable
q.pp type
@@ -118,7 +114,7 @@ class Gem::Dependency
# What does this dependency require?
def requirement
- return @requirement if defined?(@requirement) and @requirement
+ return @requirement if defined?(@requirement) && @requirement
# @version_requirements and @version_requirement are legacy ivar
# names, and supported here because older gems need to keep
@@ -139,7 +135,7 @@ class Gem::Dependency
if defined?(@version_requirement) && @version_requirement
version = @version_requirement.instance_variable_get :@version
- @version_requirement = nil
+ @version_requirement = nil
@version_requirements = Gem::Requirement.new version
end
@@ -151,7 +147,7 @@ class Gem::Dependency
end
def to_s # :nodoc:
- if type != :runtime then
+ if type != :runtime
"#{name} (#{requirement}, #{type})"
else
"#{name} (#{requirement})"
@@ -169,18 +165,18 @@ class Gem::Dependency
@type == :runtime || !@type
end
- def == other # :nodoc:
+ def ==(other) # :nodoc:
Gem::Dependency === other &&
- self.name == other.name &&
- self.type == other.type &&
- self.requirement == other.requirement
+ name == other.name &&
+ type == other.type &&
+ requirement == other.requirement
end
##
# Dependencies are ordered by name.
- def <=> other
- self.name <=> other.name
+ def <=>(other)
+ name <=> other.name
end
##
@@ -189,7 +185,7 @@ class Gem::Dependency
# other has only an equal version requirement that satisfies this
# dependency.
- def =~ other
+ def =~(other)
unless Gem::Dependency === other
return unless other.respond_to?(:name) && other.respond_to?(:version)
other = Gem::Dependency.new other.name, other.version
@@ -200,14 +196,14 @@ class Gem::Dependency
reqs = other.requirement.requirements
return false unless reqs.length == 1
- return false unless reqs.first.first == '='
+ return false unless reqs.first.first == "="
version = reqs.first.last
requirement.satisfied_by? version
end
- alias === =~
+ alias_method :===, :=~
##
# :call-seq:
@@ -221,7 +217,7 @@ class Gem::Dependency
# NOTE: Unlike #matches_spec? this method does not return true when the
# version is a prerelease version unless this is a prerelease dependency.
- def match? obj, version=nil, allow_prerelease=false
+ def match?(obj, version = nil, allow_prerelease = false)
if !version
name = obj.name
version = obj.version
@@ -233,10 +229,10 @@ class Gem::Dependency
version = Gem::Version.new version
- return true if requirement.none? and not version.prerelease?
- return false if version.prerelease? and
- not allow_prerelease and
- not prerelease?
+ return true if requirement.none? && !version.prerelease?
+ return false if version.prerelease? &&
+ !allow_prerelease &&
+ !prerelease?
requirement.satisfied_by? version
end
@@ -248,7 +244,7 @@ class Gem::Dependency
# returns true when +spec+ is a prerelease version even if this dependency
# is not a prerelease dependency.
- def matches_spec? spec
+ def matches_spec?(spec)
return false unless name === spec.name
return true if requirement.none?
@@ -258,14 +254,14 @@ class Gem::Dependency
##
# Merges the requirements of +other+ into this dependency
- def merge other
- unless name == other.name then
+ def merge(other)
+ unless name == other.name
raise ArgumentError,
"#{self} and #{other} have different names"
end
default = Gem::Requirement.default
- self_req = self.requirement
+ self_req = requirement
other_req = other.requirement
return self.class.new name, self_req if other_req == default
@@ -274,19 +270,16 @@ class Gem::Dependency
self.class.new name, self_req.as_list.concat(other_req.as_list)
end
- def matching_specs platform_only = false
- env_req = Gem.env_requirement(name)
- matches = Gem::Specification.stubs_for(name).find_all { |spec|
- requirement.satisfied_by?(spec.version) && env_req.satisfied_by?(spec.version)
- }.map(&:to_spec)
+ def matching_specs(platform_only = false)
+ matches = Gem::Specification.find_all_by_name(name, requirement)
if platform_only
- matches.reject! { |spec|
- spec.nil? || !Gem::Platform.match(spec.platform)
- }
+ matches.reject! do |spec|
+ spec.nil? || !Gem::Platform.match_spec?(spec)
+ end
end
- matches
+ matches.reject(&:ignored?)
end
##
@@ -301,7 +294,7 @@ class Gem::Dependency
# TODO: check Gem.activated_spec[self.name] in case matches falls outside
- if matches.empty? then
+ if matches.empty?
specs = Gem::Specification.stubs_for name
if specs.empty?
@@ -317,17 +310,39 @@ class Gem::Dependency
end
def to_spec
- matches = self.to_specs.compact
+ matches = to_specs.compact
- active = matches.find { |spec| spec.activated? }
+ active = matches.find(&:activated?)
return active if active
- return matches.first if prerelease?
-
- # Move prereleases to the end of the list for >= 0 requirements
- pre, matches = matches.partition { |spec| spec.version.prerelease? }
- matches += pre if requirement == Gem::Requirement.default
+ unless prerelease?
+ # Consider prereleases only as a fallback
+ pre, matches = matches.partition {|spec| spec.version.prerelease? }
+ matches = pre if matches.empty?
+ end
matches.first
end
+
+ def identity
+ if prerelease?
+ if specific?
+ :complete
+ else
+ :abs_latest
+ end
+ elsif latest_version?
+ :latest
+ else
+ :released
+ end
+ end
+
+ def encode_with(coder) # :nodoc:
+ coder.add "name", @name
+ coder.add "requirement", @requirement
+ coder.add "type", @type
+ coder.add "prerelease", @prerelease
+ coder.add "version_requirements", @version_requirements
+ end
end