Module: RubynCode::Context::SchemaFilter
- Defined in:
- lib/rubyn_code/context/schema_filter.rb
Overview
Extracts only the relevant table definitions from db/schema.rb based on which models are currently in context. Loading the full schema for a large Rails app can be 5-10K tokens; filtering to relevant tables typically reduces this to 200-500 tokens.
Constant Summary collapse
- TABLE_PATTERN =
/create_table\s+"([^"]+)"/- END_PATTERN =
/\A\s+end\s*\z/
Class Method Summary collapse
-
.filter(schema_path, table_names:) ⇒ String
Returns schema definitions for only the specified table names.
-
.filter_for_models(schema_path, model_names:) ⇒ Object
Convenience: filter schema by model names instead of table names.
-
.tableize(model_names) ⇒ Array<String>
Derives table names from model class names using Rails conventions.
Class Method Details
.filter(schema_path, table_names:) ⇒ String
Returns schema definitions for only the specified table names.
19 20 21 22 23 24 25 |
# File 'lib/rubyn_code/context/schema_filter.rb', line 19 def filter(schema_path, table_names:) return '' if table_names.empty? return '' unless File.exist?(schema_path) lines = File.readlines(schema_path) extract_tables(lines, table_names.to_set(&:to_s)) end |
.filter_for_models(schema_path, model_names:) ⇒ Object
Convenience: filter schema by model names instead of table names.
36 37 38 39 |
# File 'lib/rubyn_code/context/schema_filter.rb', line 36 def filter_for_models(schema_path, model_names:) tables = tableize(model_names) filter(schema_path, table_names: tables) end |
.tableize(model_names) ⇒ Array<String>
Derives table names from model class names using Rails conventions.
31 32 33 |
# File 'lib/rubyn_code/context/schema_filter.rb', line 31 def tableize(model_names) model_names.map { |name| "#{name.gsub(/([a-z])([A-Z])/, '\1_\2').downcase}s" } end |