Class: MongoMapper::Associations::ManyDocumentsProxy

Inherits:
Proxy
  • Object
show all
Includes:
Finders
Defined in:
lib/mongo_mapper/associations/many_documents_proxy.rb

Instance Attribute Summary

Attributes inherited from Proxy

#association, #owner

Instance Method Summary collapse

Methods included from Finders

#dynamic_find

Methods inherited from Proxy

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

#method_missing(method, *args) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/mongo_mapper/associations/many_documents_proxy.rb', line 78

def method_missing(method, *args)
  finder = DynamicFinder.new(method)
  
  if finder.found?
    dynamic_find(finder, args)
  else
    super
  end
end

Instance Method Details

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



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

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

#all(options = {}) ⇒ Object



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

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

#build(attrs = {}) ⇒ Object



48
49
50
51
52
# File 'lib/mongo_mapper/associations/many_documents_proxy.rb', line 48

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

#count(conditions = {}) ⇒ Object



30
31
32
# File 'lib/mongo_mapper/associations/many_documents_proxy.rb', line 30

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

#create(attrs = {}) ⇒ Object



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

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

#delete_all(conditions = {}) ⇒ Object



65
66
67
68
# File 'lib/mongo_mapper/associations/many_documents_proxy.rb', line 65

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

#destroy_all(conditions = {}) ⇒ Object



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

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

#find(*args) ⇒ Object



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

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

#first(options = {}) ⇒ Object



22
23
24
# File 'lib/mongo_mapper/associations/many_documents_proxy.rb', line 22

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

#last(options = {}) ⇒ Object



26
27
28
# File 'lib/mongo_mapper/associations/many_documents_proxy.rb', line 26

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

#nullifyObject



70
71
72
73
74
75
76
# File 'lib/mongo_mapper/associations/many_documents_proxy.rb', line 70

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



14
15
16
# File 'lib/mongo_mapper/associations/many_documents_proxy.rb', line 14

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

#replace(docs) ⇒ Object



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

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