Class: Moneta::Adapters::Couch
- Inherits:
-
Object
- Object
- Moneta::Adapters::Couch
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/couch.rb
Overview
CouchDB backend
Instance Attribute Summary collapse
- #backend ⇒ Object readonly
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#initialize(options = {}) ⇒ Couch
constructor
A new instance of Couch.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from Defaults
#[], #[]=, #close, #create, #decrement, #features, #fetch, included, #increment, #supports?
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ Couch
Returns a new instance of Couch.
18 19 20 21 22 |
# File 'lib/moneta/adapters/couch.rb', line 18 def initialize( = {}) url = "http://#{[:host] || '127.0.0.1'}:#{[:port] || 5984}/#{[:db] || 'moneta'}" @backend = [:backend] || ::Faraday.new(:url => url) create_db end |
Instance Attribute Details
#backend ⇒ Object (readonly)
11 12 13 |
# File 'lib/moneta/adapters/couch.rb', line 11 def backend @backend end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
63 64 65 66 67 |
# File 'lib/moneta/adapters/couch.rb', line 63 def clear( = {}) @backend.delete '' create_db self end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/moneta/adapters/couch.rb', line 49 def delete(key, = {}) response = @backend.get(key) if response.status == 200 value = MultiJson.load(response.body)['value'] response = @backend.delete("#{key}?rev=#{response['etag'][1..-2]}") raise "HTTP error #{response.status}" unless response.status == 200 value end rescue tries ||= 0 (tries += 1) < 10 ? retry : raise end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
25 26 27 |
# File 'lib/moneta/adapters/couch.rb', line 25 def key?(key, = {}) @backend.head(key).status == 200 end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
30 31 32 33 |
# File 'lib/moneta/adapters/couch.rb', line 30 def load(key, = {}) response = @backend.get(key) response.status == 200 ? MultiJson.load(response.body)['value'] : nil end |
#store(key, value, options = {}) ⇒ Object
Store value with key
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/moneta/adapters/couch.rb', line 36 def store(key, value, = {}) response = @backend.head(key) doc = { 'value' => value } doc['_rev'] = response['etag'][1..-2] if response.status == 200 response = @backend.put(key, MultiJson.dump(doc), 'Content-Type' => 'application/json') raise "HTTP error #{response.status}" unless response.status == 201 value rescue tries ||= 0 (tries += 1) < 10 ? retry : raise end |