Class: Juno::Adapters::Mongo
Overview
MongoDB backend
Instance Method Summary collapse
- #clear(options = {}) ⇒ Object
- #delete(key, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Mongo
constructor
Constructor.
- #key?(key, options = {}) ⇒ Boolean
- #load(key, options = {}) ⇒ Object
- #store(key, value, options = {}) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(options = {}) ⇒ Mongo
Constructor
Options:
-
:collection - MongoDB collection name (default juno)
-
:host - MongoDB server host (default localhost)
-
:port - MongoDB server port (default mongodb default port)
-
:db - MongoDB database (default juno)
17 18 19 20 21 22 23 24 |
# File 'lib/juno/adapters/mongo.rb', line 17 def initialize( = {}) collection = .delete(:collection) || 'juno' host = .delete(:host) || 'localhost' port = .delete(:port) || ::Mongo::Connection::DEFAULT_PORT db = .delete(:db) || 'juno' connection = ::Mongo::Connection.new(host, port, ) @collection = connection.db(db).collection(collection) end |
Instance Method Details
#clear(options = {}) ⇒ Object
48 49 50 51 |
# File 'lib/juno/adapters/mongo.rb', line 48 def clear( = {}) @collection.remove self end |
#delete(key, options = {}) ⇒ Object
35 36 37 38 39 |
# File 'lib/juno/adapters/mongo.rb', line 35 def delete(key, = {}) value = load(key, ) @collection.remove('_id' => key) if value value end |
#key?(key, options = {}) ⇒ Boolean
26 27 28 |
# File 'lib/juno/adapters/mongo.rb', line 26 def key?(key, = {}) !!load(key, ) end |
#load(key, options = {}) ⇒ Object
30 31 32 33 |
# File 'lib/juno/adapters/mongo.rb', line 30 def load(key, = {}) value = @collection.find_one('_id' => key) value ? value['value'].to_s : nil end |
#store(key, value, options = {}) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/juno/adapters/mongo.rb', line 41 def store(key, value, = {}) @collection.update({ '_id' => key }, { '_id' => key, 'value' => ::BSON::Binary.new(value) }, { :upsert => true }) value end |