Class: FlyingTable::TableMaker
- Inherits:
-
Object
- Object
- FlyingTable::TableMaker
- Defined in:
- lib/flying_table/table_maker.rb
Class Method Summary collapse
Instance Method Summary collapse
- #create_columns(t, columns) ⇒ Object
- #create_table(name, columns) ⇒ Object
- #create_tables ⇒ Object
- #drop_table(name) ⇒ Object
- #drop_tables ⇒ Object
-
#initialize(tables) ⇒ TableMaker
constructor
A new instance of TableMaker.
- #seperate_table(table) ⇒ Object
- #setup ⇒ Object
- #teardown ⇒ Object
Constructor Details
#initialize(tables) ⇒ TableMaker
Returns a new instance of TableMaker.
4 5 6 7 |
# File 'lib/flying_table/table_maker.rb', line 4 def initialize(tables) @migration = ActiveRecord::Migration @tables = tables end |
Class Method Details
.create(tables) ⇒ Object
2 |
# File 'lib/flying_table/table_maker.rb', line 2 def self.create(tables) new(tables).setup end |
.destroy(tables) ⇒ Object
3 |
# File 'lib/flying_table/table_maker.rb', line 3 def self.destroy(tables) new(tables).teardown end |
Instance Method Details
#create_columns(t, columns) ⇒ Object
8 |
# File 'lib/flying_table/table_maker.rb', line 8 def create_columns(t,columns) columns.each{|name,type| t.send(type,name) } end |
#create_table(name, columns) ⇒ Object
15 16 17 18 |
# File 'lib/flying_table/table_maker.rb', line 15 def create_table(name,columns) Object.const_set(name.classify, Class.new(ActiveRecord::Base)) @migration.create_table(name.pluralize){ |t| create_columns(t,columns)} end |
#create_tables ⇒ Object
9 |
# File 'lib/flying_table/table_maker.rb', line 9 def create_tables() @tables.each{|table| seperate_table(table)} end |
#drop_table(name) ⇒ Object
19 20 21 22 |
# File 'lib/flying_table/table_maker.rb', line 19 def drop_table(name) Object.send(:remove_const, name.classify) @migration.drop_table(name.pluralize) end |
#drop_tables ⇒ Object
10 |
# File 'lib/flying_table/table_maker.rb', line 10 def drop_tables() @tables.each{|name| drop_table(name.to_s)} end |
#seperate_table(table) ⇒ Object
11 |
# File 'lib/flying_table/table_maker.rb', line 11 def seperate_table(table) table.each{|name,columns| create_table(name.to_s, columns)} end |
#setup ⇒ Object
12 |
# File 'lib/flying_table/table_maker.rb', line 12 def setup() @migration.{create_tables} end |
#teardown ⇒ Object
13 |
# File 'lib/flying_table/table_maker.rb', line 13 def teardown() @migration.{drop_tables} end |