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 10
10:   def initialize
11:     # We want a record of all the installed gemspecs, in the order we wish to
12:     # examine them.
13:     @gemspecs = init_gemspecs
14: 
15:     # Map gem spec to glob of full require_path directories.  Preparing this
16:     # information may speed up searches later.
17:     @lib_dirs = {}
18: 
19:     @gemspecs.each do |spec|
20:       @lib_dirs[spec.object_id] = lib_dirs_for spec
21:     end
22:   end

Public Instance methods

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.

[Source]

    # File lib/rubygems/gem_path_searcher.rb, line 44
44:   def find(glob)
45:     @gemspecs.find do |spec|
46:       matching_file? spec, glob
47:     end
48:   end

Works like find, but finds all gemspecs matching glob.

[Source]

    # File lib/rubygems/gem_path_searcher.rb, line 53
53:   def find_all(glob)
54:     @gemspecs.select do |spec|
55:       matching_file? spec, glob
56:     end
57:   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 82
82:   def init_gemspecs
83:     specs = Gem.source_index.map { |_, spec| spec }
84: 
85:     specs.sort { |a, b|
86:       names = a.name <=> b.name
87:       next names if names.nonzero?
88:       b.version <=> a.version
89:     }
90:   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 96
96:   def lib_dirs_for(spec)
97:     "#{spec.full_gem_path}/{#{spec.require_paths.join(',')}}" if
98:       spec.require_paths
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 63
63:   def matching_file?(spec, path)
64:     !matching_files(spec, path).empty?
65:   end

Returns files matching path in spec.

[Source]

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

[Validate]