ahoward (owner)

Revisions

gist: 213544 Download_button fork
public
Public Clone URL: git://gist.github.com/213544.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  ##
  # 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.
  # If a path is found in more than one gem the gem with a spec.name == path
  # is preferred over others - in otherwords main-3.0.3/lib/main.rb would be
  # preferred over geonames-1.2.3/lib/main.rb if one issued a 'require "main"'
  # - assuming main had spec.name=='main'.
  #
  # 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.
 
  def find(path)
    first_named = first = nil
    @gemspecs.select do |spec|
      if(matching_file?(spec, path))
        name = File.basename(path).sub(%r/\..*$/,'')
        first ||= spec
        first_named ||= spec if(spec.name == name)
      end
      break if(first_named and first)
    end
    first_named or first
  end