Class: DbSchema::Definitions::ForeignKey

Inherits:
Object
  • Object
show all
Defined in:
lib/db_schema/definitions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, fields:, table:, keys: [], on_update: :no_action, on_delete: :no_action, deferrable: false) ⇒ ForeignKey

Returns a new instance of ForeignKey.



93
94
95
96
97
98
99
100
101
# File 'lib/db_schema/definitions.rb', line 93

def initialize(name:, fields:, table:, keys: [], on_update: :no_action, on_delete: :no_action, deferrable: false)
  @name       = name
  @fields     = fields
  @table      = table
  @keys       = keys
  @on_update  = on_update
  @on_delete  = on_delete
  @deferrable = deferrable
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



91
92
93
# File 'lib/db_schema/definitions.rb', line 91

def fields
  @fields
end

#keysObject (readonly)

Returns the value of attribute keys.



91
92
93
# File 'lib/db_schema/definitions.rb', line 91

def keys
  @keys
end

#nameObject (readonly)

Returns the value of attribute name.



91
92
93
# File 'lib/db_schema/definitions.rb', line 91

def name
  @name
end

#on_deleteObject (readonly)

Returns the value of attribute on_delete.



91
92
93
# File 'lib/db_schema/definitions.rb', line 91

def on_delete
  @on_delete
end

#on_updateObject (readonly)

Returns the value of attribute on_update.



91
92
93
# File 'lib/db_schema/definitions.rb', line 91

def on_update
  @on_update
end

#tableObject (readonly)

Returns the value of attribute table.



91
92
93
# File 'lib/db_schema/definitions.rb', line 91

def table
  @table
end

Instance Method Details

#deferrable?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/db_schema/definitions.rb', line 107

def deferrable?
  @deferrable
end

#optionsObject



111
112
113
114
115
116
117
118
119
120
# File 'lib/db_schema/definitions.rb', line 111

def options
  {
    deferrable: deferrable?,
    name:       name,
    on_delete:  on_delete,
    on_update:  on_update
  }.tap do |options|
    options[:key] = keys unless references_primary_key?
  end
end

#references_primary_key?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/db_schema/definitions.rb', line 103

def references_primary_key?
  keys.empty?
end