Module: MongoMapper::Plugins::Querying::ClassMethods
- Includes:
- PluckyMethods
- Defined in:
- lib/mongo_mapper/plugins/querying.rb
Instance Method Summary collapse
- #create(*docs) ⇒ Object
- #create!(*docs) ⇒ Object
- #criteria_hash(criteria = {}) ⇒ Object
- #delete(*ids) ⇒ Object
- #delete_all(options = {}) ⇒ Object
- #destroy(*ids) ⇒ Object
- #destroy_all(options = {}) ⇒ Object
- #find_by_id(id) ⇒ Object
- #find_each(opts = {}) ⇒ Object
- #first_or_create(args) ⇒ Object
- #first_or_new(args) ⇒ Object
- #query(options = {}) ⇒ Object
- #update(*args) ⇒ Object
Instance Method Details
#create(*docs) ⇒ Object
29 30 31 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 29 def create(*docs) initialize_each(*docs) { |doc| doc.save } end |
#create!(*docs) ⇒ Object
33 34 35 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 33 def create!(*docs) initialize_each(*docs) { |doc| doc.save! } end |
#criteria_hash(criteria = {}) ⇒ Object
73 74 75 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 73 def criteria_hash(criteria={}) Plucky::CriteriaHash.new(criteria, :object_ids => object_id_keys) end |
#delete(*ids) ⇒ Object
46 47 48 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 46 def delete(*ids) query(:_id => ids.flatten).remove end |
#delete_all(options = {}) ⇒ Object
50 51 52 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 50 def delete_all(={}) query().remove end |
#destroy(*ids) ⇒ Object
54 55 56 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 54 def destroy(*ids) find_some!(ids.flatten).each { |doc| doc.destroy } end |
#destroy_all(options = {}) ⇒ Object
58 59 60 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 58 def destroy_all(={}) find_each() { |document| document.destroy } end |
#find_by_id(id) ⇒ Object
17 18 19 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 17 def find_by_id(id) find_one(:_id => id) end |
#find_each(opts = {}) ⇒ Object
13 14 15 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 13 def find_each(opts={}) super(opts).each { |doc| yield load(doc) } end |
#first_or_create(args) ⇒ Object
21 22 23 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 21 def first_or_create(args) first(args) || create(args.reject { |key, value| !key?(key) }) end |
#first_or_new(args) ⇒ Object
25 26 27 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 25 def first_or_new(args) first(args) || new(args.reject { |key, value| !key?(key) }) end |
#query(options = {}) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 63 def query(={}) Plucky::Query.new(collection).tap do |query| query.extend(Decorator) query.object_ids(object_id_keys) query.update() query.model(self) end end |
#update(*args) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 37 def update(*args) if args.length == 1 update_multiple(args[0]) else id, attributes = args update_single(id, attributes) end end |