Class: Gem::Requirement

Inherits:
Object
  • Object
show all
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.

Defined Under Namespace

Classes: BadRequirementError

Constant Summary collapse

OPS =

:nodoc:

{ #: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 }
}
PATTERN_RAW =

:nodoc:

"\\s*(#{quoted})?\\s*(#{Gem::Version::VERSION_PATTERN})\\s*"
PATTERN =

A regular expression that matches a requirement

/\A#{PATTERN_RAW}\z/
DefaultRequirement =

The default requirement matches any version

[">=", Gem::Version.new(0)]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*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".



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rubygems/requirement.rb', line 108

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

Instance Attribute Details

#requirementsObject (readonly)

An array of requirement pairs. The first element of the pair is the op, and the second is the Gem::Version.



100
101
102
# File 'lib/rubygems/requirement.rb', line 100

def requirements
  @requirements
end

Class Method Details

.create(input) ⇒ Object

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.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rubygems/requirement.rb', line 48

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

.defaultObject

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



66
67
68
# File 'lib/rubygems/requirement.rb', line 66

def self.default
  new '>= 0'
end

.parse(obj) ⇒ Object

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"]


82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rubygems/requirement.rb', line 82

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

Instance Method Details

#==(other) ⇒ Object

:nodoc:



247
248
249
# File 'lib/rubygems/requirement.rb', line 247

def == other # :nodoc:
  Gem::Requirement === other and to_s == other.to_s
end

#as_listObject

:nodoc:



166
167
168
# File 'lib/rubygems/requirement.rb', line 166

def as_list # :nodoc:
  requirements.map { |op, version| "#{op} #{version}" }.sort
end

#concat(new) ⇒ Object

Concatenates the new requirements onto this requirement.



123
124
125
126
127
128
129
130
# File 'lib/rubygems/requirement.rb', line 123

def concat new
  new = new.flatten
  new.compact!
  new.uniq!
  new = new.map { |r| self.class.parse r }

  @requirements.concat new
end

#encode_with(coder) ⇒ Object

:nodoc:



203
204
205
# File 'lib/rubygems/requirement.rb', line 203

def encode_with coder # :nodoc:
  coder.add 'requirements', @requirements
end

#exact?Boolean

true if the requirement is for only an exact version

Returns:

  • (Boolean)


161
162
163
164
# File 'lib/rubygems/requirement.rb', line 161

def exact?
  return false unless @requirements.size == 1
  @requirements[0][0] == "="
end

#for_lockfileObject

Formats this requirement for use in a Gem::RequestSet::Lockfile.



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rubygems/requirement.rb', line 135

def for_lockfile # :nodoc:
  return if [DefaultRequirement] == @requirements

  list = requirements.sort_by { |_, version|
    version
  }.map { |op, version|
    "#{op} #{version}"
  }.uniq

  " (#{list.join ', '})"
end

#hashObject

:nodoc:



170
171
172
# File 'lib/rubygems/requirement.rb', line 170

def hash # :nodoc:
  requirements.hash
end

#init_with(coder) ⇒ Object

:nodoc:



195
196
197
# File 'lib/rubygems/requirement.rb', line 195

def init_with coder # :nodoc:
  yaml_initialize coder.tag, coder.map
end

#marshal_dumpObject

:nodoc:



174
175
176
177
178
# File 'lib/rubygems/requirement.rb', line 174

def marshal_dump # :nodoc:
  fix_syck_default_key_in_requirements

  [@requirements]
end

#marshal_load(array) ⇒ Object

:nodoc:



180
181
182
183
184
# File 'lib/rubygems/requirement.rb', line 180

def marshal_load array # :nodoc:
  @requirements = array[0]

  fix_syck_default_key_in_requirements
end

#none?Boolean

true if this gem has no requirements.

Returns:

  • (Boolean)


150
151
152
153
154
155
156
# File 'lib/rubygems/requirement.rb', line 150

def none?
  if @requirements.size == 1
    @requirements[0] == DefaultRequirement
  else
    false
  end
end

#prerelease?Boolean

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

Returns:

  • (Boolean)


211
212
213
# File 'lib/rubygems/requirement.rb', line 211

def prerelease?
  requirements.any? { |r| r.last.prerelease? }
end

#pretty_print(q) ⇒ Object

:nodoc:



215
216
217
218
219
# File 'lib/rubygems/requirement.rb', line 215

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.

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


224
225
226
227
228
229
# File 'lib/rubygems/requirement.rb', line 224

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

#specific?Boolean

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

Returns:

  • (Boolean)


237
238
239
240
241
# File 'lib/rubygems/requirement.rb', line 237

def specific?
  return true if @requirements.length > 1 # GIGO, > 1, > 2 is silly

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

#to_sObject

:nodoc:



243
244
245
# File 'lib/rubygems/requirement.rb', line 243

def to_s # :nodoc:
  as_list.join ", "
end

#to_yaml_propertiesObject

:nodoc:



199
200
201
# File 'lib/rubygems/requirement.rb', line 199

def to_yaml_properties # :nodoc:
  ["@requirements"]
end

#yaml_initialize(tag, vals) ⇒ Object

:nodoc:



186
187
188
189
190
191
192
193
# File 'lib/rubygems/requirement.rb', line 186

def yaml_initialize(tag, vals) # :nodoc:
  vals.each do |ivar, val|
    instance_variable_set "@#{ivar}", val
  end

  Gem.load_yaml
  fix_syck_default_key_in_requirements
end