class Devpack::GemGlob

Locates gems by searching in paths listed in GEM_PATH

Public Instance Methods

find(name) click to toggle source
# File lib/devpack/gem_glob.rb, line 6
def find(name)
  matched_paths(name)
    .sort { |a, b| version(a) <=> version(b) }
    .reverse
end

Private Instance Methods

gem_paths() click to toggle source
# File lib/devpack/gem_glob.rb, line 18
def gem_paths
  return [] unless ENV.key?('GEM_PATH')

  ENV.fetch('GEM_PATH').split(':').map { |path| Pathname.new(path) }
end
glob() click to toggle source
# File lib/devpack/gem_glob.rb, line 14
def glob
  @glob ||= gem_paths.map { |path| Dir.glob(path.join('gems', '*')) }.flatten
end
match?(name_with_version, basename) click to toggle source
# File lib/devpack/gem_glob.rb, line 24
def match?(name_with_version, basename)
  name, _, version = name_with_version.partition(':')
  return true if version.empty? && basename.rpartition('-').first == name
  return true if !version.empty? && basename == "#{name}-#{version}"

  false
end
matched_paths(name) click to toggle source
# File lib/devpack/gem_glob.rb, line 32
def matched_paths(name)
  glob.select do |path|
    pathname = Pathname.new(path)
    next unless pathname.directory?

    basename = pathname.basename.to_s
    match?(name, basename)
  end
end
version(path) click to toggle source
# File lib/devpack/gem_glob.rb, line 42
def version(path)
  Gem::Version.new(File.split(path).last.rpartition('-').last)
end