Class: Moneta::Adapters::Couch

Inherits:
Base
  • Object
show all
Defined in:
lib/moneta/adapters/couch.rb

Overview

CouchDB backend

Instance Method Summary collapse

Methods inherited from Base

#[], #[]=, #close, #decrement, #fetch, #increment

Methods included from Mixins::WithOptions

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ Couch

Constructor

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :db (String) — default: 'moneta'

    Couch database



12
13
14
15
# File 'lib/moneta/adapters/couch.rb', line 12

def initialize(options = {})
  options[:db] ||= 'moneta'
  @db = ::CouchRest.database!(options[:db])
end

Instance Method Details

#clear(options = {}) ⇒ Object



49
50
51
52
# File 'lib/moneta/adapters/couch.rb', line 49

def clear(options = {})
  @db.recreate!
  self
end

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



41
42
43
44
45
46
47
# File 'lib/moneta/adapters/couch.rb', line 41

def delete(key, options = {})
  value = @db.get(key)
  @db.delete_doc('_id' => value['_id'], '_rev' => value['_rev'])
  value['value']
rescue RestClient::ResourceNotFound
  nil
end

#key?(key, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/moneta/adapters/couch.rb', line 17

def key?(key, options = {})
  @db.get(key) != nil
rescue RestClient::ResourceNotFound
  false
end

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



23
24
25
26
27
# File 'lib/moneta/adapters/couch.rb', line 23

def load(key, options = {})
  @db.get(key)['value']
rescue RestClient::ResourceNotFound
  nil
end

#store(key, value, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/moneta/adapters/couch.rb', line 29

def store(key, value, options = {})
  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