Class: FlyingTable::TableMaker

Inherits:
Object
  • Object
show all
Defined in:
lib/flying_table/table_maker.rb

Class Method Summary collapse

Instance Method Summary collapse

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_tablesObject



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_tablesObject



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

#setupObject



12
# File 'lib/flying_table/table_maker.rb', line 12

def setup()                   @migration.suppress_messages{create_tables}                 end

#teardownObject



13
# File 'lib/flying_table/table_maker.rb', line 13

def teardown()                @migration.suppress_messages{drop_tables}                   end