Class: CassandraObject::BelongsTo::Association
- Inherits:
-
Object
- Object
- CassandraObject::BelongsTo::Association
- Defined in:
- lib/cassandra_object/belongs_to/association.rb
Instance Attribute Summary collapse
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#record_variable ⇒ Object
Returns the value of attribute record_variable.
-
#reflection ⇒ Object
readonly
Returns the value of attribute reflection.
Instance Method Summary collapse
- #association_class ⇒ Object
- #association_class_name ⇒ Object
-
#initialize(owner, reflection) ⇒ Association
constructor
A new instance of Association.
- #loaded? ⇒ Boolean
- #reader ⇒ Object
- #writer(record) ⇒ Object
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
#owner ⇒ Object (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_variable ⇒ Object
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 |
#reflection ⇒ Object (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_class ⇒ Object
35 36 37 |
# File 'lib/cassandra_object/belongs_to/association.rb', line 35 def association_class association_class_name.constantize end |
#association_class_name ⇒ Object
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
43 44 45 |
# File 'lib/cassandra_object/belongs_to/association.rb', line 43 def loaded? @loaded end |
#reader ⇒ Object
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 |