Class: MongoMapper::Associations::OneProxy

Inherits:
Proxy show all
Defined in:
lib/mongo_mapper/associations/one_proxy.rb

Instance Attribute Summary

Attributes inherited from Proxy

#owner, #reflection, #target

Instance Method Summary collapse

Methods inherited from Proxy

#===, #blank?, #initialize, #inspect, #loaded, #loaded?, #nil?, #present?, #proxy_respond_to?, #reload, #reset, #respond_to?, #send

Constructor Details

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

Dynamic Method Handling

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

Instance Method Details

#build(attrs = {}) ⇒ Object



4
5
6
# File 'lib/mongo_mapper/associations/one_proxy.rb', line 4

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

#create(attrs = {}) ⇒ Object



8
9
10
# File 'lib/mongo_mapper/associations/one_proxy.rb', line 8

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

#create!(attrs = {}) ⇒ Object



12
13
14
# File 'lib/mongo_mapper/associations/one_proxy.rb', line 12

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

#replace(doc) ⇒ Object



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/associations/one_proxy.rb', line 16

def replace(doc)
  load_target
  
  if !target.nil? && target != doc
    if options[:dependent] && !target.new?
      case options[:dependent]
        when :delete
          target.delete
        when :destroy
          target.destroy
        when :nullify
          target[foreign_key] = nil
          target.save
      end
    end
  end
  
  reset
  
  unless doc.nil?
    owner.save if owner.new?
    doc[foreign_key] = owner.id
    doc.save if doc.new?
    loaded
    @target = doc
  end
end