Class: CassandraObject::BelongsTo::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra_object/belongs_to/association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, reflection) ⇒ Association

Returns a new instance of Association.



8
9
10
11
# File 'lib/cassandra_object/belongs_to/association.rb', line 8

def initialize(owner, reflection)
  @owner = owner
  @reflection = reflection
end

Instance Attribute Details

#ownerObject (readonly)

Returns the value of attribute owner.



4
5
6
# File 'lib/cassandra_object/belongs_to/association.rb', line 4

def owner
  @owner
end

#record_variableObject

Returns the value of attribute record_variable.



5
6
7
# File 'lib/cassandra_object/belongs_to/association.rb', line 5

def record_variable
  @record_variable
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



4
5
6
# File 'lib/cassandra_object/belongs_to/association.rb', line 4

def reflection
  @reflection
end

Instance Method Details

#association_classObject



35
36
37
# File 'lib/cassandra_object/belongs_to/association.rb', line 35

def association_class
  association_class_name.constantize
end

#association_class_nameObject



39
40
41
# File 'lib/cassandra_object/belongs_to/association.rb', line 39

def association_class_name
  reflection.polymorphic? ? owner.send(reflection.polymorphic_column) : reflection.class_name
end

#loaded?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/cassandra_object/belongs_to/association.rb', line 43

def loaded?
  @loaded
end

#readerObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cassandra_object/belongs_to/association.rb', line 13

def reader
  unless loaded?
    if record_id = owner.send(reflection.foreign_key).presence
      self.record_variable = association_class.find_by_id(record_id)
    else
      self.record_variable = nil
    end
    @loaded = true
  end

  record_variable
end

#writer(record) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/cassandra_object/belongs_to/association.rb', line 26

def writer(record)
  self.record_variable = record
  @loaded = true
  owner.send("#{reflection.foreign_key}=", record.try(:id))
  if reflection.polymorphic?
    owner.send("#{reflection.polymorphic_column}=", record.class.name)
  end
end