Class: Miguel::Schema::Table::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/miguel/schema.rb

Overview

Helper class used to evaluate the +add_table+ block. Also implements the timestamping helper.

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ Context

Create new context for given table.



303
304
305
# File 'lib/miguel/schema.rb', line 303

def initialize( table )
  @table = table
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Send anything unrecognized as new definition to our table.



308
309
310
# File 'lib/miguel/schema.rb', line 308

def method_missing( name, *args )
  @table.add_definition( name, *args )
end

Instance Method Details

#timestampsObject

Create the default timestamp fields.



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/miguel/schema.rb', line 319

def timestamps
  opts = @table.schema.opts
  if opts[ :mysql_timestamps ]
    # Unfortunately, MySQL allows only either automatic create timestamp
    # (DEFAULT CURRENT_TIMESTAMP) or automatic update timestamp (DEFAULT
    # CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP), but not both - one
    # has to be updated manually anyway. So we choose to have the update timestamp
    # automatically updated, and let the create one to be set manually.
    # Also, Sequel doesn't currently honor :on_update for column definitions,
    # so we have to use default literal to make it work. Sigh.
    timestamp :create_time, :null => false, :default => ( opts[ :zero_timestamps ] == false ? DEFAULT_TIME : ZERO_TIME )
    timestamp :update_time, :null => false, :default => Sequel.lit( 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP' )
  else
    Time :create_time
    Time :update_time
  end
end