Class: Miguel::Schema::Table
- Inherits:
-
Object
- Object
- Miguel::Schema::Table
- Includes:
- Output
- Defined in:
- lib/miguel/schema.rb
Overview
Class representing database table.
Defined Under Namespace
Classes: Context
Instance Attribute Summary collapse
-
#foreign_keys ⇒ Object
readonly
List of table indices and foreign keys.
-
#indexes ⇒ Object
readonly
List of table indices and foreign keys.
-
#name ⇒ Object
readonly
Name of the table.
-
#schema ⇒ Object
readonly
Schema to which this table belongs.
Instance Method Summary collapse
-
#add_column(type, name, *args) ⇒ Object
Add column definition.
-
#add_definition(name, *args) ⇒ Object
Add definition of column, index or foreign key.
-
#add_foreign_key(columns, table_name, opts = {}) ⇒ Object
Add foreign key definition.
-
#add_index(columns, *args) ⇒ Object
Add index definition.
-
#column_names ⇒ Object
Get names of all table columns.
-
#columns ⇒ Object
Get all columns.
-
#define(&block) ⇒ Object
Define table using the provided block.
-
#dump(out = Dumper.new) ⇒ Object
Dump table definition to given output.
-
#initialize(schema, name) ⇒ Table
constructor
Create new table with given name belonging to given schema.
-
#named_columns(names) ⇒ Object
Get given named columns.
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.
348 349 350 351 352 353 354 |
# File 'lib/miguel/schema.rb', line 348 def initialize( schema, name ) @schema = schema @name = name @columns = {} @indexes = [] @foreign_keys = [] end |
Instance Attribute Details
#foreign_keys ⇒ Object (readonly)
List of table indices and foreign keys.
345 346 347 |
# File 'lib/miguel/schema.rb', line 345 def foreign_keys @foreign_keys end |
#indexes ⇒ Object (readonly)
List of table indices and foreign keys.
345 346 347 |
# File 'lib/miguel/schema.rb', line 345 def indexes @indexes end |
#name ⇒ Object (readonly)
Name of the table.
342 343 344 |
# File 'lib/miguel/schema.rb', line 342 def name @name end |
#schema ⇒ Object (readonly)
Schema to which this table belongs.
339 340 341 |
# File 'lib/miguel/schema.rb', line 339 def schema @schema end |
Instance Method Details
#add_column(type, name, *args) ⇒ Object
Add column definition.
372 373 374 375 |
# File 'lib/miguel/schema.rb', line 372 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.
389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/miguel/schema.rb', line 389 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, opts = {}) ⇒ Object
Add foreign key definition.
383 384 385 386 |
# File 'lib/miguel/schema.rb', line 383 def add_foreign_key( columns, table_name, opts = {} ) add_column( opts[:type] || :integer, columns, opts ) unless columns.is_a? Array @foreign_keys << ForeignKey.new( columns, table_name, opts ) end |
#add_index(columns, *args) ⇒ Object
Add index definition.
378 379 380 |
# File 'lib/miguel/schema.rb', line 378 def add_index( columns, *args ) @indexes << Index.new( self, columns, *args ) end |
#column_names ⇒ Object
Get names of all table columns.
362 363 364 |
# File 'lib/miguel/schema.rb', line 362 def column_names @columns.keys end |
#columns ⇒ Object
Get all columns.
357 358 359 |
# File 'lib/miguel/schema.rb', line 357 def columns @columns.values end |
#define(&block) ⇒ Object
Define table using the provided block.
402 403 404 405 406 |
# File 'lib/miguel/schema.rb', line 402 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.
409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/miguel/schema.rb', line 409 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.
367 368 369 |
# File 'lib/miguel/schema.rb', line 367 def named_columns( names ) @columns.values_at( *names ) end |