class Gem::Requirement

A Requirement is a set of one or more version restrictions. It supports a few (=, !=, >, <, >=, <=, ~>) different restriction operators.

See Gem::Version for a description on how versions and requirements work together in RubyGems.

Constants

DefaultRequirement

The default requirement matches any version

PATTERN

A regular expression that matches a requirement

Public Class Methods

create(input) click to toggle source

Factory method to create a Gem::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 51
def self.create input
  case input
  when Gem::Requirement then
    input
  when Gem::Version, Array then
    new input
  else
    if input.respond_to? :to_str then
      new [input.to_str]
    else
      default
    end
  end
end
default() click to toggle source

A default “version requirement” can surely only be '>= 0'.

# File lib/rubygems/requirement.rb, line 69
def self.default
  new '>= 0'
end
new(*requirements) click to toggle source

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 111
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) click to toggle source

Parse obj, returning an [op, version] pair. obj can be a String or a Gem::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")                 # => [">", "1.0"]
parse("1.0")                   # => ["=", "1.0"]
parse(Gem::Version.new("1.0")) # => ["=,  "1.0"]
# File lib/rubygems/requirement.rb, line 85
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

Public Instance Methods

===(version)
Alias for: satisfied_by?
=~(version)
Alias for: satisfied_by?
concat(new) click to toggle source

Concatenates the new requirements onto this requirement.

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

  @requirements.concat new
end
exact?() click to toggle source

true if the requirement is for only an exact version

# File lib/rubygems/requirement.rb, line 164
def exact?
  return false unless @requirements.size == 1
  @requirements[0][0] == "="
end
none?() click to toggle source

true if this gem has no requirements.

# File lib/rubygems/requirement.rb, line 153
def none?
  if @requirements.size == 1
    @requirements[0] == DefaultRequirement
  else
    false
  end
end
prerelease?() click to toggle source

A requirement is a prerelease if any of the versions inside of it are prereleases

# File lib/rubygems/requirement.rb, line 214
def prerelease?
  requirements.any? { |r| r.last.prerelease? }
end
satisfied_by?(version) click to toggle source

True if version satisfies this Requirement.

# File lib/rubygems/requirement.rb, line 227
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
Also aliased as: ===, =~
specific?() click to toggle source

True if the requirement will not always match the latest version.

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

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