Class: Rhubarb::Reference

Inherits:
Object
  • Object
show all
Defined in:
lib/rhubarb/reference.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options = {}) ⇒ Reference

Creates a new Reference object, modeling a foreign-key relationship to another table. klass is a class that includes Persisting; options is a hash of options, which include

:column => name

specifies the name of the column to reference in klass (defaults to row id)

:on_delete => :cascade

specifies that deleting the referenced row in klass will delete all rows referencing that row through this reference



20
21
22
23
24
25
# File 'lib/rhubarb/reference.rb', line 20

def initialize(klass, options={})
  @referent = klass
  @options = options
  @options[:column] ||= "row_id"
  @column = options[:column]
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



15
16
17
# File 'lib/rhubarb/reference.rb', line 15

def column
  @column
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/rhubarb/reference.rb', line 15

def options
  @options
end

#referentObject (readonly)

Returns the value of attribute referent.



15
16
17
# File 'lib/rhubarb/reference.rb', line 15

def referent
  @referent
end

Instance Method Details

#managed_ref?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/rhubarb/reference.rb', line 33

def managed_ref?
  # XXX?
  return false if referent.class == String
  referent.ancestors.include? Persisting
end

#to_sObject



27
28
29
30
31
# File 'lib/rhubarb/reference.rb', line 27

def to_s
  trigger = ""
  trigger = " on delete cascade" if options[:on_delete] == :cascade
  "references #{@referent}(#{@column})#{trigger}"
end