Class: Gem::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/rubygems/version.rb,
lib/rubygems/requirement.rb

Overview

This is needed for compatibility with older yaml gemspecs.

Constant Summary collapse

VERSION_PATTERN =

FIX: These are only used once, in .correct?. Do they deserve to be constants?

'[0-9]+(\.[0-9a-zA-Z]+)*'
ANCHORED_VERSION_PATTERN =

:nodoc:

/\A\s*(#{VERSION_PATTERN})*\s*\z/
Requirement =
Gem::Requirement

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Version

Constructs a Version from the version string. A version string is a series of digits or ASCII letters separated by dots.

Raises:

  • (ArgumentError)


190
191
192
193
194
195
# File 'lib/rubygems/version.rb', line 190

def initialize version
  raise ArgumentError, "Malformed version number string #{version}" unless
    self.class.correct?(version)

  @version = version.to_s.dup.strip
end

Instance Attribute Details

#versionObject (readonly) Also known as: to_s

A string representation of this Version.



156
157
158
# File 'lib/rubygems/version.rb', line 156

def version
  @version
end

Class Method Details

.correct?(version) ⇒ Boolean

True if the version string matches RubyGems’ requirements.

Returns:

  • (Boolean)


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

def self.correct? version
  version.to_s =~ ANCHORED_VERSION_PATTERN
end

.create(input) ⇒ Object

REFACTOR: There’s no real reason this should be separate from #initialize.



176
177
178
179
180
181
182
183
184
# File 'lib/rubygems/version.rb', line 176

def self.create input
  if input.respond_to? :version then
    input
  elsif input.nil? then
    nil
  else
    new input
  end
end

Instance Method Details

#<=>(other) ⇒ Object

Compares this version with other returning -1, 0, or 1 if the other version is larger, the same, or smaller than this one. Attempts to compare to something that’s not a Gem::Version return nil.



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/rubygems/version.rb', line 314

def <=> other
  return unless Gem::Version === other
  return 0 if @version == other.version

  lhsegments = segments
  rhsegments = other.segments

  lhsize = lhsegments.size
  rhsize = rhsegments.size
  limit  = (lhsize > rhsize ? lhsize : rhsize) - 1

  i = 0

  while i <= limit
    lhs, rhs = lhsegments[i] || 0, rhsegments[i] || 0
    i += 1

    next      if lhs == rhs
    return -1 if String  === lhs && Numeric === rhs
    return  1 if Numeric === lhs && String  === rhs

    return lhs <=> rhs
  end

  return 0
end

#approximate_recommendationObject

A recommended version for use with a ~> Requirement.



298
299
300
301
302
303
304
305
306
# File 'lib/rubygems/version.rb', line 298

def approximate_recommendation
  segments = self.segments.dup

  segments.pop    while segments.any? { |s| String === s }
  segments.pop    while segments.size > 2
  segments.push 0 while segments.size < 2

  "~> #{segments.join(".")}"
end

#bumpObject

Return a new version object where the next to the last revision number is one greater (e.g., 5.3.1 => 5.4).

Pre-release (alpha) parts, e.g, 5.3.1.b.2 => 5.4, are ignored.



203
204
205
206
207
208
209
210
# File 'lib/rubygems/version.rb', line 203

def bump
  segments = self.segments.dup
  segments.pop while segments.any? { |s| String === s }
  segments.pop if segments.size > 1

  segments[-1] = segments[-1].succ
  self.class.new segments.join(".")
end

#encode_with(coder) ⇒ Object



258
259
260
# File 'lib/rubygems/version.rb', line 258

def encode_with coder
  coder.add 'version', @version
end

#eql?(other) ⇒ Boolean

A Version is only eql? to another version if it’s specified to the same precision. Version “1.0” is not the same as version “1”.

Returns:

  • (Boolean)


216
217
218
# File 'lib/rubygems/version.rb', line 216

def eql? other
  self.class === other and @version == other.version
end

#hashObject

:nodoc:



220
221
222
# File 'lib/rubygems/version.rb', line 220

def hash # :nodoc:
  @hash ||= segments.hash
end

#init_with(coder) ⇒ Object

:nodoc:



224
225
226
# File 'lib/rubygems/version.rb', line 224

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

#inspectObject

:nodoc:



228
229
230
# File 'lib/rubygems/version.rb', line 228

def inspect # :nodoc:
  "#<#{self.class} #{version.inspect}>"
end

#marshal_dumpObject

Dump only the raw version string, not the complete object. It’s a string for backwards (RubyGems 1.3.5 and earlier) compatibility.



236
237
238
# File 'lib/rubygems/version.rb', line 236

def marshal_dump
  [version]
end

#marshal_load(array) ⇒ Object

Load custom marshal format. It’s a string for backwards (RubyGems 1.3.5 and earlier) compatibility.



244
245
246
# File 'lib/rubygems/version.rb', line 244

def marshal_load array
  initialize array[0]
end

#prerelease?Boolean

A version is considered a prerelease if it contains a letter.

Returns:

  • (Boolean)


265
266
267
# File 'lib/rubygems/version.rb', line 265

def prerelease?
  @prerelease ||= !!(@version =~ /[a-zA-Z]/)
end

#pretty_print(q) ⇒ Object

:nodoc:



269
270
271
# File 'lib/rubygems/version.rb', line 269

def pretty_print q # :nodoc:
  q.text "Gem::Version.new(#{version.inspect})"
end

#releaseObject

The release for this version (e.g. 1.2.0.a -> 1.2.0). Non-prerelease versions return themselves.



277
278
279
280
281
282
283
# File 'lib/rubygems/version.rb', line 277

def release
  return self unless prerelease?

  segments = self.segments.dup
  segments.pop while segments.any? { |s| String === s }
  self.class.new segments.join('.')
end

#segmentsObject

:nodoc:



285
286
287
288
289
290
291
292
293
# File 'lib/rubygems/version.rb', line 285

def segments # :nodoc:

  # segments is lazy so it can pick up version values that come from
  # old marshaled versions, which don't go through marshal_load.

  @segments ||= @version.scan(/[0-9]+|[a-z]+/i).map do |s|
    /^\d+$/ =~ s ? s.to_i : s
  end
end

#to_yaml_propertiesObject



254
255
256
# File 'lib/rubygems/version.rb', line 254

def to_yaml_properties
  ["@version"]
end

#yaml_initialize(tag, map) ⇒ Object



248
249
250
251
252
# File 'lib/rubygems/version.rb', line 248

def yaml_initialize(tag, map)
  @version = map['version']
  @segments = nil
  @hash = nil
end