Class: RailsAiContext::Introspectors::SchemaReader
- Inherits:
-
Object
- Object
- RailsAiContext::Introspectors::SchemaReader
- Defined in:
- lib/rails_ai_context/introspectors/schema_reader.rb
Overview
AST-backed view of a schema.rb dump: which tables it declares, their columns, defaults and indexes, plus the foreign keys, enum types and check constraints declared alongside them.
The listener emits a flat, ordered event list, so columns, in-table indexes and in-table constraints are attached to the most recent create_table. Top-level add_index and add_check_constraint name their own table.
Callers keep their own output shapes: this reads the dump, it does not decide how a table is reported.
Instance Method Summary collapse
- #any_column?(name) ⇒ Boolean
-
#check_constraints ⇒ Array<Hash>
{ table:, expression: } per declared constraint.
- #column?(table, name) ⇒ Boolean
-
#defaults_for(table) ⇒ Object
Declared defaults for one table, as source text.
-
#enums ⇒ Array<Hash>
{ name:, values: } per declared enum type.
-
#foreign_keys ⇒ Array<Hash>
{ from:, to: } per declared foreign key.
-
#initialize(path) ⇒ SchemaReader
constructor
A new instance of SchemaReader.
-
#tables ⇒ Hash
Table name => { options:, columns: [...], indexes: [...] }.
Constructor Details
#initialize(path) ⇒ SchemaReader
Returns a new instance of SchemaReader.
17 18 19 |
# File 'lib/rails_ai_context/introspectors/schema_reader.rb', line 17 def initialize(path) @path = path end |
Instance Method Details
#any_column?(name) ⇒ Boolean
53 54 55 |
# File 'lib/rails_ai_context/introspectors/schema_reader.rb', line 53 def any_column?(name) tables.any? { |table, _| column?(table, name) } end |
#check_constraints ⇒ Array<Hash>
Returns { table:, expression: } per declared constraint.
37 38 39 |
# File 'lib/rails_ai_context/introspectors/schema_reader.rb', line 37 def check_constraints parse[:check_constraints] end |
#column?(table, name) ⇒ Boolean
49 50 51 |
# File 'lib/rails_ai_context/introspectors/schema_reader.rb', line 49 def column?(table, name) columns_for(table).any? { |c| c[:name] == name } end |
#defaults_for(table) ⇒ Object
Declared defaults for one table, as source text. Callers report these verbatim when the live database returns no default.
43 44 45 46 47 |
# File 'lib/rails_ai_context/introspectors/schema_reader.rb', line 43 def defaults_for(table) columns_for(table).each_with_object({}) do |column, defaults| defaults[column[:name]] = column[:default] unless column[:default].nil? end end |
#enums ⇒ Array<Hash>
Returns { name:, values: } per declared enum type.
32 33 34 |
# File 'lib/rails_ai_context/introspectors/schema_reader.rb', line 32 def enums parse[:enums] end |
#foreign_keys ⇒ Array<Hash>
Returns { from:, to: } per declared foreign key.
27 28 29 |
# File 'lib/rails_ai_context/introspectors/schema_reader.rb', line 27 def foreign_keys parse[:foreign_keys] end |
#tables ⇒ Hash
Returns table name => { options:, columns: [...], indexes: [...] }.
22 23 24 |
# File 'lib/rails_ai_context/introspectors/schema_reader.rb', line 22 def tables parse[:tables] end |