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

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



114
115
116
117
118
119
120
121
122
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 114

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 42

def all(options={})
  random_order = klass.all(scoped_conditions)

  if (scoped_options(options)[:preserve_order])
    preserve_order = Array.new
    scoped_conditions[:_id].each do |id|
      random_order.each do |element|
        if (id.eql?(element._id))
          preserve_order << element
          break
        end
      end
    end
    return_value = preserve_order
  else
    return_value = random_order
  end
  return return_value
end

#count(options = {}) ⇒ Object



70
71
72
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 70

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

#create(attrs = {}) ⇒ Object



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

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

#create!(attrs = {}) ⇒ Object



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

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

#delete_all(options = {}) ⇒ Object



82
83
84
85
86
87
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 82

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



74
75
76
77
78
79
80
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 74

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



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

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

#last(options = {}) ⇒ Object



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

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

#nullifyObject



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

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



126
127
128
129
130
131
132
133
# File 'lib/mongo_mapper/plugins/associations/in_array_proxy.rb', line 126

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