Class: ReverseRequire::Gems

Inherits:
Hash
  • Object
show all
Defined in:
lib/reverse_require/gems.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.depending_on(name) ⇒ Object

Creates a new Gems object containing the gems which depend upon the RubyGem of the specified name.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/reverse_require/gems.rb', line 9

def self.depending_on(name)
  name = name.to_s
  spec_dir = File.join(Gem.dir,'specifications')
  gems = Gems.new

  Gem::SourceIndex.from_installed_gems(spec_dir).each do |path,gem_spec|
    deps = gem_spec.dependencies.map { |dep| dep.name }
    
    if deps.include?(name)
      gem_path = File.expand_path(File.join(Gem.dir,'gems',path))
      gems[gem_path] = gem_spec
    end
  end

  return gems
end

Instance Method Details

#map(&block) ⇒ Object

Invokes the given block once for each name and path of the gems. Returns an Array containing the values returned by the block. If no block is given, an empty Array will be returned.



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/reverse_require/gems.rb', line 47

def map(&block)
  mapped = []

  each do |path,gem_spec|
    if block
      mapped << block.call(path,gem_spec)
    end
  end

  return mapped
end

#select(&block) ⇒ Object

Returns a new Gems object containing the names and paths from the gems which match the specified block.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/reverse_require/gems.rb', line 30

def select(&block)
  gems = Gems.new

  each do |path,gem_spec|
    if block.call(path,gem_spec)
      gems[path] = gem_spec
    end
  end

  return gems
end

#with_path(sub_path) ⇒ Object

Returns a new Gems object containing the gems which contain the specified sub_path.

gems.with_path('some/path') # => {...}


65
66
67
68
69
70
71
72
73
# File 'lib/reverse_require/gems.rb', line 65

def with_path(sub_path)
  if File.extname(sub_path).empty?
    sub_path += '.rb'
  end

  select do |path,gem_spec|
    File.exists?(File.expand_path(File.join(path,sub_path)))
  end
end