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, #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



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

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

#create(attrs = {}) ⇒ Object



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

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

#create!(attrs = {}) ⇒ Object



16
17
18
# File 'lib/mongo_mapper/plugins/associations/one_proxy.rb', line 16

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

#replace(doc) ⇒ Object



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
46
47
# File 'lib/mongo_mapper/plugins/associations/one_proxy.rb', line 20

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