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 =

Callback name prefixes omitted from generated model context to reduce framework noise.

%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.

Also prepares a path resolver so source-based metadata honors custom Rails path configuration.

Parameters:

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


18
19
20
21
22
# File 'lib/rails_ai_bridge/introspectors/model_introspector.rb', line 18

def initialize(app)
  @app    = app
  @config = RailsAiBridge.configuration
  @path_resolver = PathResolver.new(app)
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 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.



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

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