Module: MiniMongo::Mapper::ClassMethods

Defined in:
lib/mini_mongo/mapper.rb

Instance Method Summary collapse

Instance Method Details

#collectionObject



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

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

#collection=(collection) ⇒ Object



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

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

#collection_nameObject



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

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

#collection_name=(collection_name) ⇒ Object



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

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

#countObject



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

def count
  self.collection.count
end

#find(attrs = {}) ⇒ Object



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

def find(attrs={})
  attrs["_id"] = BSON::ObjectId(attrs["id"]) if attrs["id"]
  attrs.delete("id")
  docs = self.collection.find(attrs).to_a
  if docs.empty?
    raise DocumentNotFound, "Couldn't find #{self.collection_name.capitalize}, with #{attrs.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



26
27
28
29
30
31
# File 'lib/mini_mongo/mapper.rb', line 26

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

#maps(name) ⇒ Object



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

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

#remove(id) ⇒ Object



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

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

#remove_allObject



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

def remove_all
  self.collection.remove
end

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



33
34
35
# File 'lib/mini_mongo/mapper.rb', line 33

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