class Bundler::Why::Command

Registers and defines the `why` command.

Public Instance Methods

exec(_command, args) click to toggle source
# File lib/bundler/why/command.rb, line 9
def exec(_command, args)
  if args.length == 1
    why(args.first)
  else
    warn 'Usage: bundle why gemname'
  end
end

Private Instance Methods

find_one_spec_in_set(spec_set, gem_name) click to toggle source

@param spec_set Bundler::SpecSet @param gem_name String @return Bundler::StubSpecification

# File lib/bundler/why/command.rb, line 22
def find_one_spec_in_set(spec_set, gem_name)
  specs = spec_set[gem_name]
  if specs.length != 1
    warn format(
      'Expected %s to match exactly 1 spec, got %d',
      gem_name,
      specs.length
    )
  end
  specs.first
end
print_path(path) click to toggle source

@param path Array @void

traverse(spec_set, parent, path = [parent]) click to toggle source

@param spec_set Bundler::SpecSet @param parent Bundler::StubSpecification @param path Array @void

# File lib/bundler/why/command.rb, line 53
def traverse(spec_set, parent, path = [parent])
  children = spec_set.select { |s|
    s.dependencies.any? { |d|
      d.type == :runtime && d.name == parent.name
    }
  }
  if children.empty?
    print_path(path)
  else
    children.each do |child|
      traverse(spec_set, child, [child].concat(path))
    end
  end
end
why(gem_name) click to toggle source

@param gem_name String @void

# File lib/bundler/why/command.rb, line 42
def why(gem_name)
  runtime = Bundler.load
  spec_set = runtime.specs # delegates to Bundler::Definition#specs
  spec = find_one_spec_in_set(spec_set, gem_name)
  traverse(spec_set, spec)
end