Class: RailsAiBridge::Introspectors::ModelIntrospector

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

Overview

Extracts ActiveRecord model metadata: associations, validations, scopes, enums, callbacks, and class-level configuration.

Constant Summary collapse

EXCLUDED_CALLBACKS =
%w[autosave_associated_records_for].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ModelIntrospector

Initializes the ModelIntrospector with the host Rails application and loads the library configuration.

Parameters:

  • app (Object)
    • The Rails application instance to introspect.


15
16
17
18
# File 'lib/rails_ai_bridge/introspectors/model_introspector.rb', line 15

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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



8
9
10
# File 'lib/rails_ai_bridge/introspectors/model_introspector.rb', line 8

def app
  @app
end

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/rails_ai_bridge/introspectors/model_introspector.rb', line 8

def config
  @config
end

Instance Method Details

#callHash<String, Hash>

Builds a hash of discovered ActiveRecord model metadata keyed by model name. For each model, performs semantic classification and collects table name, associations, validations, scopes, enums, callbacks, concerns, public methods, and source-based macro signals; if extraction for a model raises, records { error: <message> } for that model.

Builds a metadata map for all discovered ActiveRecord models. For each model, the map contains the extracted metadata hash; if extraction fails for a model, its value will be a hash with an :error key and the error message.

Returns:

  • (Hash<String, Hash>)

    Mapping from model name to its metadata hash or { error: String } on failure.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rails_ai_bridge/introspectors/model_introspector.rb', line 32

def call
  eager_load_models!
  models = discover_models
  through_names = ModelSemanticClassifier.through_join_model_names
  classifier = ModelSemanticClassifier.new(
    core_model_names: config.core_models,
    through_model_names: through_names
  )

  models.each_with_object({}) do |model, hash|
    hash[model.name] = extract_model_details(model, classifier)
  rescue StandardError => error
    hash[model.name] = { error: error.message }
  end
end