Class: Convergence::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/convergence/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, options = {}) ⇒ Table

Returns a new instance of Table.



30
31
32
33
34
35
36
# File 'lib/convergence/table.rb', line 30

def initialize(table_name, options = {})
  @table_name = table_name
  @table_options = options
  @columns = {}
  @indexes = {}
  @foreign_keys = {}
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



2
3
4
# File 'lib/convergence/table.rb', line 2

def columns
  @columns
end

#foreign_keysObject

Returns the value of attribute foreign_keys.



2
3
4
# File 'lib/convergence/table.rb', line 2

def foreign_keys
  @foreign_keys
end

#indexesObject

Returns the value of attribute indexes.



2
3
4
# File 'lib/convergence/table.rb', line 2

def indexes
  @indexes
end

#table_nameObject

Returns the value of attribute table_name.



2
3
4
# File 'lib/convergence/table.rb', line 2

def table_name
  @table_name
end

#table_optionsObject

Returns the value of attribute table_options.



2
3
4
# File 'lib/convergence/table.rb', line 2

def table_options
  @table_options
end

Instance Method Details

#foreign_key(key_columns, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/convergence/table.rb', line 16

def foreign_key(key_columns, options = {})
  if options[:reference].nil? || options[:reference_column].nil?
    fail ArgumentError.new("#{@table_name} - #{key_columns}: require reference/reference_column parameters")
  end
  key_name = options[:name]
  key_name = "#{table_name}_#{[key_columns].flatten.join('_')}_fk" if key_name.nil?
  @foreign_keys[key_name] = Convergence::ForeignKey.new(
    key_name,
    key_columns,
    options[:reference],
    [options[:reference_column]].flatten,
    options.reject { |k, _v| k == :reference || k == :reference_column })
end

#index(index_columns, options = {}) ⇒ Object



10
11
12
13
14
# File 'lib/convergence/table.rb', line 10

def index(index_columns, options = {})
  index_name = options[:name]
  index_name = "index_#{table_name}_on_#{[index_columns].flatten.join('_')}" if index_name.nil?
  @indexes[index_name] = Convergence::Index.new(index_name, index_columns, options)
end