Module: MongoMapper::Plugins::Querying::ClassMethods
- Defined in:
- lib/mongo_mapper/plugins/querying.rb
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #count(options = {}) ⇒ Object
- #create(*docs) ⇒ Object
- #create!(*docs) ⇒ Object
- #criteria_hash(criteria = {}) ⇒ Object
- #delete(*ids) ⇒ Object
- #delete_all(options = {}) ⇒ Object
- #destroy(*ids) ⇒ Object
- #destroy_all(options = {}) ⇒ Object
- #exists?(options = {}) ⇒ Boolean
- #fields(*args) ⇒ Object
- #find(*args) ⇒ Object
- #find!(*args) ⇒ Object
- #find_by_id(id) ⇒ Object
- #find_each(options = {}) ⇒ Object
- #first(options = {}) ⇒ Object
- #first_or_create(args) ⇒ Object
- #first_or_new(args) ⇒ Object
-
#last(options = {}) ⇒ Object
All bets are off an actual order if you provide none.
- #limit(*args) ⇒ Object
- #options_hash(options = {}) ⇒ Object
- #query(options = {}) ⇒ Object
- #skip(*args) ⇒ Object
- #sort(*args) ⇒ Object
- #update(*args) ⇒ Object
- #where(options = {}) ⇒ Object
Instance Method Details
#all(options = {}) ⇒ Object
46 47 48 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 46 def all(={}) find_many() end |
#count(options = {}) ⇒ Object
50 51 52 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 50 def count(={}) query().count end |
#create(*docs) ⇒ Object
66 67 68 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 66 def create(*docs) initialize_each(*docs) { |doc| doc.save } end |
#create!(*docs) ⇒ Object
70 71 72 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 70 def create!(*docs) initialize_each(*docs) { |doc| doc.save! } end |
#criteria_hash(criteria = {}) ⇒ Object
125 126 127 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 125 def criteria_hash(criteria={}) Plucky::CriteriaHash.new(criteria, :object_ids => object_id_keys) end |
#delete(*ids) ⇒ Object
83 84 85 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 83 def delete(*ids) query(:_id => ids.flatten).query.remove end |
#delete_all(options = {}) ⇒ Object
87 88 89 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 87 def delete_all(={}) query().query.remove end |
#destroy(*ids) ⇒ Object
91 92 93 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 91 def destroy(*ids) find_some!(ids.flatten).each(&:destroy) end |
#destroy_all(options = {}) ⇒ Object
95 96 97 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 95 def destroy_all(={}) find_each() { |document| document.destroy } end |
#exists?(options = {}) ⇒ Boolean
54 55 56 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 54 def exists?(={}) !count().zero? end |
#fields(*args) ⇒ Object
103 104 105 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 103 def fields(*args) query.fields(*args) end |
#find(*args) ⇒ Object
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 5 def find(*args) = args. return nil if args.size == 0 if args.first.is_a?(Array) || args.size > 1 find_some(args, ) else find_one(.merge(:_id => args[0])) end end |
#find!(*args) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 16 def find!(*args) = args. raise DocumentNotFound, "Couldn't find without an ID" if args.size == 0 if args.first.is_a?(Array) || args.size > 1 find_some!(args, ) else find_one(.merge(:_id => args[0])) || raise(DocumentNotFound, "Document match #{.inspect} does not exist in #{collection.name} collection") end end |
#find_by_id(id) ⇒ Object
33 34 35 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 33 def find_by_id(id) find_one(:_id => id) end |
#find_each(options = {}) ⇒ Object
29 30 31 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 29 def find_each(={}) query().query.find().each { |doc| yield load(doc) } end |
#first(options = {}) ⇒ Object
37 38 39 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 37 def first(={}) find_one() end |
#first_or_create(args) ⇒ Object
58 59 60 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 58 def first_or_create(args) first(args) || create(args.reject { |key, value| !key?(key) }) end |
#first_or_new(args) ⇒ Object
62 63 64 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 62 def first_or_new(args) first(args) || new(args.reject { |key, value| !key?(key) }) end |
#last(options = {}) ⇒ Object
All bets are off an actual order if you provide none.
42 43 44 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 42 def last(={}) find_one(query().reverse.to_hash) end |
#limit(*args) ⇒ Object
107 108 109 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 107 def limit(*args) query.limit(*args) end |
#options_hash(options = {}) ⇒ Object
130 131 132 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 130 def (={}) Plucky::OptionsHash.new() end |
#query(options = {}) ⇒ Object
120 121 122 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 120 def query(={}) Query.new(self).update() end |
#skip(*args) ⇒ Object
111 112 113 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 111 def skip(*args) query.skip(*args) end |
#sort(*args) ⇒ Object
115 116 117 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 115 def sort(*args) query.sort(*args) end |
#update(*args) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 74 def update(*args) if args.length == 1 update_multiple(args[0]) else id, attributes = args update_single(id, attributes) end end |
#where(options = {}) ⇒ Object
99 100 101 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 99 def where(={}) query.where() end |