Class: RailsAiBridge::Introspectors::NonArModelsIntrospector
- Inherits:
-
Object
- Object
- RailsAiBridge::Introspectors::NonArModelsIntrospector
- Defined in:
- lib/rails_ai_bridge/introspectors/non_ar_models_introspector.rb
Overview
Discovers concrete Ruby classes under +app/models+ that do not inherit from ActiveRecord::Base, so assistants can surface POJOs and service objects that would not appear in ActiveRecord model introspector.
After a best-effort eager load (including Zeitwerk +eager_load_dir+ for +app/models+ when enabled), entries are derived from +Object.const_source_location+ so anonymous or invalidly named classes are skipped.
Constant Summary collapse
- TAG =
Default label attached to each discovered non-AR class in tool and rules output.
'POJO/Service'
Instance Method Summary collapse
-
#call ⇒ Hash
Builds the non-ActiveRecord model index for MCP and static rules.
-
#initialize(app) ⇒ NonArModelsIntrospector
constructor
Initializes the introspector for a Rails application.
-
#sanitize_error_message(message) ⇒ String
Sanitizes error messages to prevent potential path disclosure Produces a sanitized, truncated error message safe for external exposure.
Constructor Details
#initialize(app) ⇒ NonArModelsIntrospector
Initializes the introspector for a Rails application. Stores the provided Rails application and its root path (as a string) for later introspection.
25 26 27 28 |
# File 'lib/rails_ai_bridge/introspectors/non_ar_models_introspector.rb', line 25 def initialize(app) @app = app @root = app.root.to_s end |
Instance Method Details
#call ⇒ Hash
Builds the non-ActiveRecord model index for MCP and static rules.
Discovers concrete Ruby classes under app/models that do not inherit from ActiveRecord::Base.
The returned hash contains either a :non_ar_models array of discovered entries or an :error string
when introspection fails. Each entry in :non_ar_models is a Hash with:
:name— the constant name of the class (String):relative_path— path to the source file relative to the app root (String):tag— the discovery tag,"POJO/Service"(String)
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rails_ai_bridge/introspectors/non_ar_models_introspector.rb', line 50 def call eager_load! models_dir = File.join(@root, 'app', 'models') return { non_ar_models: [] } unless Dir.exist?(models_dir) models_root = File.(models_dir) entries = collect_entries(models_root) { non_ar_models: entries.values.sort_by { |h| h[:name] } } rescue StandardError => error { error: (error.) } end |
#sanitize_error_message(message) ⇒ String
Sanitizes error messages to prevent potential path disclosure Produces a sanitized, truncated error message safe for external exposure.
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rails_ai_bridge/introspectors/non_ar_models_introspector.rb', line 74 def () return 'Introspection failed' if .blank? # Remove potential file paths that could expose directory structure sanitized = .gsub(%r{/[^\s]*/[^/\s]+}, '/[REDACTED]') # Limit length to prevent log flooding and information disclosure if sanitized.length > 200 "#{sanitized[0...197]}..." else sanitized end end |