Class: RRepo::Adapters::Mongo
- Includes:
- Mongo
- Defined in:
- lib/rrepo/adapters/mongo.rb
Overview
A mongodb adapter
Defined Under Namespace
Classes: Query
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Instance Method Summary collapse
- #all(collection) ⇒ Object
- #clear(collection) ⇒ Object
- #create(collection, model) ⇒ Object
- #delete(collection, model) ⇒ Object
- #find(collection, id) ⇒ Object
-
#initialize(options) ⇒ Mongo
constructor
A new instance of Mongo.
- #query(collection, &block) ⇒ Object
- #update(collection, model) ⇒ Object
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() db_name = .delete(:db) @client = MongoClient.new(.delete(:host), ) @db = @client[db_name] end |
Instance Attribute Details
#db ⇒ Object (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 |