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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Mongo

Returns a new instance of Mongo.



11
12
13
14
15
# File 'lib/rrepo/adapters/mongo.rb', line 11

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

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



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

def db
  @db
end

Instance Method Details

#all(collection) ⇒ Object



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

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

#clear(collection) ⇒ Object



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

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

#create(collection, model) ⇒ Object



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

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

#delete(collection, model) ⇒ Object



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

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

#find(collection, id) ⇒ Object



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

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

#query(collection, &block) ⇒ Object



42
43
44
# File 'lib/rrepo/adapters/mongo.rb', line 42

def query(collection, &block)
  Query.new(@db[collection], &block)
end

#update(collection, model) ⇒ Object



21
22
23
24
# File 'lib/rrepo/adapters/mongo.rb', line 21

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