Class: WithModel::Table
- Inherits:
-
Object
- Object
- WithModel::Table
- 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
-
#configure(klass) ⇒ Object
Points the model at this table.
-
#create ⇒ Object
Creates the table with the initialized options.
- #destroy ⇒ Object
-
#initialize(name, options = {}, connection: ActiveRecord::Base.connection, &block) ⇒ Table
constructor
A new instance of Table.
-
#teardown(_klass) ⇒ Object
Removes everything this table holds by dropping it.
Constructor Details
#initialize(name, options = {}, connection: ActiveRecord::Base.connection, &block) ⇒ Table
Returns a new instance of Table.
14 15 16 17 18 19 |
# File 'lib/with_model/table.rb', line 14 def initialize(name, = {}, connection: ActiveRecord::Base.connection, &block) @name = name.freeze @options = .freeze @block = block @connection = connection end |
Instance Method Details
#configure(klass) ⇒ Object
Points the model at this table.
29 30 31 |
# File 'lib/with_model/table.rb', line 29 def configure(klass) klass.table_name = @name end |
#create ⇒ Object
Creates the table with the initialized options. Drops the table if it already exists.
23 24 25 26 |
# File 'lib/with_model/table.rb', line 23 def create connection.drop_table(@name) if exists? connection.create_table(@name, **@options, &@block) end |
#destroy ⇒ Object
40 41 42 |
# File 'lib/with_model/table.rb', line 40 def destroy connection.drop_table(@name) end |
#teardown(_klass) ⇒ Object
Removes everything this table holds by dropping it. The model is not needed, but is accepted so that NullTable - which does need it - can stand in here.
36 37 38 |
# File 'lib/with_model/table.rb', line 36 def teardown(_klass) destroy end |