class RuboCop::Lockfile

Encapsulation of a lockfile for use when checking for gems. Does not actually resolve gems, just parses the lockfile. @api private

Public Instance Methods

dependencies() click to toggle source

Gems that the bundle depends on

# File lib/rubocop/lockfile.rb, line 9
def dependencies
  return [] unless parser

  parser.dependencies.values
end
gems() click to toggle source

All activated gems, including transitive dependencies

# File lib/rubocop/lockfile.rb, line 16
def gems
  return [] unless parser

  # `Bundler::LockfileParser` returns `Bundler::LazySpecification` objects
  # which are not resolved, so extract the dependencies from them
  parser.dependencies.values.concat(parser.specs.flat_map(&:dependencies))
end
includes_gem?(name) click to toggle source
# File lib/rubocop/lockfile.rb, line 24
def includes_gem?(name)
  gems.any? { |gem| gem.name == name }
end

Private Instance Methods

parser() click to toggle source
# File lib/rubocop/lockfile.rb, line 30
def parser
  return unless defined?(Bundler) && Bundler.default_lockfile
  return @parser if defined?(@parser)

  lockfile = Bundler.read_file(Bundler.default_lockfile)
  @parser = lockfile ? Bundler::LockfileParser.new(lockfile) : nil
rescue Bundler::BundlerError
  nil
end