Class: Ace::Retro::Molecules::RetroDoctorFixer

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/retro/molecules/retro_doctor_fixer.rb

Overview

Handles auto-fixing of common retro issues detected by doctor. Supports dry_run mode to preview fixes without applying them.

Constant Summary collapse

FIXABLE_PATTERNS =
[
  /Missing opening '---' delimiter/,
  /Missing closing '---' delimiter/,
  /Missing required field: id/,
  /Missing required field: status/,
  /Missing required field: title/,
  /Missing required field: type/,
  /Missing required field: created_at/,
  /Missing recommended field: status/,
  /Missing recommended field: title/,
  /Missing recommended field: tags/,
  /Field 'tags' is not an array/,
  /terminal status.*not in _archive/,
  /in _archive\/ but status is/,
  /Invalid archive partition/,
  /Stale backup file/,
  /Empty directory/
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dry_run: false, root_dir: nil) ⇒ RetroDoctorFixer

Returns a new instance of RetroDoctorFixer.



20
21
22
23
24
25
26
# File 'lib/ace/retro/molecules/retro_doctor_fixer.rb', line 20

def initialize(dry_run: false, root_dir: nil)
  @dry_run = dry_run
  @root_dir = root_dir
  @fixed_count = 0
  @skipped_count = 0
  @fixes_applied = []
end

Instance Attribute Details

#dry_runObject (readonly)

Returns the value of attribute dry_run.



18
19
20
# File 'lib/ace/retro/molecules/retro_doctor_fixer.rb', line 18

def dry_run
  @dry_run
end

#fixed_countObject (readonly)

Returns the value of attribute fixed_count.



18
19
20
# File 'lib/ace/retro/molecules/retro_doctor_fixer.rb', line 18

def fixed_count
  @fixed_count
end

#skipped_countObject (readonly)

Returns the value of attribute skipped_count.



18
19
20
# File 'lib/ace/retro/molecules/retro_doctor_fixer.rb', line 18

def skipped_count
  @skipped_count
end

Instance Method Details

#can_fix?(issue) ⇒ Boolean

Check if an issue can be auto-fixed

Parameters:

  • issue (Hash)

    Issue to check

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/ace/retro/molecules/retro_doctor_fixer.rb', line 90

def can_fix?(issue)
  return false unless issue[:location]

  FIXABLE_PATTERNS.any? { |pattern| issue[:message].match?(pattern) }
end

#fix_issue(issue) ⇒ Boolean

Fix a single issue by pattern matching its message

Parameters:

  • issue (Hash)

    Issue to fix

Returns:

  • (Boolean)

    Whether fix was successful



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ace/retro/molecules/retro_doctor_fixer.rb', line 49

def fix_issue(issue)
  case issue[:message]
  when /Missing opening '---' delimiter/
    fix_missing_opening_delimiter(issue[:location])
  when /Missing closing '---' delimiter/
    fix_missing_closing_delimiter(issue[:location])
  when /Missing required field: id/
    fix_missing_id(issue[:location])
  when /Missing required field: status/,
       /Missing recommended field: status/
    fix_missing_status(issue[:location])
  when /Missing required field: title/,
       /Missing recommended field: title/
    fix_missing_title(issue[:location])
  when /Missing required field: type/
    fix_missing_type(issue[:location])
  when /Missing required field: created_at/
    fix_missing_created_at(issue[:location])
  when /Field 'tags' is not an array/
    fix_tags_not_array(issue[:location])
  when /Missing recommended field: tags/
    fix_missing_tags(issue[:location])
  when /terminal status.*not in _archive/
    fix_move_to_archive(issue[:location])
  when /in _archive\/ but status is/
    fix_archive_status(issue[:location])
  when /Invalid archive partition/
    fix_invalid_archive_partition(issue[:location])
  when /Stale backup file/
    fix_stale_backup(issue[:location])
  when /Empty directory/
    fix_empty_directory(issue[:location])
  else
    @skipped_count += 1
    false
  end
end

#fix_issues(issues) ⇒ Hash

Fix a batch of issues

Parameters:

  • issues (Array<Hash>)

    Issues to fix

Returns:

  • (Hash)

    Fix results summary



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ace/retro/molecules/retro_doctor_fixer.rb', line 31

def fix_issues(issues)
  fixable_issues = issues.select { |issue| can_fix?(issue) }

  fixable_issues.each do |issue|
    fix_issue(issue)
  end

  {
    fixed: @fixed_count,
    skipped: @skipped_count,
    fixes_applied: @fixes_applied,
    dry_run: @dry_run
  }
end