Class: Moneta::Adapters::Couch

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

Overview

CouchDB backend

Instance Method Summary collapse

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.

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 = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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


54
55
56
57
# File 'lib/moneta/adapters/couch.rb', line 54

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

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

Delete the key from the store and return the current value

Parameters:

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

Returns:

  • (Object)

    current value



45
46
47
48
49
50
51
# File 'lib/moneta/adapters/couch.rb', line 45

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

Exists the value with key

Parameters:

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

Returns:

  • (Boolean)


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

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

Parameters:

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

Returns:

  • (Object)

    value



25
26
27
28
29
# File 'lib/moneta/adapters/couch.rb', line 25

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

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

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Returns:

  • value



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/moneta/adapters/couch.rb', line 32

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