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

Inherits:
Collection show all
Includes:
DynamicQuerying::ClassMethods
Defined in:
lib/mongo_mapper/plugins/associations/proxy/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

#[], #each, #empty?, #length, #read, #reset, #size, #to_a, #write

Methods inherited from Proxy

define_proxy_method, #initialize, #inspect, #loaded, #loaded?, #proxy_method, #proxy_respond_to?, #read, #reload, #reset, #respond_to?, #write

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



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

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

#all(options = {}) ⇒ Object



21
22
23
24
# File 'lib/mongo_mapper/plugins/associations/proxy/in_array_proxy.rb', line 21

def all(options={})
  return [] if ids.blank?
  order_results(query(options).all)
end

#count(options = {}) ⇒ Object



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

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

#create(attrs = {}) ⇒ Object



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

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

#create!(attrs = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/mongo_mapper/plugins/associations/proxy/in_array_proxy.rb', line 83

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

#delete_all(options = {}) ⇒ Object



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

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

#destroy_all(options = {}) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/mongo_mapper/plugins/associations/proxy/in_array_proxy.rb', line 53

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/proxy/in_array_proxy.rb', line 8

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

#find!(*args) ⇒ Object



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

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

#first(options = {}) ⇒ Object



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

def first(options={})
  return nil if ids.blank?

  if ordered?
    ids = find_ordered_ids(options)
    find!(ids.first) if ids.any?
  else
    query(options).first
  end
end

#last(options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/mongo_mapper/plugins/associations/proxy/in_array_proxy.rb', line 37

def last(options={})
  return nil if ids.blank?

  if ordered?
    ids = find_ordered_ids(options)
    find!(ids.last) if ids.any?
  else
    query(options).last
  end
end

#nullifyObject



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

def nullify
  replace([])
  reset
end

#paginate(options) ⇒ Object



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

def paginate(options)
  return [] if ids.blank?
  query.paginate(options)
end

#replace(docs) ⇒ Object



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

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