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

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

Instance Attribute Summary

Attributes inherited from Proxy

#owner, #reflection, #target

Instance Method Summary collapse

Methods included from Support::Find

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

Dynamic Method Handling

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

Instance Method Details

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



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

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



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

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

#count(options = {}) ⇒ Object



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

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

#create(attrs = {}) ⇒ Object



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

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

#create!(attrs = {}) ⇒ Object



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

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

#delete_all(options = {}) ⇒ Object



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

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



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

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

#find(*args) ⇒ Object



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

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



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

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



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

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

#last(options = {}) ⇒ Object



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

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

#nullifyObject



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

def nullify
  replace([])
  reset
end

#paginate(options) ⇒ Object



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

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

#replace(docs) ⇒ Object



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

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