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.

Methods

Public Class methods

Initialise the data we need to make searches later.

[Source]

    # File lib/rubygems/gem_path_searcher.rb, line 16
16:   def initialize
17:     # We want a record of all the installed gemspecs, in the order
18:     # we wish to examine them.
19:     @gemspecs = init_gemspecs
20:     # Map gem spec to glob of full require_path directories.
21:     # Preparing this information may speed up searches later.
22:     @lib_dirs = {}
23:     @gemspecs.each do |spec|
24:       @lib_dirs[spec.object_id] = lib_dirs_for(spec)
25:     end
26:   end

Public Instance methods

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.

[Source]

    # File lib/rubygems/gem_path_searcher.rb, line 48
48:   def find(path)
49:     @gemspecs.find do |spec| matching_file? spec, path end
50:   end

Works like find, but finds all gemspecs matching path.

[Source]

    # File lib/rubygems/gem_path_searcher.rb, line 55
55:   def find_all(path)
56:     @gemspecs.select do |spec|
57:       matching_file? spec, path
58:     end
59:   end

Return a list of all installed gemspecs, sorted by alphabetical order and in reverse version order. (bar-2, bar-1, foo-2)

[Source]

    # File lib/rubygems/gem_path_searcher.rb, line 83
83:   def init_gemspecs
84:     specs = Gem.source_index.map { |_, spec| spec }
85: 
86:     specs.sort { |a, b|
87:       names = a.name <=> b.name
88:       next names if names.nonzero?
89:       b.version <=> a.version
90:     }
91:   end

Returns library directories glob for a gemspec. For example,

  '/usr/local/lib/ruby/gems/1.8/gems/foobar-1.0/{lib,ext}'

[Source]

    # File lib/rubygems/gem_path_searcher.rb, line 97
97:   def lib_dirs_for(spec)
98:     "#{spec.full_gem_path}/{#{spec.require_paths.join(',')}}"
99:   end

Attempts to find a matching path using the require_paths of the given spec.

[Source]

    # File lib/rubygems/gem_path_searcher.rb, line 65
65:   def matching_file?(spec, path)
66:     !matching_files(spec, path).empty?
67:   end

Returns files matching path in spec.

[Source]

    # File lib/rubygems/gem_path_searcher.rb, line 74
74:   def matching_files(spec, path)
75:     glob = File.join @lib_dirs[spec.object_id], "#{path}#{Gem.suffix_pattern}"
76:     Dir[glob].select { |f| File.file? f.untaint }
77:   end

[Validate]