Class: GemGuard::AutoFixer

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_guard/auto_fixer.rb

Instance Method Summary collapse

Constructor Details

#initialize(lockfile_path = "Gemfile.lock", gemfile_path = "Gemfile") ⇒ AutoFixer

Returns a new instance of AutoFixer.



7
8
9
10
11
# File 'lib/gem_guard/auto_fixer.rb', line 7

def initialize(lockfile_path = "Gemfile.lock", gemfile_path = "Gemfile")
  @lockfile_path = lockfile_path
  @gemfile_path = gemfile_path
  @backup_created = false
end

Instance Method Details

#fix_vulnerabilities(vulnerable_dependencies, options = {}) ⇒ Object



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_guard/auto_fixer.rb', line 13

def fix_vulnerabilities(vulnerable_dependencies, options = {})
  dry_run = options.fetch(:dry_run, false)
  interactive = options.fetch(:interactive, false)
  create_backup = options.fetch(:backup, true)

  unless File.exist?(@gemfile_path)
    raise GemGuard::FileError, "Gemfile not found at #{@gemfile_path}. Auto-fix requires a Gemfile."
  end

  unless File.exist?(@lockfile_path)
    raise GemGuard::FileError, "Gemfile.lock not found at #{@lockfile_path}. Run 'bundle install' first."
  end

  fixes = plan_fixes(vulnerable_dependencies)

  if fixes.empty?
    return {status: :no_fixes_needed, message: "No automatic fixes available."}
  end

  if dry_run
    return {status: :dry_run, fixes: fixes, message: "Dry run completed. #{fixes.length} fixes planned."}
  end

  # Apply fixes with optional per-gem confirmation
  applied_fixes, cancelled = apply_fixes(fixes, interactive: interactive, backup: create_backup)

  if cancelled
    return {status: :cancelled, message: "No fixes approved."}
  end

  {
    status: :completed,
    fixes: applied_fixes,
    message: "Applied #{applied_fixes.length} fixes successfully."
  }
end