class MongoModel::Scope
Constants
- MULTI_VALUE_METHODS
- SINGLE_VALUE_METHODS
Attributes
klass[R]
Public Class Methods
new(klass)
click to toggle source
Calls superclass method
# File lib/mongomodel/support/scope.rb, line 23 def initialize(klass) super @klass = klass @loaded = false @documents = [] end
Public Instance Methods
==(other)
click to toggle source
# 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)
click to toggle source
# File lib/mongomodel/support/scope.rb, line 36 def build(*args, &block) new(*args, &block) end
collection()
click to toggle source
# File lib/mongomodel/support/scope.rb, line 126 def collection from_value || klass.collection end
count()
click to toggle source
# File lib/mongomodel/support/scope.rb, line 57 def count _find.count end
delete(*ids)
click to toggle source
# File lib/mongomodel/support/scope.rb, line 77 def delete(*ids) where(ids_to_conditions(ids)).delete_all reset end
delete_all()
click to toggle source
# 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)
click to toggle source
# File lib/mongomodel/support/scope.rb, line 66 def destroy(*ids) where(ids_to_conditions(ids)).destroy_all reset end
destroy_all()
click to toggle source
# File lib/mongomodel/support/scope.rb, line 61 def destroy_all to_a.each { |doc| doc.destroy } reset end
empty?()
click to toggle source
# File lib/mongomodel/support/scope.rb, line 53 def empty? loaded? ? @documents.empty? : count.zero? end
finder_options()
click to toggle source
# 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)
click to toggle source
# File lib/mongomodel/support/scope.rb, line 32 def initialize_copy(other) reset end
loaded?()
click to toggle source
# File lib/mongomodel/support/scope.rb, line 97 def loaded? @loaded end
options_for_create()
click to toggle source
# 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()
click to toggle source
# File lib/mongomodel/support/scope.rb, line 101 def reload reset to_a self end
reset()
click to toggle source
# 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)
click to toggle source
Calls superclass method
# 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()
click to toggle source
# File lib/mongomodel/support/scope.rb, line 49 def size loaded? ? @documents.size : count end
to_a()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
Protected Instance Methods
method_missing(method, *args, &block)
click to toggle source
Calls superclass method
# 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
Private Instance Methods
_find()
click to toggle source
# File lib/mongomodel/support/scope.rb, line 162 def _find ensure_indexes! selector, options = MongoOptions.new(klass, finder_options).to_a collection.find(selector, options) end
_find_and_instantiate()
click to toggle source
# File lib/mongomodel/support/scope.rb, line 168 def _find_and_instantiate _find.to_a.map { |doc| klass.from_mongo(doc).tap { |instance| on_load_proc.call(instance) if on_load_proc } } end
ensure_indexes!()
click to toggle source
# File lib/mongomodel/support/scope.rb, line 194 def ensure_indexes! if klass.respond_to?(:indexes_initialized?) && !klass.indexes_initialized? klass.ensure_indexes! end end
finder_conditions()
click to toggle source
# File lib/mongomodel/support/scope.rb, line 176 def finder_conditions where_values.inject({}) { |conditions, v| conditions.merge(v) } end
ids_to_conditions(ids)
click to toggle source
# File lib/mongomodel/support/scope.rb, line 184 def ids_to_conditions(ids) ids = Array.wrap(ids).flatten if ids.size == 1 { :id => ids.first.to_s } else { :id.in => ids.map { |id| id.to_s } } end end
with_scope(&block)
click to toggle source
# File lib/mongomodel/support/scope.rb, line 180 def with_scope(&block) klass.send(:with_scope, self, &block) end