Class: Moneta::Couch

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

Instance Method Summary collapse

Methods included from Defaults

#fetch, #store

Constructor Details

#initialize(options = {}) ⇒ Couch

Returns a new instance of Couch.



12
13
14
15
16
17
18
# File 'lib/moneta/couch.rb', line 12

def initialize(options = {})
  @db = ::CouchRest.database!(options[:db])
  unless options[:skip_expires]
    @expiration = Moneta::Couch.new(:db => "#{options[:db]}_expiration", :skip_expires => true)
    self.extend(StringExpires)
  end
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



34
35
36
37
38
# File 'lib/moneta/couch.rb', line 34

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

#clearObject



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

def clear
  @db.recreate!
end

#delete(key) ⇒ Object



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

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

#delete_storeObject



59
60
61
# File 'lib/moneta/couch.rb', line 59

def delete_store
  @db.delete!
end

#key?(key) ⇒ Boolean Also known as: has_key?

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/moneta/couch.rb', line 20

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

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



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

def update_key(key, options = {})
  val = self[key]
  self.store(key, val, options)
rescue RestClient::ResourceNotFound
  nil
end