Module: Mongo::Model::QueryMixin::ClassMethods
- Includes:
- DynamicFinders
- Defined in:
- lib/mongo/model/query_mixin.rb
Instance Method Summary collapse
- #all(selector = {}, options = {}, &block) ⇒ Object
- #count(selector = {}, options = {}) ⇒ Object
- #each(selector = {}, options = {}, &block) ⇒ Object
- #exists?(selector = {}, options = {}) ⇒ Boolean (also: #exist?)
- #first(selector = {}, options = {}) ⇒ Object
- #first!(selector = {}, options = {}) ⇒ Object
- #query(*args) ⇒ Object (also: #where)
- #size(*args) ⇒ Object
Instance Method Details
#all(selector = {}, options = {}, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/mongo/model/query_mixin.rb', line 26 def all selector = {}, = {}, &block if block each selector, , &block else list = [] each(selector, ){|doc| list << doc} list end end |
#count(selector = {}, options = {}) ⇒ Object
10 11 12 |
# File 'lib/mongo/model/query_mixin.rb', line 10 def count selector = {}, = {} collection.count selector, end |
#each(selector = {}, options = {}, &block) ⇒ Object
22 23 24 |
# File 'lib/mongo/model/query_mixin.rb', line 22 def each selector = {}, = {}, &block collection.each selector, , &block end |
#exists?(selector = {}, options = {}) ⇒ Boolean Also known as: exist?
40 41 42 |
# File 'lib/mongo/model/query_mixin.rb', line 40 def exists? selector = {}, = {} count(selector, ) > 0 end |
#first(selector = {}, options = {}) ⇒ Object
18 19 20 |
# File 'lib/mongo/model/query_mixin.rb', line 18 def first selector = {}, = {} collection.first selector, end |
#first!(selector = {}, options = {}) ⇒ Object
36 37 38 |
# File 'lib/mongo/model/query_mixin.rb', line 36 def first! selector = {}, = {} first(selector, ) || raise(Mongo::NotFound, "document with selector #{selector} not found!") end |
#query(*args) ⇒ Object Also known as: where
45 46 47 48 49 50 51 52 |
# File 'lib/mongo/model/query_mixin.rb', line 45 def query *args if args.first.is_a? Mongo::Model::Query args.first else selector, = args.first.is_a?(::Array) ? args.first : args Mongo::Model::Query.new self, (selector || {}), ( || {}) end end |
#size(*args) ⇒ Object
14 15 16 |
# File 'lib/mongo/model/query_mixin.rb', line 14 def size *args count *args end |