Class: DataMapper::Associations::Reference

Inherits:
Object
  • Object
show all
Defined in:
lib/data_mapper/associations/reference.rb

Overview

Reference is an abstract-class providing the boiler-plate for the association proxies (ie: HasManyAssociation::Set, or HasOneAssociation::Instance) The proxies need to access the defining Association instances to obtain mapping information. This class provides convenient access to said Association.

EXAMPLE:

class Zoo
  has_many :exhibits
end

The has_many declaration instantiates a DataMapper::Associations::HasManyAssociation and adds it to the DataMapper::Adapters::Sql::Mappings::Table#associations array for the Table representing Zoo.

Zoo.new.exhibits

exhibits above returns an instance of DataMapper::Associations::HasManyAssociation::Set. This instance needs to access the actual HasManyAssociation instance in order to access the mapping information within. The DataMapper::Associations::Reference abstract-class for the Set provides the Reference#association method in order to provide easy access to this information.

Instance Method Summary collapse

Constructor Details

#initialize(instance, association_name) ⇒ Reference

instance is a mapped object instance. ie: #<Zoo:0x123456 …> association_name is the Symbol used to look up the Association instance within the DataMapper::Adapters::Sql::Mappings::Table



33
34
35
36
# File 'lib/data_mapper/associations/reference.rb', line 33

def initialize(instance, association_name)
  @instance, @association_name = instance, association_name.to_sym
  @instance.loaded_associations << self
end

Instance Method Details

#associationObject

#association provides lazily initialized access to the declared Association.



40
41
42
# File 'lib/data_mapper/associations/reference.rb', line 40

def association
  @association || (@association = @instance.database_context.table(@instance.class).associations[@association_name])
end