Class: ActiveEntity::Associations::Embeds::Association
- Inherits:
-
Object
- Object
- ActiveEntity::Associations::Embeds::Association
- Defined in:
- lib/active_entity/associations/embeds/association.rb
Overview
Active Entity Associations
This is the root class of all associations (‘+ Foo’ signifies an included module Foo):
Association
SingularAssociation
HasOneAssociation + ForeignAssociation
HasOneThroughAssociation + ThroughAssociation
BelongsToAssociation
BelongsToPolymorphicAssociation
CollectionAssociation
HasManyAssociation + ForeignAssociation
HasManyThroughAssociation + ThroughAssociation
Direct Known Subclasses
Instance Attribute Summary collapse
-
#owner ⇒ Object
readonly
:nodoc:.
-
#reflection ⇒ Object
readonly
:nodoc:.
-
#target ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #extensions ⇒ Object
-
#initialize(owner, reflection) ⇒ Association
constructor
A new instance of Association.
-
#initialize_attributes(record, attributes = {}) ⇒ Object
:nodoc:.
- #inversed_from(record) ⇒ Object
-
#klass ⇒ Object
Returns the class of the target.
-
#loaded? ⇒ Boolean
Has the target been already loaded?.
-
#marshal_dump ⇒ Object
We can’t dump @reflection and @through_reflection since it contains the scope proc.
- #marshal_load(data) ⇒ Object
-
#remove_inverse_instance(record) ⇒ Object
Remove the inverse association, if possible.
-
#set_inverse_instance(record) ⇒ Object
Set the inverse association, if possible.
Constructor Details
#initialize(owner, reflection) ⇒ Association
Returns a new instance of Association.
26 27 28 29 30 31 32 33 |
# File 'lib/active_entity/associations/embeds/association.rb', line 26 def initialize(owner, reflection) reflection.check_validity! @owner, @reflection = owner, reflection @target = nil @inversed = false end |
Instance Attribute Details
#owner ⇒ Object (readonly)
:nodoc:
22 23 24 |
# File 'lib/active_entity/associations/embeds/association.rb', line 22 def owner @owner end |
#reflection ⇒ Object (readonly)
:nodoc:
22 23 24 |
# File 'lib/active_entity/associations/embeds/association.rb', line 22 def reflection @reflection end |
#target ⇒ Object
:nodoc:
22 23 24 |
# File 'lib/active_entity/associations/embeds/association.rb', line 22 def target @target end |
Instance Method Details
#extensions ⇒ Object
69 70 71 |
# File 'lib/active_entity/associations/embeds/association.rb', line 69 def extensions reflection.extensions end |
#initialize_attributes(record, attributes = {}) ⇒ Object
:nodoc:
85 86 87 88 |
# File 'lib/active_entity/associations/embeds/association.rb', line 85 def initialize_attributes(record, attributes = {}) #:nodoc: record.assign_attributes attributes if attributes.any? set_inverse_instance(record) end |
#inversed_from(record) ⇒ Object
58 59 60 61 |
# File 'lib/active_entity/associations/embeds/association.rb', line 58 def inversed_from(record) self.target = record @inversed = !!record end |
#klass ⇒ Object
Returns the class of the target. belongs_to polymorphic overrides this to look at the polymorphic_type field on the owner.
65 66 67 |
# File 'lib/active_entity/associations/embeds/association.rb', line 65 def klass reflection.klass end |
#loaded? ⇒ Boolean
Has the target been already loaded?
36 37 38 |
# File 'lib/active_entity/associations/embeds/association.rb', line 36 def loaded? true end |
#marshal_dump ⇒ Object
We can’t dump @reflection and @through_reflection since it contains the scope proc
74 75 76 77 |
# File 'lib/active_entity/associations/embeds/association.rb', line 74 def marshal_dump ivars = (instance_variables - [:@reflection, :@through_reflection]).map { |name| [name, instance_variable_get(name)] } [@reflection.name, ivars] end |
#marshal_load(data) ⇒ Object
79 80 81 82 83 |
# File 'lib/active_entity/associations/embeds/association.rb', line 79 def marshal_load(data) reflection_name, ivars = data ivars.each { |name, val| instance_variable_set(name, val) } @reflection = @owner.class._reflect_on_association(reflection_name) end |
#remove_inverse_instance(record) ⇒ Object
Remove the inverse association, if possible
52 53 54 55 56 |
# File 'lib/active_entity/associations/embeds/association.rb', line 52 def remove_inverse_instance(record) if inverse = inverse_association_for(record) inverse.inversed_from(nil) end end |
#set_inverse_instance(record) ⇒ Object
Set the inverse association, if possible
44 45 46 47 48 49 |
# File 'lib/active_entity/associations/embeds/association.rb', line 44 def set_inverse_instance(record) if inverse = inverse_association_for(record) inverse.inversed_from(owner) end record end |