Class: ActiveRecord::ConnectionAdapters::EnsuredTable

Inherits:
Table
  • Object
show all
Defined in:
lib/ensured_schema/ensured_table.rb

Instance Method Summary collapse

Methods inherited from Table

#column_exists?

Instance Method Details

#build_column_definition(column_name, column_type, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ensured_schema/ensured_table.rb', line 38

def build_column_definition(column_name, column_type, options = {})
  column = ColumnDefinition.new(@base, column_name, column_type)
  if options[:limit]
    column.limit = options[:limit]
  elsif native[column_type.to_sym].is_a?(Hash)
    column.limit = native[column_type.to_sym][:limit]
  end
  column.precision = options[:precision]
  column.scale = options[:scale]
  column.default = options[:default]
  column.null = options[:null]
  column
end

#column(column_name, type, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/ensured_schema/ensured_table.rb', line 5

def column(column_name, type, options = {})
  if column_exists?(column_name)
    unless column_exists?(column_name, type, options)
      change(column_name, type, options)
    end
  else
    @base.add_column(@table_name, column_name, type, options)
  end
end

#define_columns(column_type, *args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/ensured_schema/ensured_table.rb', line 27

def define_columns(column_type, *args)
  options = args.extract_options!
  column_names = args

  column_names.each do |name|
    column_def = build_column_definition(name, column_type, options)
    def_options = column_def.members.inject({}){|h, k| h[k.to_sym] = column_def[k]; h;}
    column(name, column_type.to_sym, def_options)
  end
end

#remove(column_name) ⇒ Object



15
16
17
# File 'lib/ensured_schema/ensured_table.rb', line 15

def remove(column_name)
  super if column_exists?(column_name)
end