Class: Gem::SourceIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_extension/source_index.rb

Instance Method Summary collapse

Instance Method Details

#locate(gem_pattern, platform_only = false) ⇒ Object

This is basically the same as Gem::SourceIndex.search



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
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gem_extension/source_index.rb', line 4

def locate(gem_pattern, platform_only = false)
  version_requirement = nil
  only_platform = false

  # TODO - Remove support and warning for legacy arguments after 2008/11
  unless Gem::Dependency === gem_pattern
    warn "#{Gem.location_of_caller.join ':'}:Warning: Gem::SourceIndex#search support for #{gem_pattern.class} patterns is deprecated, use #find_name"
  end

  case gem_pattern
  when Regexp then
    version_requirement = platform_only || Gem::Requirement.default
  when Gem::Dependency then
    only_platform = platform_only
    version_requirement = (Gem::VERSION == '1.3.6') ? gem_pattern.requirement : gem_pattern.version_requirements
    gem_pattern = if Regexp === gem_pattern.name then
                    gem_pattern.name
                  elsif gem_pattern.name.empty? then
                    //
                  else
                    /^#{Regexp.escape gem_pattern.name}$/
                  end
  else
    version_requirement = platform_only || Gem::Requirement.default
    gem_pattern = /#{gem_pattern}/i
  end

  unless Gem::Requirement === version_requirement then
    version_requirement = Gem::Requirement.create version_requirement
  end

  specs = all_gems.values.select do |spec|
    if version_requirement.satisfied_by? spec.version
      [spec.name, spec.description, spec.summary].detect{|q| (q =~ gem_pattern) if q }
    end
  end

  if only_platform then
    specs = specs.select do |spec|
      Gem::Platform.match spec.platform
    end
  end

  specs.sort_by { |s| s.sort_obj }
end