Class: TempTable::Create

Inherits:
Object
  • Object
show all
Defined in:
lib/temp_table/create.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, columns) ⇒ Create

Returns a new instance of Create.



7
8
9
10
11
# File 'lib/temp_table/create.rb', line 7

def initialize(table_name, columns)
  super()
  @table_name = table_name
  @columns = columns
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



5
6
7
# File 'lib/temp_table/create.rb', line 5

def columns
  @columns
end

#table_nameObject

Returns the value of attribute table_name.



5
6
7
# File 'lib/temp_table/create.rb', line 5

def table_name
  @table_name
end

Instance Method Details

#performObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/temp_table/create.rb', line 13

def perform
  column_definitions = columns.map { |name, type| "#{name} #{type}" }.join(", ")
  if column_definitions.empty?
    ActiveRecord::Base.connection.execute <<-SQL.squish
      CREATE TEMPORARY IF NOT EXISTS #{@table_name} (
        id SERIAL PRIMARY KEY
      );
    SQL
  else
    ActiveRecord::Base.connection.execute <<-SQL.squish
      CREATE TEMPORARY TABLE #{@table_name} (
        id SERIAL PRIMARY KEY,
        #{column_definitions}
      );
    SQL
  end
end