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, #owner, #target

Instance Method Summary collapse

Methods included from DynamicQuerying::ClassMethods

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

Instance Method Details

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



98
99
100
101
102
103
104
105
106
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 98

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



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

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

#count(options = {}) ⇒ Object



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

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

#create(attrs = {}) ⇒ Object



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

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

#create!(attrs = {}) ⇒ Object



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

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

#delete_all(options = {}) ⇒ Object



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

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



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

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

#find(*args) ⇒ Object



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

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



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

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



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

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

#last(options = {}) ⇒ Object



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

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

#nullifyObject



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

def nullify
  replace([])
  reset
end

#paginate(options) ⇒ Object



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

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

#replace(docs) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 110

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