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

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

Direct Known Subclasses

OneAsProxy

Instance Attribute Summary

Attributes inherited from Proxy

#association, #proxy_owner, #target

Instance Method Summary collapse

Methods inherited from Proxy

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

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

#deleteObject



49
50
51
52
# File 'lib/mongo_mapper/plugins/associations/one_proxy.rb', line 49

def delete
  target.delete
  reset
end

#destroyObject



44
45
46
47
# File 'lib/mongo_mapper/plugins/associations/one_proxy.rb', line 44

def destroy
  target.destroy
  reset
end

#nullifyObject



54
55
56
57
58
# File 'lib/mongo_mapper/plugins/associations/one_proxy.rb', line 54

def nullify
  nullify_scope(target)
  target.save
  reset
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
# File 'lib/mongo_mapper/plugins/associations/one_proxy.rb', line 18

def replace(doc)
  load_target

  if !target.nil? && target != doc
    if target.persisted?
      case options[:dependent]
        when :delete  then target.delete
        when :destroy then target.destroy
        else
          nullify_scope(target)
          target.save
      end
    end
  end
  
  unless doc.nil?
    proxy_owner.save unless proxy_owner.persisted?
    doc = klass.new(doc) unless doc.is_a?(klass)
    apply_scope(doc)
    doc.save unless doc.persisted?
  end

  loaded
  @target = doc
end