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

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



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

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

#create(attrs = {}) ⇒ Object



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

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

#create!(attrs = {}) ⇒ Object



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

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

#replace(doc) ⇒ Object



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 19

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