Class: MotionRecord::Schema::TableDefinition
- Inherits:
-
Object
- Object
- MotionRecord::Schema::TableDefinition
- Defined in:
- lib/motion_record/schema/table_definition.rb
Instance Method Summary collapse
- #execute ⇒ Object
- #float(column_name, options = {}) ⇒ Object
- #index(columns, options = {}) ⇒ Object
-
#initialize(name, options = {}) ⇒ TableDefinition
constructor
A new instance of TableDefinition.
- #integer(column_name, options = {}) ⇒ Object
- #text(column_name, options = {}) ⇒ Object
-
#timestamps ⇒ Object
Add :created_at and :updated_at columns to the table.
Constructor Details
#initialize(name, options = {}) ⇒ TableDefinition
Returns a new instance of TableDefinition.
4 5 6 7 8 9 10 11 12 |
# File 'lib/motion_record/schema/table_definition.rb', line 4 def initialize(name, ={}) @name = name @columns = [] @index_definitions = [] unless .has_key?(:id) && ![:id] add_default_primary_column end end |
Instance Method Details
#execute ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/motion_record/schema/table_definition.rb', line 14 def execute # Create table column_sql = @columns.map(&:to_sql_definition).join(", ") MotionRecord::Base.connection.execute "CREATE TABLE #{@name} (#{column_sql})" # Create table's indexes @index_definitions.each(&:execute) end |
#float(column_name, options = {}) ⇒ Object
31 32 33 |
# File 'lib/motion_record/schema/table_definition.rb', line 31 def float(column_name, ={}) @columns << ColumnDefinition.new(:float, column_name, ) end |
#index(columns, options = {}) ⇒ Object
35 36 37 |
# File 'lib/motion_record/schema/table_definition.rb', line 35 def index(columns, ={}) @index_definitions << IndexDefinition.new(@name, columns, ) end |
#integer(column_name, options = {}) ⇒ Object
27 28 29 |
# File 'lib/motion_record/schema/table_definition.rb', line 27 def integer(column_name, ={}) @columns << ColumnDefinition.new(:integer, column_name, ) end |
#text(column_name, options = {}) ⇒ Object
23 24 25 |
# File 'lib/motion_record/schema/table_definition.rb', line 23 def text(column_name, ={}) @columns << ColumnDefinition.new(:text, column_name, ) end |
#timestamps ⇒ Object
Add :created_at and :updated_at columns to the table
40 41 42 43 |
# File 'lib/motion_record/schema/table_definition.rb', line 40 def self.integer(:created_at) self.integer(:updated_at) end |