Class: Gem::GemPathSearcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems/custom_require.rb

Overview

GemPathSearcher has the capability to find loadable files inside gems. It generates data up front to speed up searches later.

Instance Method Summary collapse

Constructor Details

#initializeGemPathSearcher

Initialise the data we need to make searches later.



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

def initialize
  # We want a record of all the installed gemspecs, in the order
  # we wish to examine them.
  @gemspecs = init_gemspecs
  # Map gem spec to glob of full require_path directories.
  # Preparing this information may speed up searches later.
  @lib_dirs = {}
  @gemspecs.each do |spec|
    @lib_dirs[spec.object_id] = lib_dirs(spec)
  end
end

Instance Method Details

#find(path) ⇒ Object

Look in all the installed gems until a matching path is found. Return the gemspec of the gem where it was found. If no match is found, return nil.

The gems are searched in alphabetical order, and in reverse version order.

For example:

find('log4r')              # -> (log4r-1.1 spec)
find('log4r.rb')           # -> (log4r-1.1 spec)
find('rake/rdoctask')      # -> (rake-0.4.12 spec)
find('foobarbaz')          # -> nil

Matching paths can have various suffixes (‘.rb’, ‘.so’, and others), which may or may not already be attached to file. This method doesn’t care about the full filename that matches; only that there is a match.



83
84
85
86
87
88
# File 'lib/rubygems/custom_require.rb', line 83

def find(path)
  @gemspecs.each do |spec|
    return spec if matching_file(spec, path)
  end
  nil
end