Module: AsktiveRecord::SchemaLoader

Included in:
Model::ClassMethods, AsktiveRecord::Service::ClassMethods
Defined in:
lib/asktive_record/schema_loader.rb

Overview

SchemaLoader provides shared schema loading functionality used by both Model::ClassMethods and Service::ClassMethods. It handles reading schema files, falling back to structure.sql, and providing clear error messages.

Instance Method Summary collapse

Instance Method Details

#ensure_schema_is_not_empty!(schema_content) ⇒ Object

Validates that the schema content is not empty.

Parameters:

  • schema_content (String)

    the schema content to validate

Raises:



28
29
30
31
32
33
# File 'lib/asktive_record/schema_loader.rb', line 28

def ensure_schema_is_not_empty!(schema_content)
  return unless schema_content.to_s.strip.empty?

  raise ConfigurationError,
        "Schema content is empty. Cannot proceed without database schema context."
end

#load_schema_contentString

Loads the database schema content from the configured path. Falls back to db/structure.sql if the primary schema file is not found.

Returns:

  • (String)

    the schema content

Raises:



15
16
17
18
19
20
21
22
# File 'lib/asktive_record/schema_loader.rb', line 15

def load_schema_content
  path = AsktiveRecord.configuration.db_schema_path
  return File.read(path) if File.exist?(path)

  attempt_schema_fallback(path)
rescue SystemCallError => e
  raise ConfigurationError, "Error reading schema file at #{path}: #{e.message}"
end

#validate_llm_api_key!Object

Validates that the LLM API key is configured.

Raises:



38
39
40
41
42
# File 'lib/asktive_record/schema_loader.rb', line 38

def validate_llm_api_key!
  return if AsktiveRecord.configuration&.llm_api_key

  raise ConfigurationError, "LLM API key is not configured for AsktiveRecord."
end