Class: WithModel::Table

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

Overview

In general, direct use of this class should be avoided. Instead use either the high-level API or low-level API.

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ Table

Returns a new instance of Table.

Parameters:

  • name (Symbol)

    The name of the table to create.

  • options (defaults to: {})

    Passed to ActiveRecord create_table.

  • block

    Passed to ActiveRecord create_table.

See Also:



13
14
15
16
17
# File 'lib/with_model/table.rb', line 13

def initialize(name, options = {}, &block)
  @name = name.freeze
  @options = options.freeze
  @block = block
end

Instance Method Details

#createObject

Creates the table with the initialized options. Drops the table if it already exists.



21
22
23
24
# File 'lib/with_model/table.rb', line 21

def create
  connection.drop_table(@name) if exists?
  connection.create_table(@name, **@options, &@block)
end

#destroyObject



26
27
28
# File 'lib/with_model/table.rb', line 26

def destroy
  connection.drop_table(@name)
end