Class: MongoMapper::Plugins::Associations::SingleAssociation

Inherits:
Base
  • Object
show all
Defined in:
lib/mongo_mapper/plugins/associations/single_association.rb

Direct Known Subclasses

BelongsToAssociation, OneAssociation

Constant Summary

Constant Summary

Constants inherited from Base

Base::AssociationOptions

Instance Attribute Summary

Attributes inherited from Base

#name, #options, #query_options

Instance Method Summary collapse

Methods inherited from Base

#as, #as?, #autosave?, #class_name, #counter_cache?, #embeddable?, #foreign_key, #in_array?, #initialize, #ivar, #klass, #polymorphic?, #proxy_class, #touch?, #type_key_name

Constructor Details

This class inherits a constructor from MongoMapper::Plugins::Associations::Base

Instance Method Details

#setup(model) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mongo_mapper/plugins/associations/single_association.rb', line 6

def setup(model)
  @model = model
  model.associations_module.module_eval "def \#{name}\nproxy = get_proxy(associations[\#{name.inspect}])\nproxy.nil? ? nil : proxy\nend\n\ndef \#{name}=(value)\nassociation = associations[\#{name.inspect}]\nproxy = get_proxy(association)\n\nif proxy.nil? || proxy.target != value\nproxy = build_proxy(association)\nend\n\nproxy.replace(value)\nvalue\nend\n\ndef \#{name}?\nget_proxy(associations[\#{name.inspect}]).present?\nend\n\ndef build_\#{name}(attrs={}, &block)\nget_proxy(associations[\#{name.inspect}]).build(attrs, &block)\nend\n\ndef create_\#{name}(attrs={}, &block)\nget_proxy(associations[\#{name.inspect}]).create(attrs, &block)\nend\n\ndef create_\#{name}!(attrs={}, &block)\nget_proxy(associations[\#{name.inspect}]).create!(attrs, &block)\nend\n"
end