class Miguel::Schema::Table

Class representing database table.

Attributes

foreign_keys[R]

List of table indices and foreign keys.

indexes[R]

List of table indices and foreign keys.

name[R]

Name of the table.

schema[R]

Schema to which this table belongs.

Public Class Methods

new( schema, name ) click to toggle source

Create new table with given name belonging to given schema.

# File lib/miguel/schema.rb, line 339
def initialize( schema, name )
  @schema = schema
  @name = name
  @columns = {}
  @indexes = []
  @foreign_keys = []
end

Public Instance Methods

add_column( type, name, *args ) click to toggle source

Add column definition.

# File lib/miguel/schema.rb, line 363
def add_column( type, name, *args )
  fail( ArgumentError, "column #{name} in table #{self.name} is already defined" ) if @columns[ name ]
  @columns[ name ] = Column.new( type, name, *args )
end
add_definition( name, *args ) click to toggle source

Add definition of column, index or foreign key.

# File lib/miguel/schema.rb, line 380
def add_definition( name, *args )
  name, *args = schema.apply_defaults( self.name, name, *args )
  case name
  when :index
    add_index( *args )
  when :foreign_key
    add_foreign_key( *args )
  else
    add_column( name, *args )
  end
end
add_foreign_key( columns, table_name, *args ) click to toggle source

Add foreign key definition.

# File lib/miguel/schema.rb, line 374
def add_foreign_key( columns, table_name, *args )
  add_column( :integer, columns, *args ) unless columns.is_a? Array
  @foreign_keys << ForeignKey.new( columns, table_name, *args )
end
add_index( columns, *args ) click to toggle source

Add index definition.

# File lib/miguel/schema.rb, line 369
def add_index( columns, *args )
  @indexes << Index.new( columns, *args )
end
column_names() click to toggle source

Get names of all table columns.

# File lib/miguel/schema.rb, line 353
def column_names
  @columns.keys
end
columns() click to toggle source

Get all columns.

# File lib/miguel/schema.rb, line 348
def columns
  @columns.values
end
define( &block ) click to toggle source

Define table using the provided block.

# File lib/miguel/schema.rb, line 393
def define( &block )
  fail( ArgumentError, "missing table definition block" ) unless block
  Context.new( self ).instance_eval( &block )
  self
end
dump( out = Dumper.new ) click to toggle source

Dump table definition to given output.

# File lib/miguel/schema.rb, line 400
def dump( out = Dumper.new )
  out.dump "table #{out_name}" do
    for column in columns
      column.dump( out )
    end
    for index in indexes
      index.dump( out )
    end
    for foreign_key in foreign_keys
      foreign_key.dump( out )
    end
  end
end
named_columns( names ) click to toggle source

Get given named columns.

# File lib/miguel/schema.rb, line 358
def named_columns( names )
  @columns.values_at( *names )
end