Class: MongoMapper::Associations::InArrayProxy

Inherits:
Collection show all
Includes:
Finders
Defined in:
lib/mongo_mapper/associations/in_array_proxy.rb

Instance Attribute Summary

Attributes inherited from Proxy

#owner, #reflection, #target

Instance Method Summary collapse

Methods included from Finders

#dynamic_find

Methods inherited from Collection

#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::Associations::Proxy

Dynamic Method Handling

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

Instance Method Details

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



94
95
96
97
98
99
100
101
102
# File 'lib/mongo_mapper/associations/in_array_proxy.rb', line 94

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



40
41
42
# File 'lib/mongo_mapper/associations/in_array_proxy.rb', line 40

def all(options={})
  klass.all(scoped_options(options))
end

#count(options = {}) ⇒ Object



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

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

#create(attrs = {}) ⇒ Object



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

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

#create!(attrs = {}) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/mongo_mapper/associations/in_array_proxy.rb', line 85

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

#delete_all(options = {}) ⇒ Object



64
65
66
67
68
69
# File 'lib/mongo_mapper/associations/in_array_proxy.rb', line 64

def delete_all(options={})
  docs = all(options.merge(:select => ['_id']))
  docs.each { |doc| ids.delete(doc.id) }
  klass.delete(docs.map(&:id))
  reset
end

#destroy_all(options = {}) ⇒ Object



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

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

#find(*args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mongo_mapper/associations/in_array_proxy.rb', line 6

def find(*args)
  options = args.extract_options!
  
  case args.first
    when :first
      first(options)
    when :last
      last(options)
    when :all
      all(options)
    else
      klass.find(*scoped_ids(args) << scoped_options(options))
  end
end

#find!(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mongo_mapper/associations/in_array_proxy.rb', line 21

def find!(*args)
  options = args.extract_options!
  
  case args.first
    when :first
      first(options)
    when :last
      last(options)
    when :all
      all(options)
    else
      klass.find!(*scoped_ids(args) << scoped_options(options))
  end
end

#first(options = {}) ⇒ Object



44
45
46
# File 'lib/mongo_mapper/associations/in_array_proxy.rb', line 44

def first(options={})
  klass.first(scoped_options(options))
end

#last(options = {}) ⇒ Object



48
49
50
# File 'lib/mongo_mapper/associations/in_array_proxy.rb', line 48

def last(options={})
  klass.last(scoped_options(options))
end

#nullifyObject



71
72
73
74
# File 'lib/mongo_mapper/associations/in_array_proxy.rb', line 71

def nullify
  replace([])
  reset
end

#paginate(options) ⇒ Object



36
37
38
# File 'lib/mongo_mapper/associations/in_array_proxy.rb', line 36

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

#replace(docs) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/mongo_mapper/associations/in_array_proxy.rb', line 106

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