Sha256: ed7f2d124e8b8e66180b7b42ce03b9cf1d9cf7fa5777477d19ed39f464f31152

Contents?: true

Size: 1020 Bytes

Versions: 3

Compression:

Stored size: 1020 Bytes

Contents

module MockedFixtures
  class SchemaParser
    cattr_accessor :schema_path
    
    # 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.
    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mocked_fixtures-0.1.0 lib/mocked_fixtures/schema_parser.rb
mocked_fixtures-0.2.0 lib/mocked_fixtures/schema_parser.rb
mocked_fixtures-0.3.0 lib/mocked_fixtures/schema_parser.rb