Module: Adapter::Couch

Defined in:
lib/adapter/couch.rb,
lib/adapter/couch/version.rb

Constant Summary collapse

NonHashValueKeyName =
'nh_value'
VERSION =
"0.1.2"

Instance Method Summary collapse

Instance Method Details

#clearObject



37
38
39
# File 'lib/adapter/couch.rb', line 37

def clear
  client.recreate!
end

#decode(value) ⇒ Object



45
46
47
48
# File 'lib/adapter/couch.rb', line 45

def decode(value)
  return if value.nil?
  value.key?(NonHashValueKeyName) ? value[NonHashValueKeyName] : value
end

#delete(key) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/adapter/couch.rb', line 27

def delete(key)
  begin
    doc = client.get(key_for(key))
    client.delete_doc(doc)
    decode(doc)
  rescue RestClient::ResourceNotFound => e
    nil
  end        
end

#encode(value) ⇒ Object



41
42
43
# File 'lib/adapter/couch.rb', line 41

def encode(value)
  value.is_a?(Hash) ? value : {NonHashValueKeyName => value}
end

#read(key) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/adapter/couch.rb', line 8

def read(key)
  begin
    if doc = client.get(key_for(key))
      rev_map[key_for(key)] = doc.delete("_rev")
      decode(doc)
    end
  rescue RestClient::ResourceNotFound => e
    nil
  end
end

#write(key, value) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/adapter/couch.rb', line 19

def write(key, value)
  doc = {"_id" => key_for(key)}.merge(encode(value))
  doc["_rev"] = rev_map[key_for(key)] if rev_map[key_for(key)]
  status = client.save_doc(doc)
  rev_map[key_for(key)] = status["rev"]
  value
end