Module: RareMap::SchemaParser

Defined in:
lib/rspec-doc/model_builder.rb

Overview

RareMap::SchemaParser parses schema.rb into Table.

Instance Method Summary collapse

Instance Method Details

#parse_schema(schema) ⇒ Array

Parses schema.rb into an Array of Table.

Returns:

  • (Array)

    an Array of Table



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rspec-doc/model_builder.rb', line 16

def parse_schema(schema)
  tables = []
  
  schema.split(/\n/).each do |line|
    case line.strip!
    when /^create_table/
      name = line.match(/create_table\s+['"]([^'"]+)['"]/)[1]
      id = line.match(/(:id\s*=>|id:)\s*false/) ? false : true
      pk = line.match(/(:primary_key\s*=>|primary_key:)\s*['"](.+)['"]/)
      primary_key = pk[2] if pk
      tables << RareMap::Table.new(name, :id => id, :primary_key => primary_key)
    when /^t\.index/
      unique_column_math = line.match(/t\.index\s+.*\[\s*['"]([^'"]+)['"]\s*\].*(:unique\s*=>|unique:)\s*true/)
      next if !unique_column_math || unique_column_math.size < 2
      unique_column = unique_column_math[1] 
      column = tables.last.columns.find { |col| col.name == unique_column }
      column.unique = true
    when /^t\./
      name = line.match(/t\.\w+\s+['"]([^'"]+)['"]/)[1]
      type = line.match(/t\.(\w+)\s+/)[1]
      tables.last.columns << RareMap::Column.new(name, type)
    end
  end
  
  tables
end