Class: GemWhy::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_why/analyzer.rb

Overview

Analyzes gem dependencies to find dependents and dependency chains

Instance Method Summary collapse

Constructor Details

#initializeAnalyzer

Returns a new instance of Analyzer.



8
9
10
# File 'lib/gem_why/analyzer.rb', line 8

def initialize
  @spec_cache = {}
end

Instance Method Details

#find_dependency_chains(target_gem_name) ⇒ Array<Array<Hash>>

Finds all dependency chains leading to the target gem

Parameters:

  • target_gem_name (String)

    the gem to find chains for

Returns:

  • (Array<Array<Hash>>)

    array of dependency chains



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gem_why/analyzer.rb', line 31

def find_dependency_chains(target_gem_name)
  chains = []
  normalized_target = target_gem_name.downcase

  Gem::Specification.each do |spec|
    paths = find_paths_to_target(spec.name, normalized_target, [])
    chains.concat(paths)
  end

  chains.uniq.sort_by { |chain| chain.first[:name] }
end

#find_direct_dependents(target_gem_name) ⇒ Array<Dependent>

Finds all gems that directly depend on the target gem

Parameters:

  • target_gem_name (String)

    the gem to find dependents for

Returns:

  • (Array<Dependent>)

    array of Dependent objects



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gem_why/analyzer.rb', line 15

def find_direct_dependents(target_gem_name)
  Gem::Specification
    .map(&:dependencies).flatten
    .filter { |dep| dep.name.downcase == target_gem_name.downcase }
    .map do |dep|
    Dependent.new(
      name: spec.name,
      version: spec.version.to_s,
      requirement: dep.requirement.to_s
    )
  end.sort_by(&:name)
end