Class: DBLeftovers::ForeignKey

Inherits:
Object
  • Object
show all
Defined in:
lib/db_leftovers/foreign_key.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constraint_name, from_table, from_column, to_table, to_column, opts = {}) ⇒ ForeignKey

Returns a new instance of ForeignKey.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/db_leftovers/foreign_key.rb', line 6

def initialize(constraint_name, from_table, from_column, to_table, to_column, opts={})
  opts = {
    :on_delete => nil
  }.merge(opts)
  opts.keys.each do |k|
    raise "`:set_null => true` should now be `:on_delete => :set_null`" if k.to_s == 'set_null'
    raise "`:cascade => true` should now be `:on_delete => :cascade`"   if k.to_s == 'cascade'
    raise "Unknown option: #{k}" unless [:on_delete].include?(k)
  end
  raise "Unknown on_delete option: #{opts[:on_delete]}" unless [nil, :set_null, :cascade].include?(opts[:on_delete])
  @constraint_name = constraint_name.to_s
  @from_table = from_table.to_s
  @from_column = from_column.to_s
  @to_table = to_table.to_s
  @to_column = to_column.to_s

  @set_null = opts[:on_delete] == :set_null
  @cascade = opts[:on_delete] == :cascade

  raise "ON DELETE can't be both set_null and cascade" if @set_null and @cascade
end

Instance Attribute Details

#cascadeObject

Returns the value of attribute cascade.



4
5
6
# File 'lib/db_leftovers/foreign_key.rb', line 4

def cascade
  @cascade
end

#constraint_nameObject

Returns the value of attribute constraint_name.



4
5
6
# File 'lib/db_leftovers/foreign_key.rb', line 4

def constraint_name
  @constraint_name
end

#from_columnObject

Returns the value of attribute from_column.



4
5
6
# File 'lib/db_leftovers/foreign_key.rb', line 4

def from_column
  @from_column
end

#from_tableObject

Returns the value of attribute from_table.



4
5
6
# File 'lib/db_leftovers/foreign_key.rb', line 4

def from_table
  @from_table
end

#set_nullObject

Returns the value of attribute set_null.



4
5
6
# File 'lib/db_leftovers/foreign_key.rb', line 4

def set_null
  @set_null
end

#to_columnObject

Returns the value of attribute to_column.



4
5
6
# File 'lib/db_leftovers/foreign_key.rb', line 4

def to_column
  @to_column
end

#to_tableObject

Returns the value of attribute to_table.



4
5
6
# File 'lib/db_leftovers/foreign_key.rb', line 4

def to_table
  @to_table
end

Instance Method Details

#equals(other) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/db_leftovers/foreign_key.rb', line 28

def equals(other)
  other.constraint_name == constraint_name and
  other.from_table == from_table and
  other.from_column == from_column and
  other.to_table == to_table and
  other.to_column == to_column and
  other.set_null == set_null and
  other.cascade == cascade
end

#to_sObject



38
39
40
# File 'lib/db_leftovers/foreign_key.rb', line 38

def to_s
  "<#{@constraint_name}: from #{@from_table}.#{@from_column} to #{@to_table}.#{@to_column} #{if @set_null; "ON DELETE SET NULL "; elsif @cascade; "ON DELETE CASCADE "; else ""; end}>"
end