Class: MongoMapper::Plugins::Associations::OneProxy

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

Instance Attribute Summary

Attributes inherited from Proxy

#association, #proxy_owner, #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::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 = {}) ⇒ Object



6
7
8
# File 'lib/mongo_mapper/plugins/associations/one_proxy.rb', line 6

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

#create(attrs = {}) ⇒ Object



10
11
12
# File 'lib/mongo_mapper/plugins/associations/one_proxy.rb', line 10

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

#create!(attrs = {}) ⇒ Object



14
15
16
# File 'lib/mongo_mapper/plugins/associations/one_proxy.rb', line 14

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

#replace(doc) ⇒ Object



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
43
44
45
# File 'lib/mongo_mapper/plugins/associations/one_proxy.rb', line 18

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?
    proxy_owner.save if proxy_owner.new?
    doc = klass.new(doc) unless klass === doc
    doc[foreign_key] = proxy_owner.id
    doc.save if doc.new?
    loaded
    @target = doc
  end
end