Class: RailsAiBridge::Introspector

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/introspector.rb

Overview

Orchestrates all sub-introspectors to build a complete picture of the Rails application for AI consumption.

Constant Summary collapse

BUILTIN_INTROSPECTORS =
{
  schema: Introspectors::SchemaIntrospector,
  models: Introspectors::ModelIntrospector,
  non_ar_models: Introspectors::NonArModelsIntrospector,
  routes: Introspectors::RouteIntrospector,
  jobs: Introspectors::JobIntrospector,
  gems: Introspectors::GemIntrospector,
  conventions: Introspectors::ConventionDetector,
  stimulus: Introspectors::StimulusIntrospector,
  database_stats: Introspectors::DatabaseStatsIntrospector,
  controllers: Introspectors::ControllerIntrospector,
  views: Introspectors::ViewIntrospector,
  turbo: Introspectors::TurboIntrospector,
  i18n: Introspectors::I18nIntrospector,
  config: Introspectors::ConfigIntrospector,
  active_storage: Introspectors::ActiveStorageIntrospector,
  action_text: Introspectors::ActionTextIntrospector,
  auth: Introspectors::AuthIntrospector,
  api: Introspectors::ApiIntrospector,
  tests: Introspectors::TestIntrospector,
  rake_tasks: Introspectors::RakeTaskIntrospector,
  assets: Introspectors::AssetPipelineIntrospector,
  devops: Introspectors::DevOpsIntrospector,
  action_mailbox: Introspectors::ActionMailboxIntrospector,
  migrations: Introspectors::MigrationIntrospector,
  seeds: Introspectors::SeedsIntrospector,
  middleware: Introspectors::MiddlewareIntrospector,
  engines: Introspectors::EngineIntrospector,
  multi_database: Introspectors::MultiDatabaseIntrospector
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Introspector

Returns a new instance of Introspector.



9
10
11
12
# File 'lib/rails_ai_bridge/introspector.rb', line 9

def initialize(app)
  @app    = app
  @config = RailsAiBridge.configuration
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



7
8
9
# File 'lib/rails_ai_bridge/introspector.rb', line 7

def app
  @app
end

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/rails_ai_bridge/introspector.rb', line 7

def config
  @config
end

Instance Method Details

#app_nameObject



70
71
72
73
74
75
76
# File 'lib/rails_ai_bridge/introspector.rb', line 70

def app_name
  if app.class.respond_to?(:module_parent_name)
    app.class.module_parent_name
  else
    app.class.name.deconstantize
  end
end

#call(only: nil) ⇒ Hash

Run all configured introspectors and return unified context hash

Parameters:

  • only (Array<Symbol>, nil) (defaults to: nil)

    optional subset of introspectors to execute

Returns:

  • (Hash)

    complete application context or metadata + requested sections



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rails_ai_bridge/introspector.rb', line 18

def call(only: nil)
  context = {
    app_name: app_name,
    ruby_version: RUBY_VERSION,
    rails_version: Rails.version,
    environment: Rails.env,
    generated_at: Time.current.iso8601,
    generator: "rails-ai-bridge v#{RailsAiBridge::VERSION}"
  }

  selected_introspectors(only).each do |name|
    introspector = resolve_introspector(name)
    context[name] = introspector.call
  rescue StandardError => error
    context[name] = { error: error.message }
    Rails.logger.warn "[rails-ai-bridge] #{name} introspection failed: #{error.message}"
  end

  context
end

#resolve_introspector(name) ⇒ Object

Raises:



85
86
87
88
89
90
# File 'lib/rails_ai_bridge/introspector.rb', line 85

def resolve_introspector(name)
  introspector_class = config.additional_introspectors[name] || BUILTIN_INTROSPECTORS[name]
  raise ConfigurationError, "Unknown introspector: #{name}" unless introspector_class

  introspector_class.new(app)
end

#selected_introspectors(only) ⇒ Object



78
79
80
81
82
83
# File 'lib/rails_ai_bridge/introspector.rb', line 78

def selected_introspectors(only)
  names = Array(only).compact
  return config.effective_introspectors if names.empty?

  names
end