Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created October 19, 2009 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahoward/213544 to your computer and use it in GitHub Desktop.
Save ahoward/213544 to your computer and use it in GitHub Desktop.
##
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment