Class: ActiveEntity::Associations::Embeds::Association

Inherits:
Object
  • Object
show all
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

CollectionAssociation, SingularAssociation

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#ownerObject (readonly)

:nodoc:



22
23
24
# File 'lib/active_entity/associations/embeds/association.rb', line 22

def owner
  @owner
end

#reflectionObject (readonly)

:nodoc:



22
23
24
# File 'lib/active_entity/associations/embeds/association.rb', line 22

def reflection
  @reflection
end

#targetObject

:nodoc:



22
23
24
# File 'lib/active_entity/associations/embeds/association.rb', line 22

def target
  @target
end

Instance Method Details

#extensionsObject



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

#klassObject

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?

Returns:

  • (Boolean)


36
37
38
# File 'lib/active_entity/associations/embeds/association.rb', line 36

def loaded?
  true
end

#marshal_dumpObject

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