Class: MongoMapper::Plugins::Associations::InArrayProxy

Inherits:
Collection show all
Includes:
DynamicQuerying::ClassMethods
Defined in:
lib/mongo_mapper/plugins/associations/in_array_proxy.rb

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

#===, #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::DynamicQuerying::ClassMethods

Instance Method Details

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



76
77
78
79
80
81
82
83
84
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 76

def <<(*docs)
  flatten_deeper(docs).each do |doc|
    doc.save if doc.new?
    unless ids.include?(doc.id)
      ids << doc.id
    end
  end
  reset
end

#all(options = {}) ⇒ Object



20
21
22
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 20

def all(options={})
  query(options).all
end

#count(options = {}) ⇒ Object



32
33
34
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 32

def count(options={})
  options.blank? ? ids.size : query(options).count
end

#create(attrs = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 56

def create(attrs={})
  doc = klass.create(attrs)
  unless doc.new?
    ids << doc.id
    proxy_owner.save
    reset
  end
  doc
end

#create!(attrs = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 66

def create!(attrs={})
  doc = klass.create!(attrs)
  unless doc.new?
    ids << doc.id
    proxy_owner.save
    reset
  end
  doc
end

#delete_all(options = {}) ⇒ Object



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

def delete_all(options={})
  docs = query(options).fields(:_id).all
  docs.each { |doc| ids.delete(doc.id) }
  klass.delete(docs.map(&:id))
  reset
end

#destroy_all(options = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 36

def destroy_all(options={})
  all(options).each do |doc|
    ids.delete(doc.id)
    doc.destroy
  end
  reset
end

#find(*args) ⇒ Object



8
9
10
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 8

def find(*args)
  query.find(*scoped_ids(args))
end

#find!(*args) ⇒ Object



12
13
14
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 12

def find!(*args)
  query.find!(*scoped_ids(args))
end

#first(options = {}) ⇒ Object



24
25
26
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 24

def first(options={})
  query(options).first
end

#last(options = {}) ⇒ Object



28
29
30
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 28

def last(options={})
  query(options).last
end

#nullifyObject



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

def nullify
  replace([])
  reset
end

#paginate(options) ⇒ Object



16
17
18
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 16

def paginate(options)
  query.paginate(options)
end

#replace(docs) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 88

def replace(docs)
  doc_ids = docs.map do |doc|
    doc.save if doc.new?
    doc.id
  end
  ids.replace(doc_ids.uniq)
  reset
end