Class: MockedFixtures::SchemaParser

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

Class Method Summary collapse

Class Method Details

.load_schemaObject

Parses schema.rb file and pulls out all the columns names and types into an array, with each row being an array of the column name and type respectively.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mocked_fixtures/schema_parser.rb', line 8

def self.load_schema
  schema = {}
  table_name = ""
  open(schema_path, 'r').each do |line|
    if /create_table "(\w+)".*? do \|t\|/ =~ line
      table_name = $1
      schema[table_name] = {:columns => [], :primary_key => nil}
      schema[table_name][:columns] 
      if line =~ /:primary_key => "(.*?)"/
        schema[table_name][:columns] << [$1, 'integer']
        schema[table_name][:primary_key] = $1
      elsif line !~ /:id => false/
        schema[table_name][:columns] << ['id', 'integer']
        schema[table_name][:primary_key] = 'id'
      end
    elsif /t\.(\w+)\s+"(\w+)"/ =~ line
      schema[table_name][:columns] << [$2, $1]
    end
  end
  schema
end