Module: MongoLight::Document::ClassMethods

Defined in:
lib/mongo_light/document.rb

Instance Method Summary collapse

Instance Method Details

#collectionObject



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), map_options(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(options = {}, collection = nil)
  raw = options.delete(:raw) || false
  options[:query] = map(options[:query]) if options[:query]
  options[:update] = map(options[:update]) if options[:update]
  options[:sort] = map_options(options[:sort]) if options[:sort]
  options[:fields] = map_options(options[:fields]) if options[:fields]
  c = collection || self.collection
  found = c.find_and_modify(options)
  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), map_options(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, options = {}, collection = nil)
  c = collection || self.collection
  c.insert(map(document), options)
end

#remove(selector = {}, options = {}, collection = nil) ⇒ Object



40
41
42
43
# File 'lib/mongo_light/document.rb', line 40

def remove(selector = {}, options = {}, collection = nil)
  c = collection || self.collection
  c.remove(map(selector), options)
end

#update(selector, document, options = {}, collection = nil) ⇒ Object



48
49
50
51
# File 'lib/mongo_light/document.rb', line 48

def update(selector, document, options = {}, collection = nil)
  c = collection || self.collection
  c.update(map(selector), map(document), options)
end