Module: MiniMongo::Mapper::ClassMethods

Defined in:
lib/mini_mongo/mapper.rb

Instance Method Summary collapse

Instance Method Details

#collectionObject



57
58
59
# File 'lib/mini_mongo/mapper.rb', line 57

def collection
  self.class_variable_get(:@@collection)
end

#collection=(collection) ⇒ Object



61
62
63
# File 'lib/mini_mongo/mapper.rb', line 61

def collection=(collection)
  self.class_variable_set(:@@collection, collection)
end

#collection_nameObject



65
66
67
# File 'lib/mini_mongo/mapper.rb', line 65

def collection_name
  self.class_variable_get(:@@collection_name)
end

#collection_name=(collection_name) ⇒ Object



69
70
71
# File 'lib/mini_mongo/mapper.rb', line 69

def collection_name=(collection_name)
  self.class_variable_set(:@@collection_name, collection_name)
end

#countObject



44
45
46
# File 'lib/mini_mongo/mapper.rb', line 44

def count
  self.collection.count
end

#find(attrs = {}, opts = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mini_mongo/mapper.rb', line 11

def find(attrs={}, opts={})
  attrs["_id"] = BSON::ObjectId(attrs["id"]) if attrs["id"]
  attrs.delete("id")
  docs = self.collection.find(attrs, opts).to_a
  if docs.empty?
    raise DocumentNotFound,
    "Couldn't find #{self.collection_name.capitalize}, "
    "with #{attrs.to_a.collect {|p| p.join(' = ')}.join(', ')}"
    "and #{opts.to_a.collect {|p| p.join(' = ')}.join(', ')}"
  else
    result = []
    docs.each do |doc|
      result << self.class_eval("new(#{doc})") if doc
    end
    result
  end
end

#insert(attrs = {}) ⇒ Object



29
30
31
32
33
34
# File 'lib/mini_mongo/mapper.rb', line 29

def insert(attrs={})
  doc = {}
  doc["_id"] = self.collection.insert(attrs).to_s
  doc.merge!(attrs)
  self.new(doc)
end

#maps(name) ⇒ Object



52
53
54
55
# File 'lib/mini_mongo/mapper.rb', line 52

def maps(name)
  self.collection_name = name.to_s
  self.collection = MiniMongo.db_connection.collection(name.to_s)
end

#remove(id) ⇒ Object



40
41
42
# File 'lib/mini_mongo/mapper.rb', line 40

def remove(id)
  self.collection.remove("_id" => BSON::ObjectId(id))
end

#remove_allObject



48
49
50
# File 'lib/mini_mongo/mapper.rb', line 48

def remove_all
  self.collection.remove
end

#update(id, attrs = {}) ⇒ Object



36
37
38
# File 'lib/mini_mongo/mapper.rb', line 36

def update(id, attrs={})
  self.collection.update({"_id" => BSON::ObjectId(id)}, attrs)
end