Class: Miguel::Schema::ForeignKey

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

Overview

Class representing foreign key constraint.

Constant Summary collapse

IGNORED_OPTS =

Options we ignore when comparing. These are usually tied to the underlying column, not constraint.

[ :null, :unsigned, :type, :default ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Output

#out_canonic_opts, #out_columns, #out_default, #out_default_opts, #out_name, #out_opts, #out_table_name, #out_type

Constructor Details

#initialize(columns, table_name, opts = {}) ⇒ ForeignKey

Create new foreign key for given columns referring to given table.



249
250
251
252
253
254
255
256
# File 'lib/miguel/schema.rb', line 249

def initialize( columns, table_name, opts = {} )
  @columns = [ *columns ]
  @table_name = table_name
  @opts = opts
  if key = opts[ :key ]
    opts[ :key ] = [ *key ]
  end
end

Instance Attribute Details

#columnsObject (readonly)

Key's column(s), the target table name and options.



246
247
248
# File 'lib/miguel/schema.rb', line 246

def columns
  @columns
end

#optsObject (readonly)

Key's column(s), the target table name and options.



246
247
248
# File 'lib/miguel/schema.rb', line 246

def opts
  @opts
end

#table_nameObject (readonly)

Key's column(s), the target table name and options.



246
247
248
# File 'lib/miguel/schema.rb', line 246

def table_name
  @table_name
end

Instance Method Details

#==(other) ⇒ Object

Compare one foreign key with another one.



270
271
272
273
274
275
# File 'lib/miguel/schema.rb', line 270

def == other
  other.is_a?( ForeignKey ) &&
  columns == other.columns &&
  table_name == other.table_name &&
  canonic_opts == other.canonic_opts
end

#canonic_optsObject

Get the foreign key options, in a canonic way.



263
264
265
266
267
# File 'lib/miguel/schema.rb', line 263

def canonic_opts
  o = { :on_update => :no_action, :on_delete => :no_action }
  o.merge!( opts )
  o.delete_if{ |key, value| IGNORED_OPTS.include? key }
end

#dump(out) ⇒ Object

Dump foreign key definition.



278
279
280
# File 'lib/miguel/schema.rb', line 278

def dump( out )
  out << "foreign_key #{out_columns}, #{out_table_name}#{out_opts}"
end