Class: AIRspecWriter::SpecFileHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/ai_rspec_writer/spec_file_handler.rb

Constant Summary collapse

SPEC_DIRS =
{
  "app/models/" => "spec/models",
  "app/controllers/" => "spec/controllers"
}.freeze

Class Method Summary collapse

Class Method Details

.determine_spec_path(file_path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ai_rspec_writer/spec_file_handler.rb', line 15

def determine_spec_path(file_path)
  # Ensure we strip out "app/" to avoid double "models/" issue
  relative_path = file_path.sub(%r{^app/}, "")

  # Find matching folder mapping
  spec_dir = SPEC_DIRS.find { |prefix, _| relative_path.start_with?(prefix.sub("app/", "")) }
  spec_folder = spec_dir ? spec_dir[1] : "spec/other"

  # Ensure correct path
  File.join(spec_folder, relative_path.sub(%r{^models/|controllers/}, "").sub(".rb", "_spec.rb"))
end

.save_spec_file(file_path, spec_code) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ai_rspec_writer/spec_file_handler.rb', line 27

def save_spec_file(file_path, spec_code)
  # Define loading spinner
  spinner = TTY::Spinner.new("[:spinner] RSpec File Create".yellow, format: :dots)

  output_path = determine_spec_path(file_path)

  FileUtils.mkdir_p(File.dirname(output_path))
  File.write(output_path, spec_code)
  # Stop the spinner when AI response is ready
  spinner.success("✅ Done!")
  puts "   - Open the file & review: `code #{output_path}`".light_blue
  puts "   - Run the test: `bundle exec rspec #{output_path}`".light_blue
  puts "   - Refactor if needed & improve test coverage! 🚀\n".green
end