Module: SimpleMapper::Associations

Defined in:
lib/simple_mapper/default_plugins/associations.rb

Defined Under Namespace

Modules: AssociationMacros Classes: Association

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



33
34
35
# File 'lib/simple_mapper/default_plugins/associations.rb', line 33

def self.included(base)
  base.extend(AssociationMacros)
end

Instance Method Details

#association(name) ⇒ Object

Accesses name in assocations hash



16
17
18
# File 'lib/simple_mapper/default_plugins/associations.rb', line 16

def association(name)
  associations[name]
end

#association_set(name, options = {}) ⇒ Object

Returns an Association::Instance or Association::Set object (depending on the type of association, :single or :many), with the association applied to the current object. Note that the association definition object is duplicated when it is applied to an object – this allows for them to be modified after they are applied to an object without modifying the class association template.



9
10
11
12
13
# File 'lib/simple_mapper/default_plugins/associations.rb', line 9

def association_set(name,options={})
  @association_sets ||= {}
  names = name.is_a?(Array) ? name : [name]
  @association_sets[names.flatten.sort.join('&')] ||= names.inject(associations[names.shift]) {|a,n| a.merge(n)}.set(self)
end

#associationsObject

Generates (and caches) a hash of the associations for this object’s class – but duplicates that are applied to this object.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/simple_mapper/default_plugins/associations.rb', line 21

def associations
  @associations || begin
    @associations = {}
    self.class.associations.keys.each {|k| @associations[k] = self.class.associations[k].apply_to(self)}
    obj = self
    (class << @associations; self end).send(:define_method, :[]) do |k|
      obj.class.associations[k].apply_to(obj)
    end
  end
  @associations
end