Class: Volt::DataStore::MongoDriver
- Defined in:
- lib/volt/data_stores/mongo_driver.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#mongo_db ⇒ Object
readonly
Returns the value of attribute mongo_db.
Instance Method Summary collapse
- #delete(collection, query) ⇒ Object
- #drop_database ⇒ Object
-
#initialize ⇒ MongoDriver
constructor
A new instance of MongoDriver.
- #insert(collection, values) ⇒ Object
- #query(collection, query) ⇒ Object
- #update(collection, values) ⇒ Object
Constructor Details
#initialize ⇒ MongoDriver
Returns a new instance of MongoDriver.
9 10 11 12 13 14 15 16 17 |
# File 'lib/volt/data_stores/mongo_driver.rb', line 9 def initialize if Volt.config.db_uri.present? @mongo_db ||= Mongo::MongoClient.from_uri(Volt.config.db_uri) @db ||= @mongo_db.db(Volt.config.db_uri.split('/').last || Volt.config.db_name) else @mongo_db ||= Mongo::MongoClient.new(Volt.config.db_host, Volt.config.db_path) @db ||= @mongo_db.db(Volt.config.db_name) end end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
7 8 9 |
# File 'lib/volt/data_stores/mongo_driver.rb', line 7 def db @db end |
#mongo_db ⇒ Object (readonly)
Returns the value of attribute mongo_db.
7 8 9 |
# File 'lib/volt/data_stores/mongo_driver.rb', line 7 def mongo_db @mongo_db end |
Instance Method Details
#delete(collection, query) ⇒ Object
60 61 62 |
# File 'lib/volt/data_stores/mongo_driver.rb', line 60 def delete(collection, query) @db[collection].remove(query) end |
#drop_database ⇒ Object
64 65 66 |
# File 'lib/volt/data_stores/mongo_driver.rb', line 64 def drop_database db.connection.drop_database(Volt.config.db_name) end |
#insert(collection, values) ⇒ Object
19 20 21 |
# File 'lib/volt/data_stores/mongo_driver.rb', line 19 def insert(collection, values) @db[collection].insert(values) end |
#query(collection, query) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/volt/data_stores/mongo_driver.rb', line 42 def query(collection, query) allowed_methods = ['find', 'skip', 'limit'] cursor = @db[collection] query.each do |query_part| method_name, *args = query_part unless allowed_methods.include?(method_name.to_s) raise "`#{method_name}` is not part of a valid query" end cursor = cursor.send(method_name, *args) end cursor.to_a end |
#update(collection, values) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/volt/data_stores/mongo_driver.rb', line 23 def update(collection, values) # TODO: Seems mongo is dumb and doesn't let you upsert with custom id's begin @db[collection].insert(values) rescue Mongo::OperationFailure => error # Really mongo client? if error.[/^11000[:]/] # Update because the id already exists update_values = values.dup id = update_values.delete(:_id) @db[collection].update({ _id: id }, update_values) else return { error: error. } end end return nil end |