Class: Miguel::Schema::Table

Inherits:
Object
  • Object
show all
Includes:
Output
Defined in:
lib/miguel/schema.rb

Overview

Class representing database table.

Defined Under Namespace

Classes: Context

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Output

#out_canonic_opts, #out_columns, #out_default, #out_default_opts, #out_name, #out_opts, #out_table_name, #out_type

Constructor Details

#initialize(schema, name) ⇒ Table

Create new table with given name belonging to given schema.



339
340
341
342
343
344
345
# File 'lib/miguel/schema.rb', line 339

def initialize( schema, name )
  @schema = schema
  @name = name
  @columns = {}
  @indexes = []
  @foreign_keys = []
end

Instance Attribute Details

#foreign_keysObject (readonly)

List of table indices and foreign keys.



336
337
338
# File 'lib/miguel/schema.rb', line 336

def foreign_keys
  @foreign_keys
end

#indexesObject (readonly)

List of table indices and foreign keys.



336
337
338
# File 'lib/miguel/schema.rb', line 336

def indexes
  @indexes
end

#nameObject (readonly)

Name of the table.



333
334
335
# File 'lib/miguel/schema.rb', line 333

def name
  @name
end

#schemaObject (readonly)

Schema to which this table belongs.



330
331
332
# File 'lib/miguel/schema.rb', line 330

def schema
  @schema
end

Instance Method Details

#add_column(type, name, *args) ⇒ Object

Add column definition.



363
364
365
366
# 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) ⇒ Object

Add definition of column, index or foreign key.



380
381
382
383
384
385
386
387
388
389
390
# 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) ⇒ Object

Add foreign key definition.



374
375
376
377
# 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) ⇒ Object

Add index definition.



369
370
371
# File 'lib/miguel/schema.rb', line 369

def add_index( columns, *args )
  @indexes << Index.new( columns, *args )
end

#column_namesObject

Get names of all table columns.



353
354
355
# File 'lib/miguel/schema.rb', line 353

def column_names
  @columns.keys
end

#columnsObject

Get all columns.



348
349
350
# File 'lib/miguel/schema.rb', line 348

def columns
  @columns.values
end

#define(&block) ⇒ Object

Define table using the provided block.



393
394
395
396
397
# 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) ⇒ Object

Dump table definition to given output.



400
401
402
403
404
405
406
407
408
409
410
411
412
# 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) ⇒ Object

Get given named columns.



358
359
360
# File 'lib/miguel/schema.rb', line 358

def named_columns( names )
  @columns.values_at( *names )
end