Class: Mingo::ManyProxy
- Inherits:
-
Object
show all
- Defined in:
- lib/mingo/many_proxy.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(parent, property, mapping) ⇒ ManyProxy
Returns a new instance of ManyProxy.
31
32
33
34
35
36
37
38
39
|
# File 'lib/mingo/many_proxy.rb', line 31
def initialize(parent, property, mapping)
@parent = parent
@property = property
@model, @self_referencing_key, @forward_referencing_key = analyze_mapping mapping
@counter_cache_field = "#{@property}_count"
@join_loaded = nil
@loaded = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
111
112
113
114
|
# File 'lib/mingo/many_proxy.rb', line 111
def method_missing(method, *args, &block)
load_collection
@loaded.send(method, *args, &block)
end
|
Class Method Details
.decorate_each(&block) ⇒ Object
23
24
25
26
27
28
29
|
# File 'lib/mingo/many_proxy.rb', line 23
def self.decorate_each(&block)
if block_given?
@decorate_each = block
else
@decorate_each
end
end
|
.decorate_with(mod = nil, &block) ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/mingo/many_proxy.rb', line 15
def self.decorate_with(mod = nil, &block)
if mod or block_given?
@decorate_with = mod || Module.new(&block)
else
@decorate_with
end
end
|
Instance Method Details
#<<(doc) ⇒ Object
62
63
64
65
66
67
68
69
|
# File 'lib/mingo/many_proxy.rb', line 62
def <<(doc)
doc = convert(doc)
doc['_id'] = join_collection.save doc
change_counter_cache(1)
load_join << doc if @join_loaded
unload_collection
self
end
|
#cache_key ⇒ Object
105
106
107
|
# File 'lib/mingo/many_proxy.rb', line 105
def cache_key
[@parent, @property, counter_cache]
end
|
#convert(doc) ⇒ Object
58
59
60
|
# File 'lib/mingo/many_proxy.rb', line 58
def convert(doc)
{@self_referencing_key => @parent.id, @forward_referencing_key => doc.id}
end
|
#delete(doc) ⇒ Object
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/mingo/many_proxy.rb', line 71
def delete(doc)
doc = convert(doc)
if join_doc = find_join_doc(doc[@forward_referencing_key])
join_collection.remove :_id => join_doc['_id']
change_counter_cache(-1)
unload_collection
@join_loaded.delete join_doc
end
doc
end
|
#find(selector = {}, options = {}, &block) ⇒ Object
91
92
93
|
# File 'lib/mingo/many_proxy.rb', line 91
def find(selector = {}, options = {}, &block)
@model.find(selector, find_options.merge(options), &block)
end
|
#include?(doc) ⇒ Boolean
54
55
56
|
# File 'lib/mingo/many_proxy.rb', line 54
def include?(doc)
!!find_join_doc(doc.id)
end
|
#loaded? ⇒ Boolean
82
83
84
|
# File 'lib/mingo/many_proxy.rb', line 82
def loaded?
!!@loaded
end
|
#object_ids ⇒ Object
44
45
46
47
48
|
# File 'lib/mingo/many_proxy.rb', line 44
def object_ids
join_docs = load_join
join_docs = join_docs.select(&Proc.new) if block_given?
join_docs.map { |doc| doc[@forward_referencing_key] }
end
|
#reload ⇒ Object
86
87
88
89
|
# File 'lib/mingo/many_proxy.rb', line 86
def reload
@loaded = @join_loaded = nil
self
end
|
#reset_counter_cache ⇒ Object
99
100
101
102
103
|
# File 'lib/mingo/many_proxy.rb', line 99
def reset_counter_cache
delta = join_cursor.count - counter_cache
change_counter_cache delta
counter_cache
end
|
#respond_to?(method, priv = false) ⇒ Boolean
95
96
97
|
# File 'lib/mingo/many_proxy.rb', line 95
def respond_to?(method, priv = false)
super || method_missing(:respond_to?, method, priv)
end
|
#size ⇒ Object
50
51
52
|
# File 'lib/mingo/many_proxy.rb', line 50
def size
(counter_cache? && counter_cache) || (@join_loaded && @join_loaded.size) || join_cursor.count
end
|