Class: RailsAiContext::Introspectors::SourceIntrospector

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_context/introspectors/source_introspector.rb

Overview

Single-pass Prism AST introspector using the Dispatcher pattern. Walks the AST once and feeds events to all registered listeners, extracting associations, validations, scopes, enums, callbacks, macros, and methods in a single tree traversal.

Can be called with a file path (cached via AstCache) or a source string.

Constant Summary collapse

LISTENER_MAP =

Map result keys to listener classes. Iteration order is preserved (Ruby >= 1.9), but results are accessed by key - never by index.

{
  associations: Listeners::AssociationsListener,
  validations:  Listeners::ValidationsListener,
  scopes:       Listeners::ScopesListener,
  enums:        Listeners::EnumsListener,
  callbacks:    Listeners::CallbacksListener,
  macros:       Listeners::MacrosListener,
  methods:      Listeners::MethodsListener,
  mixins:       Listeners::MixinsListener
}.freeze

Class Method Summary collapse

Class Method Details

.call(path) ⇒ Object

Introspect a file on disk (cached parse) with default listeners.



28
29
30
# File 'lib/rails_ai_context/introspectors/source_introspector.rb', line 28

def self.call(path)
  walk(path)
end

.from_source(source) ⇒ Object

Introspect a source string (no caching) with default listeners.



33
34
35
# File 'lib/rails_ai_context/introspectors/source_introspector.rb', line 33

def self.from_source(source)
  walk_source(source)
end

.walk(path, listener_map = LISTENER_MAP) ⇒ Object

Walk a file with a custom listener map. Returns { key => results_array }.



38
39
40
41
# File 'lib/rails_ai_context/introspectors/source_introspector.rb', line 38

def self.walk(path, listener_map = LISTENER_MAP)
  result = AstCache.parse(path)
  walk_dispatch(result, listener_map)
end

.walk_source(source, listener_map = LISTENER_MAP) ⇒ Object

Walk a source string with a custom listener map. Returns { key => results_array }.



44
45
46
47
# File 'lib/rails_ai_context/introspectors/source_introspector.rb', line 44

def self.walk_source(source, listener_map = LISTENER_MAP)
  result = AstCache.parse_string(source)
  walk_dispatch(result, listener_map)
end