class Gem::Requirement

Requirement 是一组或多组版本限制。它支持几种不同的限制运算符 (=, !=, >, <, >=, <=, ~>)。

请参阅 Gem::Version 以了解版本和要求如何在 RubyGems 中协同工作。

常量

DefaultPrereleaseRequirement

默认要求匹配任何版本

DefaultRequirement

默认要求匹配任何非预发布版本

PATTERN

匹配要求的正则表达式

公共类方法

create(*inputs) 点击以切换源代码

创建 Gem::Requirement 对象的工厂方法。输入可以是 Version、String 或 nil。旨在简化客户端代码。

如果输入是“奇怪的”,则返回默认的版本要求。

# File 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() 点击以切换源代码
# File rubygems/requirement.rb, line 76
def self.default
  new ">= 0"
end
default_prerelease() 点击以切换源代码
# File rubygems/requirement.rb, line 80
def self.default_prerelease
  new ">= 0.a"
end
new(*requirements) 点击以切换源代码

requirements 构建要求。要求可以是字符串、Gem::Versions 或它们的数组。nil 和重复的要求将被忽略。一组空的 requirements 等同于 ">= 0"

# File 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
parse(obj) 点击以切换源代码

解析 obj,返回一个 [op, version] 对。obj 可以是 String 或 Gem::Version

如果 obj 是 String,它可以是完整的需求规范,例如 ">= 1.2",也可以是简单的版本号,例如 "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 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

公共实例方法

===(version)
别名: satisfied_by?
=~(version)
别名: satisfied_by?
concat(new) 点击以切换源代码

new 的要求连接到此要求。

# File rubygems/requirement.rb, line 146
def concat(new)
  new = new.flatten
  new.compact!
  new.uniq!
  new = new.map {|r| self.class.parse r }

  @requirements.concat new
end
exact?() 点击以切换源代码

如果要求只针对确切的版本,则为 true

# File rubygems/requirement.rb, line 184
def exact?
  return false unless @requirements.size == 1
  @requirements[0][0] == "="
end
none?() 点击以切换源代码

如果此 gem 没有要求,则为 true。

# File rubygems/requirement.rb, line 173
def none?
  if @requirements.size == 1
    @requirements[0] == DefaultRequirement
  else
    false
  end
end
prerelease?() 点击以切换源代码

如果其中任何版本是预发布版本,则要求是预发布版本

# File rubygems/requirement.rb, line 226
def prerelease?
  requirements.any? {|r| r.last.prerelease? }
end
satisfied_by?(version) 点击以切换源代码

如果 version 满足此 Requirement,则为 true。

# File rubygems/requirement.rb, line 239
def satisfied_by?(version)
  raise ArgumentError, "Need a Gem::Version: #{version.inspect}" unless
    Gem::Version === version
  requirements.all? {|op, rv| OPS.fetch(op).call version, rv }
end
别名:===, =~
specific?() 点击以切换源代码

如果要求不会总是匹配最新版本,则为 True。

# File rubygems/requirement.rb, line 251
def specific?
  return true if @requirements.length > 1 # GIGO, > 1, > 2 is silly

  !%w[> >=].include? @requirements.first.first # grab the operator
end

受保护的实例方法

_sorted_requirements() 点击以切换源代码
# File rubygems/requirement.rb, line 277
def _sorted_requirements
  @_sorted_requirements ||= requirements.sort_by(&:to_s)
end
_tilde_requirements() 点击以切换源代码
# File rubygems/requirement.rb, line 281
def _tilde_requirements
  @_tilde_requirements ||= _sorted_requirements.select {|r| r.first == "~>" }
end