Class: Juno::Adapters::Mongo

Inherits:
Base
  • Object
show all
Defined in:
lib/juno/adapters/mongo.rb

Overview

MongoDB backend

Instance Method Summary collapse

Methods inherited from Base

#[], #[]=, #close, #fetch

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(options = {})
  collection = options.delete(:collection) || 'juno'
  host = options.delete(:host) || 'localhost'
  port = options.delete(:port) || ::Mongo::Connection::DEFAULT_PORT
  db = options.delete(:db) || 'juno'
  connection = ::Mongo::Connection.new(host, port, options)
  @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(options = {})
  @collection.remove
  self
end

#delete(key, options = {}) ⇒ Object



35
36
37
38
39
# File 'lib/juno/adapters/mongo.rb', line 35

def delete(key, options = {})
  value = load(key, options)
  @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, options = {})
  !!load(key, options)
end

#load(key, options = {}) ⇒ Object



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

def load(key, options = {})
  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, options = {})
  @collection.update({ '_id' => key },
                     { '_id' => key, 'value' => ::BSON::Binary.new(value) },
                     { :upsert => true })
  value
end