Class: RailsMcpInsight::Analyzers::AstParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_mcp_insight/analyzers/ast_parser.rb

Overview

Lightweight Ruby AST parser using regex-based analysis. Extracts class definitions, method definitions, module inclusions, DSL calls (has_many, belongs_to, validates, etc.) from Ruby source files.

This avoids requiring Prism as a hard dependency while still providing useful structural information. Can be upgraded to Prism later for more accurate parsing.

Defined Under Namespace

Classes: ClassInfo, DslCall, MethodInfo, ParseResult

Instance Method Summary collapse

Constructor Details

#initializeAstParser

Returns a new instance of AstParser.



33
34
35
# File 'lib/rails_mcp_insight/analyzers/ast_parser.rb', line 33

def initialize
  @cache = {}
end

Instance Method Details

#clear_cache!Object

Clear the parse cache



48
49
50
# File 'lib/rails_mcp_insight/analyzers/ast_parser.rb', line 48

def clear_cache!
  @cache.clear
end

#parse_content(content, file_path = "(string)") ⇒ Object

Parse Ruby source content



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rails_mcp_insight/analyzers/ast_parser.rb', line 53

def parse_content(content, file_path = "(string)")
  lines = content.lines

  ParseResult.new(
    file_path: file_path,
    classes: extract_classes(lines),
    modules: extract_modules(lines),
    methods: extract_methods(lines),
    includes: extract_includes(lines),
    extends: extract_extends(lines),
    dsl_calls: extract_dsl_calls(lines),
    constants: extract_constants(lines),
    comments: extract_comments(lines)
  )
end

#parse_file(file_path) ⇒ Object

Parse a Ruby file and return structured information



38
39
40
41
42
43
44
45
# File 'lib/rails_mcp_insight/analyzers/ast_parser.rb', line 38

def parse_file(file_path)
  return nil unless File.exist?(file_path)

  @cache[file_path] ||= begin
    content = File.read(file_path)
    parse_content(content, file_path)
  end
end