Class: Docit::Ai::ScaffoldGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/docit/ai/scaffold_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(output: $stdout) ⇒ ScaffoldGenerator

Returns a new instance of ScaffoldGenerator.



9
10
11
12
# File 'lib/docit/ai/scaffold_generator.rb', line 9

def initialize(output: $stdout)
  @output = output
  @files_written = []
end

Instance Method Details

#runObject



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
49
50
51
52
53
54
55
56
57
# File 'lib/docit/ai/scaffold_generator.rb', line 14

def run
  check_base_setup!
  gaps = detect_gaps

  if gaps.empty?
    @output.puts "No undocumented endpoints found."
    return @files_written
  end

  @output.puts "Found #{gaps.length} endpoint#{"s" if gaps.length > 1} to scaffold:"
  gaps.each { |g| @output.puts "  #{g[:method].upcase} #{g[:path]} (#{g[:controller]}##{g[:action]})" }
  @output.puts ""

  grouped = gaps.group_by { |g| g[:controller] }

  grouped.each do |controller, controller_gaps|
    writer = DocWriter.new(controller_name: controller)

    if writer.file_exists?
      existing_actions = existing_doc_actions(writer.doc_file_path)
      controller_gaps = controller_gaps.reject { |g| existing_actions.include?(g[:action]) }
      next if controller_gaps.empty?
    end

    blocks = controller_gaps.map { |gap| build_placeholder(gap, controller) }
    writer.write(blocks)
    @files_written << writer.doc_file_path

    relative = writer.doc_file_path.sub("#{Rails.root}/", "")
    @output.puts "  Created: #{relative}"

    if writer.inject_use_docs
      controller_relative = File.join("app", "controllers", "#{controller.underscore}.rb")
      @output.puts "  Added use_docs to #{controller_relative}"
    end
  end

  inject_tags(grouped)

  @output.puts ""
  @output.puts "Scaffolded #{gaps.length} endpoint#{"s" if gaps.length > 1} in #{@files_written.length} file#{"s" if @files_written.length > 1}."
  @output.puts "Fill in the TODO placeholders in your doc files."
  @files_written
end