Module: MongoMapper::Plugins::Querying::ClassMethods
- Extended by:
- Forwardable
- Defined in:
- lib/mongo_mapper/plugins/querying.rb
Instance Method Summary collapse
- #create(*docs) ⇒ Object
- #create!(*docs) ⇒ Object
- #criteria_hash(criteria = {}) ⇒ Object
- #find_by_id(id) ⇒ Object
- #first_or_create(args) ⇒ Object
- #first_or_new(args) ⇒ Object
- #query(options = {}) ⇒ Object (also: #scoped)
- #update(*args) ⇒ Object
Instance Method Details
#create(*docs) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 26 def create(*docs) initialize_each(*docs) do |doc| yield doc if block_given? doc.save end end |
#create!(*docs) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 33 def create!(*docs) initialize_each(*docs) do |doc| yield doc if block_given? doc.save! end end |
#criteria_hash(criteria = {}) ⇒ Object
60 61 62 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 60 def criteria_hash(criteria={}) Plucky::CriteriaHash.new(criteria, :object_ids => object_id_keys) end |
#find_by_id(id) ⇒ Object
14 15 16 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 14 def find_by_id(id) find_one(:_id => id) end |
#first_or_create(args) ⇒ Object
18 19 20 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 18 def first_or_create(args) first(args) || create(args.reject { |key, value| !key?(key) }) end |
#first_or_new(args) ⇒ Object
22 23 24 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 22 def first_or_new(args) first(args) || new(args.reject { |key, value| !key?(key) }) end |
#query(options = {}) ⇒ Object Also known as: scoped
50 51 52 53 54 55 56 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 50 def query(={}) query = MongoMapper::Plugins::Querying::DecoratedPluckyQuery.new(collection, :transformer => transformer) query.object_ids(object_id_keys) query.amend() query.model(self) query end |
#update(*args) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/mongo_mapper/plugins/querying.rb', line 40 def update(*args) if args.length == 1 update_multiple(args[0]) else id, attributes = args update_single(id, attributes) end end |