Class: Ruby::Text2sql::SchemaParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/text2sql/schema_parser.rb

Constant Summary collapse

SCHEMA_PATH =
"db/schema.rb"

Instance Method Summary collapse

Constructor Details

#initializeSchemaParser

Returns a new instance of SchemaParser.



8
9
10
# File 'lib/ruby/text2sql/schema_parser.rb', line 8

def initialize
  @schema = File.read(SCHEMA_PATH)
end

Instance Method Details

#parseObject

Parses the schema to extract table and column information, including column types



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby/text2sql/schema_parser.rb', line 13

def parse
  tables = []
  @schema.each_line do |line|
    # Extract table names
    if line =~ /create_table "(.*?)"/
      tables << { table: Regexp.last_match(1), columns: [] }

    # Extract column types and names
    elsif line =~ /t\.(\w+) "(.*?)"/
      column_type = Regexp.last_match(1)
      column_name = Regexp.last_match(2)
      tables.last[:columns] << { name: column_name, type: column_type } if tables.any?
    end
  end
  tables
end