Class: Cequel::Model::RemoteAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/cequel/model/remote_association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, owning_class, options) ⇒ RemoteAssociation

Returns a new instance of RemoteAssociation.



9
10
11
12
13
14
# File 'lib/cequel/model/remote_association.rb', line 9

def initialize(name, owning_class, options)
  @name, @owning_class = name, owning_class
  @class_name = options[:class_name] || name.to_s.classify.to_sym
  @foreign_key_name = options[:foreign_key]
  @dependent = options[:dependent]
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



7
8
9
# File 'lib/cequel/model/remote_association.rb', line 7

def class_name
  @class_name
end

#dependentObject (readonly)

Returns the value of attribute dependent.



7
8
9
# File 'lib/cequel/model/remote_association.rb', line 7

def dependent
  @dependent
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/cequel/model/remote_association.rb', line 7

def name
  @name
end

Instance Method Details

#clazzObject



32
33
34
# File 'lib/cequel/model/remote_association.rb', line 32

def clazz
  @clazz ||= class_name.to_s.constantize
end

#foreign_key_nameObject



28
29
30
# File 'lib/cequel/model/remote_association.rb', line 28

def foreign_key_name
  @foreign_key_name ||= :"#{@owning_class.name.underscore}_id"
end

#primary_keyObject



20
21
22
# File 'lib/cequel/model/remote_association.rb', line 20

def primary_key
  @primary_key ||= @owning_class.key_column
end

#primary_key_nameObject



24
25
26
# File 'lib/cequel/model/remote_association.rb', line 24

def primary_key_name
  @primary_key_name ||= primary_key.name
end

#scope(instance) ⇒ Object



16
17
18
# File 'lib/cequel/model/remote_association.rb', line 16

def scope(instance)
  clazz.where(foreign_key_name => instance.__send__(primary_key_name))
end