Class: MarkMapper::Plugins::Associations::InArrayProxy

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

#include?, #reset, #to_ary

Methods inherited from Proxy

#as_json, #blank?, #initialize, #inspect, #loaded, #loaded?, #nil?, #present?, #proxy_respond_to?, #reload, #reset, #respond_to?, #send, #to_json

Constructor Details

This class inherits a constructor from MarkMapper::Plugins::Associations::Proxy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MarkMapper::Plugins::DynamicQuerying::ClassMethods

Instance Method Details

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


81
82
83
84
85
86
87
88
89
# File 'lib/mark_mapper/plugins/associations/in_array_proxy.rb', line 81

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/mark_mapper/plugins/associations/in_array_proxy.rb', line 21

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

#count(options = {}) ⇒ Object


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

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

#create(attrs = {}) ⇒ Object


61
62
63
64
65
66
67
68
69
# File 'lib/mark_mapper/plugins/associations/in_array_proxy.rb', line 61

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

#create!(attrs = {}) ⇒ Object


71
72
73
74
75
76
77
78
79
# File 'lib/mark_mapper/plugins/associations/in_array_proxy.rb', line 71

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

#delete_all(options = {}) ⇒ Object


49
50
51
52
53
54
# File 'lib/mark_mapper/plugins/associations/in_array_proxy.rb', line 49

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


41
42
43
44
45
46
47
# File 'lib/mark_mapper/plugins/associations/in_array_proxy.rb', line 41

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

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

#find!(*args) ⇒ Object


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

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

#first(options = {}) ⇒ Object


26
27
28
29
# File 'lib/mark_mapper/plugins/associations/in_array_proxy.rb', line 26

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

#last(options = {}) ⇒ Object


31
32
33
34
# File 'lib/mark_mapper/plugins/associations/in_array_proxy.rb', line 31

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

#nullifyObject


56
57
58
59
# File 'lib/mark_mapper/plugins/associations/in_array_proxy.rb', line 56

def nullify
  replace([])
  reset
end

#paginate(options) ⇒ Object


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

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

#replace(docs) ⇒ Object


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

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