Class | Gem::GemPathSearcher |
In: |
lib/rubygems/gem_path_searcher.rb
|
Parent: | Object |
GemPathSearcher has the capability to find loadable files inside gems. It generates data up front to speed up searches later.
Initialise the data we need to make searches later.
# File lib/rubygems/gem_path_searcher.rb, line 18 18: def initialize 19: # We want a record of all the installed gemspecs, in the order 20: # we wish to examine them. 21: @gemspecs = init_gemspecs 22: # Map gem spec to glob of full require_path directories. 23: # Preparing this information may speed up searches later. 24: @lib_dirs = {} 25: @gemspecs.each do |spec| 26: @lib_dirs[spec.object_id] = lib_dirs_for(spec) 27: end 28: end
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.
# File lib/rubygems/gem_path_searcher.rb, line 50 50: def find(path) 51: @gemspecs.each do |spec| 52: return spec if matching_file(spec, path) 53: end 54: nil 55: end
Attempts to find a matching path using the require_paths of the given spec.
Some of the intermediate results are cached in @lib_dirs for speed.
# File lib/rubygems/gem_path_searcher.rb, line 64 64: def matching_file(spec, path) # :doc: 65: glob = File.join @lib_dirs[spec.object_id], "#{path}#{Gem.suffix_pattern}" 66: return true unless Dir[glob].select { |f| File.file?(f.untaint) }.empty? 67: end