Class: Moneta::Adapters::Couch

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

Instance Method Summary collapse

Methods included from Defaults

#[]=, #fetch

Constructor Details

#initialize(options = {}) ⇒ Couch

Returns a new instance of Couch.



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

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

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  deserialize(@db.get(key_for(key))["data"])
rescue RestClient::ResourceNotFound
  nil
end

#clearObject



46
47
48
# File 'lib/moneta/adapters/couch.rb', line 46

def clear(*)
  @db.recreate!
end

#delete(key) ⇒ Object



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

def delete(key, *)
  value = @db.get(key_for(key))

  if value
    @db.delete_doc({"_id" => value["_id"], "_rev" => value["_rev"]}) if value
    deserialize(value["data"])
  end
rescue RestClient::ResourceNotFound
  nil
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

def key?(key, *)
  !self[key_for(key)].nil?
rescue RestClient::ResourceNotFound
  false
end

#store(key, value) ⇒ Object



29
30
31
32
33
# File 'lib/moneta/adapters/couch.rb', line 29

def store(key, value, *)
  @db.save_doc("_id" => key_for(key), :data => serialize(value))
rescue RestClient::RequestFailed
  self[key_for(key)]
end