Class: Moneta::Adapters::Couch
- Inherits:
-
Object
- Object
- Moneta::Adapters::Couch
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/couch.rb
Overview
CouchDB backend
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#initialize(options = {}) ⇒ Couch
constructor
A new instance of Couch.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from Defaults
#[], #[]=, #close, #decrement, #fetch, #increment
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ Couch
Returns a new instance of Couch.
12 13 14 15 |
# File 'lib/moneta/adapters/couch.rb', line 12 def initialize( = {}) [:db] ||= 'moneta' @db = ::CouchRest.database!([:db]) end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
54 55 56 57 |
# File 'lib/moneta/adapters/couch.rb', line 54 def clear( = {}) @db.recreate! self end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
45 46 47 48 49 50 51 |
# File 'lib/moneta/adapters/couch.rb', line 45 def delete(key, = {}) value = @db.get(key) @db.delete_doc('_id' => value['_id'], '_rev' => value['_rev']) value['value'] rescue ::RestClient::ResourceNotFound nil end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
18 19 20 21 22 |
# File 'lib/moneta/adapters/couch.rb', line 18 def key?(key, = {}) @db.get(key) != nil rescue ::RestClient::ResourceNotFound false end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
25 26 27 28 29 |
# File 'lib/moneta/adapters/couch.rb', line 25 def load(key, = {}) @db.get(key)['value'] rescue ::RestClient::ResourceNotFound nil end |
#store(key, value, options = {}) ⇒ Object
Store value with key
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/moneta/adapters/couch.rb', line 32 def store(key, value, = {}) doc = {'_id' => key, 'value' => value} begin doc['_rev'] = @db.get(key)['_rev'] rescue ::RestClient::ResourceNotFound end @db.save_doc(doc) value rescue ::RestClient::RequestFailed value end |