Skip to content

Instantly share code, notes, and snippets.

@blakesmith
Created June 15, 2010 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blakesmith/439588 to your computer and use it in GitHub Desktop.
Save blakesmith/439588 to your computer and use it in GitHub Desktop.
class Migration
class << self
def create_table(name, &block)
table = Table.new name
table.evaluate &block
table.create
end
def drop_table(name)
table = Table.new name
table.drop
end
end
end
class Table
def initialize(name)
@name = name
@columns = []
end
def evaluate(&block)
instance_eval &block
end
def string(*columns)
@columns << columns
end
def create
#DB logic to create table here
"Table #{@name} created with columns #{@columns.join(', ')}"
end
def drop
#DB logic to remove table here
"Table #{@name} dropped from database"
end
end
class MyMigration < Migration
def self.up
create_table 'users' do
string 'username', 'password'
end
end
def self.down
drop_table 'users'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment