Class: MongoMapper::Plugins::Associations::BelongsToProxy

Inherits:
Proxy
  • Object
show all
Defined in:
lib/mongo_mapper/plugins/associations/proxy/belongs_to_proxy.rb

Instance Attribute Summary

Attributes inherited from Proxy

#association, #proxy_owner, #target

Instance Method Summary collapse

Methods inherited from Proxy

define_proxy_method, #initialize, #inspect, #loaded, #loaded?, #proxy_method, #proxy_respond_to?, #read, #reload, #reset, #respond_to?, #write

Constructor Details

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MongoMapper::Plugins::Associations::Proxy

Instance Method Details

#build(attrs = {}, &block) ⇒ Object



21
22
23
# File 'lib/mongo_mapper/plugins/associations/proxy/belongs_to_proxy.rb', line 21

def build(attrs={}, &block)
  instantiate_target(:new, attrs, &block)
end

#create(attrs = {}, &block) ⇒ Object



25
26
27
# File 'lib/mongo_mapper/plugins/associations/proxy/belongs_to_proxy.rb', line 25

def create(attrs={}, &block)
  instantiate_target(:create, attrs, &block)
end

#create!(attrs = {}, &block) ⇒ Object



29
30
31
# File 'lib/mongo_mapper/plugins/associations/proxy/belongs_to_proxy.rb', line 29

def create!(attrs={}, &block)
  instantiate_target(:create!, attrs, &block)
end

#replace(doc) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mongo_mapper/plugins/associations/proxy/belongs_to_proxy.rb', line 6

def replace(doc)
  if doc
    doc.save if !doc.persisted?
    id = doc.id
  end

  reset
  proxy_owner[association.foreign_key] = id
  unless doc.nil?
    loaded
    @target = doc
  end
  @target
end

#save_to_collection(options = {}) ⇒ Object



33
34
35
# File 'lib/mongo_mapper/plugins/associations/proxy/belongs_to_proxy.rb', line 33

def save_to_collection(options={})
  @target.save(options) if @target
end