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 13 13: def initialize 14: # We want a record of all the installed gemspecs, in the order we wish to 15: # examine them. 16: # TODO: remove this stupid method 17: @gemspecs = init_gemspecs 18: 19: # Map gem spec to glob of full require_path directories. Preparing this 20: # information may speed up searches later. 21: @lib_dirs = {} 22: 23: @gemspecs.each do |spec| 24: @lib_dirs[spec.object_id] = lib_dirs_for spec 25: end 26: end
Look in all the installed gems until a matching glob 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 48 48: def find(glob) 49: # HACK violation of encapsulation 50: @gemspecs.find do |spec| 51: # TODO: inverted responsibility 52: matching_file? spec, glob 53: end 54: end
# File lib/rubygems/gem_path_searcher.rb, line 65 65: def find_active(glob) 66: # HACK violation of encapsulation 67: @gemspecs.find do |spec| 68: # TODO: inverted responsibility 69: spec.loaded? and matching_file? spec, glob 70: end 71: end
Works like #, but finds all gemspecs matching glob.
# File lib/rubygems/gem_path_searcher.rb, line 76 76: def find_all(glob) 77: # HACK violation of encapsulation 78: @gemspecs.select do |spec| 79: # TODO: inverted responsibility 80: matching_file? spec, glob 81: end || [] 82: end
# File lib/rubygems/gem_path_searcher.rb, line 84 84: def find_in_unresolved(glob) 85: # HACK violation 86: specs = Gem.unresolved_deps.values.map { |dep| 87: Gem.source_index.search dep, true 88: }.flatten 89: 90: specs.select do |spec| 91: # TODO: inverted responsibility 92: matching_file? spec, glob 93: end || [] 94: end
# File lib/rubygems/gem_path_searcher.rb, line 96 96: def find_in_unresolved_tree glob 97: # HACK violation 98: # TODO: inverted responsibility 99: specs = Gem.unresolved_deps.values.map { |dep| 100: Gem.source_index.search dep, true 101: }.flatten 102: 103: specs.reverse_each do |spec| 104: trails = matching_paths(spec, glob) 105: next if trails.empty? 106: return trails.map(&:reverse).sort.first.reverse 107: end 108: 109: [] 110: end
Looks through the available gemspecs and finds the first one that contains file as a requirable file.
# File lib/rubygems/gem_path_searcher.rb, line 59 59: def find_spec_for_file(file) 60: @gemspecs.find do |spec| 61: return spec if spec.contains_requirable_file?(file) 62: end 63: end
Return a list of all installed gemspecs, sorted by alphabetical order and in reverse version order. (bar-2, bar-1, foo-2)
# File lib/rubygems/gem_path_searcher.rb, line 146 146: def init_gemspecs 147: Gem::Specification.sort { |a, b| 148: names = a.name <=> b.name 149: next names if names.nonzero? 150: b.version <=> a.version 151: } 152: end
Returns library directories glob for a gemspec. For example,
'/usr/local/lib/ruby/gems/1.8/gems/foobar-1.0/{lib,ext}'
# File lib/rubygems/gem_path_searcher.rb, line 158 158: def lib_dirs_for(spec) 159: "#{spec.full_gem_path}/{#{spec.require_paths.join(',')}}" if 160: spec.require_paths 161: end
Attempts to find a matching path using the require_paths of the given spec.
# File lib/rubygems/gem_path_searcher.rb, line 116 116: def matching_file?(spec, path) 117: not matching_files(spec, path).empty? 118: end
Returns files matching path in spec.
# File lib/rubygems/gem_path_searcher.rb, line 136 136: def matching_files(spec, path) 137: return [] unless @lib_dirs[spec.object_id] # case no paths 138: glob = File.join @lib_dirs[spec.object_id], "#{path}#{Gem.suffix_pattern}" 139: Dir[glob].select { |f| File.file? f.untaint } 140: end
# File lib/rubygems/gem_path_searcher.rb, line 120 120: def matching_paths(spec, path) 121: trails = [] 122: 123: spec.traverse do |from_spec, dep, to_spec, trail| 124: next unless to_spec.conflicts.empty? 125: trails << trail unless matching_files(to_spec, path).empty? 126: end 127: 128: trails 129: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.