Class: RRepo::Adapters::Mongo

Inherits:
Base
  • Object
show all
Includes:
Mongo
Defined in:
lib/rrepo/adapters/mongo.rb

Overview

A mongodb adapter

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Mongo

Returns a new instance of Mongo.



8
9
10
11
12
# File 'lib/rrepo/adapters/mongo.rb', line 8

def initialize(options)
  db_name = options.delete(:db)
  @client = MongoClient.new(options.delete(:host), options)
  @db = @client[db_name]
end

Instance Method Details

#all(collection) ⇒ Object



28
29
30
# File 'lib/rrepo/adapters/mongo.rb', line 28

def all(collection)
  @db[collection.to_s].find
end

#clear(collection) ⇒ Object



36
37
38
# File 'lib/rrepo/adapters/mongo.rb', line 36

def clear(collection)
  @db[collection].drop
end

#create(collection, model) ⇒ Object



14
15
16
# File 'lib/rrepo/adapters/mongo.rb', line 14

def create(collection, model)
  @db[collection.to_s].insert(model.to_hash)
end

#delete(collection, model) ⇒ Object



23
24
25
26
# File 'lib/rrepo/adapters/mongo.rb', line 23

def delete(collection, model)
  id = model.to_hash[:_id]
  @db[collection.to_s].remove(id_query(id))
end

#find(collection, id) ⇒ Object



32
33
34
# File 'lib/rrepo/adapters/mongo.rb', line 32

def find(collection, id)
  @db[collection].find(id_query(id))
end

#update(collection, model) ⇒ Object



18
19
20
21
# File 'lib/rrepo/adapters/mongo.rb', line 18

def update(collection, model)
  hash = model.to_hash
  @db[collection.to_s].update(id_query(hash.delete(:_id)), hash)
end