Class: MongoModel::Scope
- Inherits:
-
Object
show all
- Includes:
- ArrayMethods, Batches, DynamicFinders, FinderMethods, LoadMethods, Pagination, QueryMethods, SpawnMethods
- Defined in:
- lib/mongomodel/support/scope.rb,
lib/mongomodel/support/scope/batches.rb,
lib/mongomodel/support/scope/pagination.rb,
lib/mongomodel/support/scope/load_methods.rb,
lib/mongomodel/support/scope/array_methods.rb,
lib/mongomodel/support/scope/query_methods.rb,
lib/mongomodel/support/scope/spawn_methods.rb,
lib/mongomodel/support/scope/finder_methods.rb,
lib/mongomodel/support/scope/dynamic_finders.rb
Defined Under Namespace
Modules: ArrayMethods, Batches, DynamicFinders, FinderMethods, LoadMethods, Pagination, QueryMethods, SpawnMethods
Constant Summary
collapse
- MULTI_VALUE_METHODS =
[ :select, :order, :where ]
- SINGLE_VALUE_METHODS =
[ :limit, :offset, :from ]
Instance Attribute Summary collapse
Attributes included from LoadMethods
#on_load_proc
Instance Method Summary
collapse
Methods included from Batches
#in_batches
Methods included from Pagination
#paginate
#on_load
#all, #apply_finder_options, #exists?, #find, #first, #last
#any?, #select
#from, #reverse_order
#except, #merge
Constructor Details
#initialize(klass) ⇒ Scope
Returns a new instance of Scope.
23
24
25
26
27
28
29
30
|
# File 'lib/mongomodel/support/scope.rb', line 23
def initialize(klass)
super
@klass = klass
@loaded = false
@documents = []
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/mongomodel/support/scope.rb', line 149
def method_missing(method, *args, &block)
if Array.method_defined?(method)
to_a.send(method, *args, &block)
elsif klass.scopes[method]
merge(klass.send(method, *args, &block))
elsif klass.respond_to?(method)
with_scope { klass.send(method, *args, &block) }
else
super
end
end
|
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
21
22
23
|
# File 'lib/mongomodel/support/scope.rb', line 21
def klass
@klass
end
|
Instance Method Details
#==(other) ⇒ Object
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/mongomodel/support/scope.rb', line 115
def ==(other)
case other
when Scope
klass == other.klass &&
collection == other.collection &&
finder_options == other.finder_options
when Array
to_a == other.to_a
end
end
|
#build(*args, &block) ⇒ Object
36
37
38
|
# File 'lib/mongomodel/support/scope.rb', line 36
def build(*args, &block)
new(*args, &block)
end
|
#collection ⇒ Object
126
127
128
|
# File 'lib/mongomodel/support/scope.rb', line 126
def collection
from_value || klass.collection
end
|
#count ⇒ Object
57
58
59
|
# File 'lib/mongomodel/support/scope.rb', line 57
def count
_find.count
end
|
#delete(*ids) ⇒ Object
77
78
79
80
|
# File 'lib/mongomodel/support/scope.rb', line 77
def delete(*ids)
where(ids_to_conditions(ids)).delete_all
reset
end
|
#delete_all ⇒ Object
71
72
73
74
75
|
# File 'lib/mongomodel/support/scope.rb', line 71
def delete_all
selector = MongoOptions.new(klass, :conditions => finder_conditions).selector
collection.remove(selector, {})
reset
end
|
#destroy(*ids) ⇒ Object
66
67
68
69
|
# File 'lib/mongomodel/support/scope.rb', line 66
def destroy(*ids)
where(ids_to_conditions(ids)).destroy_all
reset
end
|
#destroy_all ⇒ Object
61
62
63
64
|
# File 'lib/mongomodel/support/scope.rb', line 61
def destroy_all
to_a.each { |doc| doc.destroy }
reset
end
|
53
54
55
|
# File 'lib/mongomodel/support/scope.rb', line 53
def empty?
loaded? ? @documents.empty? : count.zero?
end
|
#finder_options ⇒ Object
130
131
132
133
134
135
136
137
138
|
# File 'lib/mongomodel/support/scope.rb', line 130
def finder_options
@finder_options ||= {}.tap do |result|
result[:conditions] = finder_conditions if where_values.any?
result[:select] = select_values if select_values.any?
result[:order] = order_values if order_values.any?
result[:limit] = limit_value if limit_value.present?
result[:offset] = offset_value if offset_value.present?
end
end
|
#initialize_copy(other) ⇒ Object
32
33
34
|
# File 'lib/mongomodel/support/scope.rb', line 32
def initialize_copy(other)
reset
end
|
97
98
99
|
# File 'lib/mongomodel/support/scope.rb', line 97
def loaded?
@loaded
end
|
#options_for_create ⇒ Object
140
141
142
|
# File 'lib/mongomodel/support/scope.rb', line 140
def options_for_create
@options_for_create ||= finder_conditions.reject { |k, v| k.is_a?(MongoModel::MongoOperator) || k.respond_to?(:to_mongo_operator) }
end
|
#reload ⇒ Object
101
102
103
104
105
|
# File 'lib/mongomodel/support/scope.rb', line 101
def reload
reset
to_a
self
end
|
#reset ⇒ Object
107
108
109
110
111
112
113
|
# File 'lib/mongomodel/support/scope.rb', line 107
def reset
@loaded = nil
@documents = []
@finder_options = nil
@options_for_create = nil
self
end
|
#respond_to?(method, include_private = false) ⇒ Boolean
144
145
146
|
# File 'lib/mongomodel/support/scope.rb', line 144
def respond_to?(method, include_private = false)
Array.method_defined?(method) || klass.respond_to?(method, include_private) || super
end
|
#size ⇒ Object
49
50
51
|
# File 'lib/mongomodel/support/scope.rb', line 49
def size
loaded? ? @documents.size : count
end
|
#to_a ⇒ Object
40
41
42
43
44
45
46
47
|
# File 'lib/mongomodel/support/scope.rb', line 40
def to_a
return @documents if loaded?
@documents = _find_and_instantiate
@loaded = true
@documents
end
|
#update(ids, updates) ⇒ Object
92
93
94
95
|
# File 'lib/mongomodel/support/scope.rb', line 92
def update(ids, updates)
where(ids_to_conditions(ids)).update_all(updates)
reset
end
|
#update_all(updates) ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'lib/mongomodel/support/scope.rb', line 82
def update_all(updates)
if updates.any?
selector = MongoOptions.new(klass, :conditions => finder_conditions).selector
collection.update(selector, { "$set" => updates }, { :multi => true })
reset
else
self
end
end
|