Class: MongoMapper::Associations::ManyDocumentsProxy

Inherits:
Proxy
  • Object
show all
Defined in:
lib/mongomapper/associations/many_documents_proxy.rb

Instance Attribute Summary

Attributes inherited from Proxy

#association, #owner

Instance Method Summary collapse

Methods inherited from Proxy

#initialize, #inspect, #nil?, #reload_target, #reset, #respond_to?, #send

Constructor Details

This class inherits a constructor from MongoMapper::Associations::Proxy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MongoMapper::Associations::Proxy

Instance Method Details

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



37
38
39
40
41
# File 'lib/mongomapper/associations/many_documents_proxy.rb', line 37

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

#all(options = {}) ⇒ Object



15
16
17
# File 'lib/mongomapper/associations/many_documents_proxy.rb', line 15

def all(options={})
  find(:all, scoped_options(options))
end

#build(attrs = {}) ⇒ Object



45
46
47
48
49
# File 'lib/mongomapper/associations/many_documents_proxy.rb', line 45

def build(attrs={})
  doc = klass.new(attrs)
  apply_scope(doc)
  doc
end

#count(conditions = {}) ⇒ Object



27
28
29
# File 'lib/mongomapper/associations/many_documents_proxy.rb', line 27

def count(conditions={})
  klass.count(conditions.deep_merge(scoped_conditions))
end

#create(attrs = {}) ⇒ Object



51
52
53
54
55
# File 'lib/mongomapper/associations/many_documents_proxy.rb', line 51

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

#delete_all(conditions = {}) ⇒ Object



62
63
64
65
# File 'lib/mongomapper/associations/many_documents_proxy.rb', line 62

def delete_all(conditions={})
  klass.delete_all(conditions.deep_merge(scoped_conditions))
  reset
end

#destroy_all(conditions = {}) ⇒ Object



57
58
59
60
# File 'lib/mongomapper/associations/many_documents_proxy.rb', line 57

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

#find(*args) ⇒ Object



6
7
8
9
# File 'lib/mongomapper/associations/many_documents_proxy.rb', line 6

def find(*args)
  options = args.extract_options!
  klass.find(*args << scoped_options(options))
end

#first(options = {}) ⇒ Object



19
20
21
# File 'lib/mongomapper/associations/many_documents_proxy.rb', line 19

def first(options={})
  find(:first, scoped_options(options))
end

#last(options = {}) ⇒ Object



23
24
25
# File 'lib/mongomapper/associations/many_documents_proxy.rb', line 23

def last(options={})
  find(:last, scoped_options(options))
end

#nullifyObject



67
68
69
70
71
72
73
# File 'lib/mongomapper/associations/many_documents_proxy.rb', line 67

def nullify
  criteria = FinderOptions.to_mongo_criteria(scoped_conditions)
  all(criteria).each do |doc|
    doc.update_attributes self.foreign_key => nil
  end
  reset
end

#paginate(options) ⇒ Object



11
12
13
# File 'lib/mongomapper/associations/many_documents_proxy.rb', line 11

def paginate(options)
  klass.paginate(scoped_options(options))
end

#replace(docs) ⇒ Object



31
32
33
34
35
# File 'lib/mongomapper/associations/many_documents_proxy.rb', line 31

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