class Gem::Dependency

Dependency 类保存一个 Gem 名称和一个 Gem::Requirement

常量

TYPES

有效的依赖类型。

属性

name[RW]

Dependency 名称或正则表达式。

prerelease[W]

允许您强制此依赖项为预发布版本。

公共类方法

new(name, *requirements) 点击以切换源码

使用 namerequirements 构建一个依赖项。最后一个参数可以选择是依赖类型,默认为 :runtime

# File rubygems/dependency.rb, line 36
def initialize(name, *requirements)
  case name
  when String then # ok
  when Regexp then
    msg = ["NOTE: Dependency.new w/ a regexp is deprecated.",
           "Dependency.new called from #{Gem.location_of_caller.join(":")}"]
    warn msg.join("\n") unless Gem::Deprecate.skip
  else
    raise ArgumentError,
          "dependency name must be a String, was #{name.inspect}"
  end

  type         = Symbol === requirements.last ? requirements.pop : :runtime
  requirements = requirements.first if requirements.length == 1 # unpack

  unless TYPES.include? type
    raise ArgumentError, "Valid types are #{TYPES.inspect}, " \
                         "not #{type.inspect}"
  end

  @name        = name
  @requirement = Gem::Requirement.create requirements
  @type        = type
  @prerelease  = false

  # This is for Marshal backwards compatibility. See the comments in
  # +requirement+ for the dirty details.

  @version_requirements = @requirement
end

公共实例方法

<=>(other) 点击以切换源码

依赖项按名称排序。

# File rubygems/dependency.rb, line 178
def <=>(other)
  name <=> other.name
end
===(other)
别名为: =~
=~(other) 点击以切换源码

使用此依赖项作为模式来与 other 进行比较。如果名称与另一个名称匹配,并且另一个只有满足此依赖项的相等版本要求,则此依赖项将匹配。

# File rubygems/dependency.rb, line 188
def =~(other)
  unless Gem::Dependency === other
    return unless other.respond_to?(:name) && other.respond_to?(:version)
    other = Gem::Dependency.new other.name, other.version
  end

  return false unless name === other.name

  reqs = other.requirement.requirements

  return false unless reqs.length == 1
  return false unless reqs.first.first == "="

  version = reqs.first.last

  requirement.satisfied_by? version
end
也别名为: ===
identity() 点击以切换源码
# File rubygems/dependency.rb, line 327
def identity
  if prerelease?
    if specific?
      :complete
    else
      :abs_latest
    end
  elsif latest_version?
    :latest
  else
    :released
  end
end
latest_version?() 点击以切换源码

此依赖项是否只是请求 gem 的最新版本?

# File rubygems/dependency.rb, line 94
def latest_version?
  @requirement.none?
end
match? name → true 或 false 点击以切换源码
match? name, version → true 或 false
match? spec → true 或 false

此依赖项是否匹配由 nameversion 描述的规范,或者是否匹配 spec

注意:与 matches_spec? 不同,此方法在版本为预发布版本时不返回 true,除非这是预发布依赖项。

# File rubygems/dependency.rb, line 220
def match?(obj, version=nil, allow_prerelease=false)
  if !version
    name = obj.name
    version = obj.version
  else
    name = obj
  end

  return false unless self.name === name

  version = Gem::Version.new version

  return true if requirement.none? && !version.prerelease?
  return false if version.prerelease? &&
                  !allow_prerelease &&
                  !prerelease?

  requirement.satisfied_by? version
end
matches_spec?(spec) 点击以切换源码

此依赖项是否匹配 spec

注意:这不是一个方便的方法。与 match? 不同,当 spec 是预发布版本时,即使此依赖项不是预发布依赖项,此方法也会返回 true。

# File rubygems/dependency.rb, line 247
def matches_spec?(spec)
  return false unless name === spec.name
  return true  if requirement.none?

  requirement.satisfied_by?(spec.version)
end
matching_specs(platform_only = false) 点击以切换源码
# File rubygems/dependency.rb, line 273
def matching_specs(platform_only = false)
  matches = Gem::Specification.find_all_by_name(name, requirement)

  if platform_only
    matches.reject! do |spec|
      spec.nil? || !Gem::Platform.match_spec?(spec)
    end
  end

  matches.reject(&:ignored?)
end
merge(other) 点击以切换源码

other 的要求合并到此依赖项中

# File rubygems/dependency.rb, line 257
def merge(other)
  unless name == other.name
    raise ArgumentError,
          "#{self} and #{other} have different names"
  end

  default = Gem::Requirement.default
  self_req = requirement
  other_req = other.requirement

  return self.class.new name, self_req  if other_req == default
  return self.class.new name, other_req if self_req  == default

  self.class.new name, self_req.as_list.concat(other_req.as_list)
end
prerelease?() 点击以切换源码

此依赖项是否需要预发布版本?

# File rubygems/dependency.rb, line 86
def prerelease?
  @prerelease || requirement.prerelease?
end
requirement() 点击以切换源码

此依赖项需要什么?

# File rubygems/dependency.rb, line 116
def 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
  # working and Dependency doesn't implement marshal_dump and
  # marshal_load. In a happier world, this would be an
  # attr_accessor. The horrifying instance_variable_get you see
  # below is also the legacy of some old restructurings.
  #
  # Note also that because of backwards compatibility (loading new
  # gems in an old RubyGems installation), we can't add explicit
  # marshaling to this class until we want to make a big
  # break. Maybe 2.0.
  #
  # Children, define explicit marshal and unmarshal behavior for
  # public classes. Marshal formats are part of your public API.

  # REFACTOR: See above

  if defined?(@version_requirement) && @version_requirement
    version = @version_requirement.instance_variable_get :@version
    @version_requirement = nil
    @version_requirements = Gem::Requirement.new version
  end

  @requirement = @version_requirements if defined?(@version_requirements)
end
requirements_list() 点击以切换源码
# File rubygems/dependency.rb, line 145
def requirements_list
  requirement.as_list
end
runtime?() 点击以切换源码
# File rubygems/dependency.rb, line 164
def runtime?
  @type == :runtime || !@type
end
specific?() 点击以切换源码

如果依赖项并非总是匹配最新版本,则为 True。

# File rubygems/dependency.rb, line 288
def specific?
  @requirement.specific?
end
to_spec() 点击以切换源码
# File rubygems/dependency.rb, line 312
def to_spec
  matches = to_specs.compact

  active = matches.find(&:activated?)
  return active if active

  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
to_specs() 点击以切换源码
# File rubygems/dependency.rb, line 292
def to_specs
  matches = matching_specs true

  # TODO: check Gem.activated_spec[self.name] in case matches falls outside

  if matches.empty?
    specs = Gem::Specification.stubs_for name

    if specs.empty?
      raise Gem::MissingSpecError.new name, requirement
    else
      raise Gem::MissingSpecVersionError.new name, requirement, specs
    end
  end

  # TODO: any other resolver validations should go here

  matches
end
type() 点击以切换源码

Dependency 类型。

# File rubygems/dependency.rb, line 160
def type
  @type ||= :runtime
end