Class: RRepo::Adapters::Mongo

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

Overview

A mongodb adapter

Defined Under Namespace

Classes: Query

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



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

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

#clear(collection) ⇒ Object



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

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
# File 'lib/rrepo/adapters/mongo.rb', line 23

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

#find(collection, id) ⇒ Object



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

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

#query(collection, &block) ⇒ Object



39
40
41
# File 'lib/rrepo/adapters/mongo.rb', line 39

def query(collection, &block)
  Query.new(@db[collection], &block)
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(model._id), hash)
end