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.



258
259
260
261
262
263
264
265
# File 'lib/miguel/schema.rb', line 258

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.



255
256
257
# File 'lib/miguel/schema.rb', line 255

def columns
  @columns
end

#optsObject (readonly)

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



255
256
257
# File 'lib/miguel/schema.rb', line 255

def opts
  @opts
end

#table_nameObject (readonly)

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



255
256
257
# File 'lib/miguel/schema.rb', line 255

def table_name
  @table_name
end

Instance Method Details

#==(other) ⇒ Object

Compare one foreign key with another one.



279
280
281
282
283
284
# File 'lib/miguel/schema.rb', line 279

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.



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

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.



287
288
289
# File 'lib/miguel/schema.rb', line 287

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