Class: MongoMapper::Plugins::Associations::ManyDocumentsProxy

Inherits:
Collection show all
Includes:
DynamicQuerying::ClassMethods, Querying::PluckyMethods
Defined in:
lib/mongo_mapper/plugins/associations/many_documents_proxy.rb

Direct Known Subclasses

ManyDocumentsAsProxy, ManyPolymorphicProxy

Instance Attribute Summary

Attributes inherited from Proxy

#association, #proxy_owner, #target

Instance Method Summary collapse

Methods included from DynamicQuerying::ClassMethods

#dynamic_find

Methods inherited from Collection

#include?, #reset, #to_ary

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

#method_missing(method, *args, &block) ⇒ Object (protected)



72
73
74
75
76
77
78
79
80
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 72

def method_missing(method, *args, &block)
  if klass.respond_to?(method)
    result = klass.send(method, *args, &block)
    result.is_a?(Plucky::Query) ? 
      query.merge(result) : super
  else
    super
  end
end

Instance Method Details

#<<(*docs) ⇒ Object Also known as: push, concat



16
17
18
19
20
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 16

def <<(*docs)
  ensure_owner_saved
  flatten_deeper(docs).each { |doc| prepare(doc).save }
  reset
end

#build(attrs = {}) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 24

def build(attrs={})
  doc = klass.new(attrs)
  apply_scope(doc)
  @target ||= [] unless loaded?
  @target << doc
  doc
end

#create(attrs = {}) ⇒ Object



32
33
34
35
36
37
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 32

def create(attrs={})
  doc = klass.new(attrs)
  apply_scope(doc).save
  reset
  doc
end

#create!(attrs = {}) ⇒ Object



39
40
41
42
43
44
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 39

def create!(attrs={})
  doc = klass.new(attrs)
  apply_scope(doc).save!
  reset
  doc
end

#delete_all(options = {}) ⇒ Object



51
52
53
54
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 51

def delete_all(options={})
  query(options).remove
  reset
end

#destroy_all(options = {}) ⇒ Object



46
47
48
49
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 46

def destroy_all(options={})
  all(options).map(&:destroy)
  reset
end

#nullifyObject



56
57
58
59
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 56

def nullify
  all.each { |doc| doc.update_attributes(self.foreign_key => nil) }
  reset
end

#replace(docs) ⇒ Object



9
10
11
12
13
14
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 9

def replace(docs)
  load_target
  target.map(&:destroy)
  docs.each { |doc| prepare(doc).save }
  reset
end

#save_to_collection(options = {}) ⇒ Object



61
62
63
# File 'lib/mongo_mapper/plugins/associations/many_documents_proxy.rb', line 61

def save_to_collection(options={})
  @target.each { |doc| doc.save(options) } if @target
end