Class: DBLeftovers::ForeignKey
- Inherits:
-
Object
- Object
- DBLeftovers::ForeignKey
- Defined in:
- lib/db_leftovers/foreign_key.rb
Instance Attribute Summary collapse
-
#cascade ⇒ Object
Returns the value of attribute cascade.
-
#constraint_name ⇒ Object
Returns the value of attribute constraint_name.
-
#from_column ⇒ Object
Returns the value of attribute from_column.
-
#from_table ⇒ Object
Returns the value of attribute from_table.
-
#set_null ⇒ Object
Returns the value of attribute set_null.
-
#to_column ⇒ Object
Returns the value of attribute to_column.
-
#to_table ⇒ Object
Returns the value of attribute to_table.
Instance Method Summary collapse
- #equals(other) ⇒ Object
-
#initialize(from_table, from_column, to_table, to_column, opts = {}) ⇒ ForeignKey
constructor
A new instance of ForeignKey.
- #name_constraint(from_table, from_column) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(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 27 |
# File 'lib/db_leftovers/foreign_key.rb', line 6 def initialize(from_table, from_column, to_table, to_column, opts={}) opts = { :on_delete => nil, :name => name_constraint(from_table, from_column) }.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, :name].include?(k) end raise "Unknown on_delete option: #{opts[:on_delete]}" unless [nil, :set_null, :cascade].include?(opts[:on_delete]) @constraint_name = opts[: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
#cascade ⇒ Object
Returns the value of attribute cascade.
4 5 6 |
# File 'lib/db_leftovers/foreign_key.rb', line 4 def cascade @cascade end |
#constraint_name ⇒ Object
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_column ⇒ Object
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_table ⇒ Object
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_null ⇒ Object
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_column ⇒ Object
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_table ⇒ Object
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
29 30 31 32 33 34 35 36 37 |
# File 'lib/db_leftovers/foreign_key.rb', line 29 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 |
#name_constraint(from_table, from_column) ⇒ Object
43 44 45 |
# File 'lib/db_leftovers/foreign_key.rb', line 43 def name_constraint(from_table, from_column) "fk_#{from_table}_#{from_column}" end |
#to_s ⇒ Object
39 40 41 |
# File 'lib/db_leftovers/foreign_key.rb', line 39 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 |