Class: Woods::Ast::MethodExtractor
- Inherits:
-
Object
- Object
- Woods::Ast::MethodExtractor
- Includes:
- SourceSpan
- Defined in:
- lib/woods/ast/method_extractor.rb
Overview
Extracts method definitions and their source from Ruby source code.
Replaces the fragile ~240 lines of ‘nesting_delta` / `neutralize_strings_and_comments` / `detect_heredoc_start` indentation heuristics in controller and mailer extractors.
Instance Method Summary collapse
-
#extract_all_methods(source) ⇒ Array<Ast::Node>
Extract all method definition nodes from source.
-
#extract_method(source, method_name, class_method: false) ⇒ Ast::Node?
Extract a method definition node by name.
-
#extract_method_source(source, method_name, class_method: false) ⇒ String?
Extract the raw source text of a method, including def…end.
-
#initialize(parser: nil) ⇒ MethodExtractor
constructor
A new instance of MethodExtractor.
Constructor Details
#initialize(parser: nil) ⇒ MethodExtractor
Returns a new instance of MethodExtractor.
22 23 24 |
# File 'lib/woods/ast/method_extractor.rb', line 22 def initialize(parser: nil) @parser = parser || Parser.new end |
Instance Method Details
#extract_all_methods(source) ⇒ Array<Ast::Node>
Extract all method definition nodes from source.
45 46 47 48 |
# File 'lib/woods/ast/method_extractor.rb', line 45 def extract_all_methods(source) root = @parser.parse(source) root.find_all(:def) + root.find_all(:defs) end |
#extract_method(source, method_name, class_method: false) ⇒ Ast::Node?
Extract a method definition node by name.
32 33 34 35 36 37 38 39 |
# File 'lib/woods/ast/method_extractor.rb', line 32 def extract_method(source, method_name, class_method: false) root = @parser.parse(source) target_type = class_method ? :defs : :def root.find_all(target_type).find do |node| node.method_name == method_name.to_s end end |
#extract_method_source(source, method_name, class_method: false) ⇒ String?
Extract the raw source text of a method, including def…end.
This is the key replacement for ‘extract_action_source` in the controller and mailer extractors. Uses AST line tracking instead of indentation heuristics.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/woods/ast/method_extractor.rb', line 59 def extract_method_source(source, method_name, class_method: false) node = extract_method(source, method_name, class_method: class_method) return nil unless node # If the node has a source field populated by the parser, use it return node.source if node.source # Fallback: extract by line range extract_source_span(source, node.line, node.end_line) end |