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.



47
48
49
50
51
52
53
# File 'lib/convergence/table.rb', line 47

def initialize(table_name, options = {})
  @table_name = table_name
  @table_options = options.reject { |k| k == :id }
  @columns = {}
  @indexes = {}
  @foreign_keys = {}
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



6
7
8
# File 'lib/convergence/table.rb', line 6

def columns
  @columns
end

#foreign_keysObject

Returns the value of attribute foreign_keys.



6
7
8
# File 'lib/convergence/table.rb', line 6

def foreign_keys
  @foreign_keys
end

#indexesObject

Returns the value of attribute indexes.



6
7
8
# File 'lib/convergence/table.rb', line 6

def indexes
  @indexes
end

#table_nameObject

Returns the value of attribute table_name.



6
7
8
# File 'lib/convergence/table.rb', line 6

def table_name
  @table_name
end

#table_optionsObject

Returns the value of attribute table_options.



6
7
8
# File 'lib/convergence/table.rb', line 6

def table_options
  @table_options
end

Instance Method Details

#boolean(column_name, options = {}) ⇒ Object



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

def boolean(column_name, options = {})
  case options[:default]
  when TrueClass
    options[:default] = 1
  when FalseClass
    options[:default] = 0
  end
  tinyint(column_name, options.merge(limit: 1))
end

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



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/convergence/table.rb', line 33

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



27
28
29
30
31
# File 'lib/convergence/table.rb', line 27

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