Module: MongoModel::Scope::FinderMethods
- Included in:
- MongoModel::Scope
- Defined in:
- lib/mongomodel/support/scope/finder_methods.rb
Instance Method Summary collapse
- #all ⇒ Object
- #apply_finder_options(options = {}) ⇒ Object
- #exists?(id) ⇒ Boolean
- #find(*ids, &block) ⇒ Object
- #first(count = nil) ⇒ Object
- #last(count = nil) ⇒ Object
Instance Method Details
#all ⇒ Object
41 42 43 |
# File 'lib/mongomodel/support/scope/finder_methods.rb', line 41 def all to_a end |
#apply_finder_options(options = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/mongomodel/support/scope/finder_methods.rb', line 49 def (={}) result = clone result = result.where([:conditions]) if [:conditions] result = result.order([:order]) if [:order] result = result.select([:select]) if [:select] result = result.limit([:limit]) if [:limit] result = result.offset([:offset]) if [:offset] result end |
#exists?(id) ⇒ Boolean
45 46 47 |
# File 'lib/mongomodel/support/scope/finder_methods.rb', line 45 def exists?(id) where(:id => id).any? end |
#find(*ids, &block) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/mongomodel/support/scope/finder_methods.rb', line 4 def find(*ids, &block) if block_given? to_a.find(&block) else ids.flatten! case ids.size when 0 raise ArgumentError, "At least one id must be specified" when 1 id = ids.first where(:id => id).first || raise(DocumentNotFound, "Couldn't find document with id: #{id}") else ids = ids.map { |id| Reference.cast(id) } docs = where(:id.in => ids).to_a raise DocumentNotFound if docs.size != ids.size docs.sort_by { |doc| ids.index(doc.id) } end end end |
#first(count = nil) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/mongomodel/support/scope/finder_methods.rb', line 25 def first(count=nil) if loaded? count ? to_a.first(count) : to_a.first else count ? limit(count).to_a : limit(1).to_a[0] end end |
#last(count = nil) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/mongomodel/support/scope/finder_methods.rb', line 33 def last(count=nil) if loaded? count ? to_a.last(count) : to_a.last else count ? reverse_order.limit(count).to_a : reverse_order.limit(1).to_a[0] end end |