Class: ActiveMongoid::Associations::Targets::Enumerable
- Inherits:
-
Object
- Object
- ActiveMongoid::Associations::Targets::Enumerable
show all
- Includes:
- Enumerable
- Defined in:
- lib/active_mongoid/associations/targets/enumerable.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(target) ⇒ Enumerable
Returns a new instance of Enumerable.
85
86
87
88
89
90
91
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 85
def initialize(target)
if target.is_a?(::Mongoid::Criteria) || target.is_a?(::ActiveRecord::Relation)
@added, @loaded, @unloaded = [], [], target
else
@added, @executed, @loaded = [], true, target
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
154
155
156
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 154
def method_missing(name, *args, &block)
entries.send(name, *args, &block)
end
|
Instance Attribute Details
#added ⇒ Object
Returns the value of attribute added.
9
10
11
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 9
def added
@added
end
|
#loaded ⇒ Object
Returns the value of attribute loaded.
9
10
11
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 9
def loaded
@loaded
end
|
#unloaded ⇒ Object
Returns the value of attribute unloaded.
9
10
11
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 9
def unloaded
@unloaded
end
|
Instance Method Details
#<<(document) ⇒ Object
Also known as:
push
18
19
20
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 18
def <<(document)
added.push(document)
end
|
#==(other) ⇒ Object
13
14
15
16
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 13
def ==(other)
return false unless other.respond_to?(:entries)
entries == other.entries
end
|
#as_json(options = {}) ⇒ Object
136
137
138
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 136
def as_json(options = {})
entries.as_json(options)
end
|
#clear ⇒ Object
23
24
25
26
27
28
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 23
def clear
if block_given?
in_memory { |doc| yield(doc) }
end
loaded.clear and added.clear
end
|
#clone ⇒ Object
30
31
32
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 30
def clone
collect { |doc| doc.clone }
end
|
#delete(document) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 34
def delete(document)
(loaded.delete_one(document) || added.delete_one(document)).tap do |doc|
unless doc
key = document.is_a?(::Mongoid::Document) ? :_id : :id
if unloaded && unloaded.where(key => document.id).exists?
yield(document) if block_given?
return document
end
end
yield(doc) if block_given?
end
end
|
#delete_if(&block) ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 47
def delete_if(&block)
load_all!
tap do
loaded.delete_if(&block)
added.delete_if(&block)
end
end
|
#each ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 55
def each
if loaded?
loaded.each do |doc|
yield(doc)
end
else
unloaded.each do |doc|
document = added.delete(doc) || loaded.delete(doc) || doc
yield(document)
loaded.push(document)
end
end
added.each do |doc|
yield(doc)
end
@executed = true
end
|
#empty? ⇒ Boolean
73
74
75
76
77
78
79
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 73
def empty?
if loaded?
in_memory.count == 0
else
unloaded.count + added.count == 0
end
end
|
#first ⇒ Object
81
82
83
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 81
def first
added.first || (loaded? ? loaded.first : unloaded.first)
end
|
#in_memory ⇒ Object
97
98
99
100
101
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 97
def in_memory
(loaded + added).tap do |docs|
docs.each { |doc| yield(doc) } if block_given?
end
end
|
#inspect ⇒ Object
93
94
95
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 93
def inspect
entries.inspect
end
|
#last ⇒ Object
103
104
105
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 103
def last
added.last || (loaded? ? loaded.last : unloaded.last)
end
|
#loaded? ⇒ Boolean
109
110
111
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 109
def loaded?
!!@executed
end
|
#reset ⇒ Object
113
114
115
116
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 113
def reset
loaded.clear and added.clear
@executed = false
end
|
#respond_to?(name, include_private = false) ⇒ Boolean
118
119
120
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 118
def respond_to?(name, include_private = false)
[].respond_to?(name, include_private) || super
end
|
#size ⇒ Object
Also known as:
length
122
123
124
125
126
127
128
129
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 122
def size
count = (unloaded ? unloaded.count : loaded.count)
if count.zero?
count + added.count
else
count + added.count{ |d| d.new_record? }
end
end
|
#to_json(options = {}) ⇒ Object
132
133
134
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 132
def to_json(options = {})
entries.to_json(options)
end
|
#uniq ⇒ Object
140
141
142
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 140
def uniq
entries.uniq
end
|
#where(opts = :chain, *rest) ⇒ Object
144
145
146
147
148
149
150
|
# File 'lib/active_mongoid/associations/targets/enumerable.rb', line 144
def where(opts = :chain, *rest)
if unloaded.is_a?(::Mongoid::Criteria) || unloaded.is_a?(::ActiveRecord::Relation)
unloaded.where(opts, *rest)
else
raise NoMethodError
end
end
|