Module: MongoLight::Document::ClassMethods
- Defined in:
- lib/mongo_light/document.rb
Instance Method Summary collapse
- #collection ⇒ Object
- #count(selector = {}, collection = nil) ⇒ Object
- #find(selector = {}, opts = {}, collection = nil) ⇒ Object
- #find_and_modify(options = {}, collection = nil) ⇒ Object
- #find_by_id(id) ⇒ Object
- #find_one(selector = {}, opts = {}) ⇒ Object
- #insert(document, options = {}, collection = nil) ⇒ Object
- #remove(selector = {}, options = {}, collection = nil) ⇒ Object
- #update(selector, document, options = {}, collection = nil) ⇒ Object
Instance Method Details
#collection ⇒ Object
7 8 9 |
# File 'lib/mongo_light/document.rb', line 7 def collection Connection[self.to_s.tableize] end |
#count(selector = {}, collection = nil) ⇒ Object
52 53 54 55 |
# File 'lib/mongo_light/document.rb', line 52 def count(selector={}, collection = nil) c = collection || self.collection c.find(map(selector)).count end |
#find(selector = {}, opts = {}, collection = nil) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/mongo_light/document.rb', line 23 def find(selector = {}, opts = {}, collection = nil) raw = opts.delete(:raw) || false opts[:transformer] = Proc.new{|data| raw ? unmap(data, raw) : self.new(unmap(data)) } c = collection || self.collection c.find(map(selector), (opts)) end |
#find_and_modify(options = {}, collection = nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mongo_light/document.rb', line 29 def find_and_modify( = {}, collection = nil) raw = .delete(:raw) || false [:query] = map([:query]) if [:query] [:update] = map([:update]) if [:update] [:sort] = ([:sort]) if [:sort] [:fields] = ([:fields]) if [:fields] c = collection || self.collection found = c.find_and_modify() return nil if found.nil? raw ? unmap(found) : self.new(unmap(found)) end |
#find_by_id(id) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/mongo_light/document.rb', line 10 def find_by_id(id) real_id = Id.from_string(id) return nil if real_id.nil? found = collection.find_one(real_id) found.nil? ? nil : self.new(unmap(found)) end |
#find_one(selector = {}, opts = {}) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/mongo_light/document.rb', line 17 def find_one(selector = {}, opts = {}) raw = opts.delete(:raw) || false found = collection.find_one(map(selector), (opts)) return nil if found.nil? return raw ? unmap(found, true) : self.new(unmap(found)) end |
#insert(document, options = {}, collection = nil) ⇒ Object
44 45 46 47 |
# File 'lib/mongo_light/document.rb', line 44 def insert(document, = {}, collection = nil) c = collection || self.collection c.insert(map(document), ) end |
#remove(selector = {}, options = {}, collection = nil) ⇒ Object
40 41 42 43 |
# File 'lib/mongo_light/document.rb', line 40 def remove(selector = {}, = {}, collection = nil) c = collection || self.collection c.remove(map(selector), ) end |
#update(selector, document, options = {}, collection = nil) ⇒ Object
48 49 50 51 |
# File 'lib/mongo_light/document.rb', line 48 def update(selector, document, = {}, collection = nil) c = collection || self.collection c.update(map(selector), map(document), ) end |