Class: Moneta::Adapters::Couch
- Inherits:
-
Object
- Object
- Moneta::Adapters::Couch
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/couch.rb
Overview
CouchDB backend
You can store hashes directly using this adapter.
Instance Attribute Summary collapse
- #backend ⇒ Object readonly
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#create(key, value, options = {}) ⇒ Boolean
Atomically sets a key to value if it’s not set.
-
#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, #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.
29 30 31 32 33 34 35 |
# File 'lib/moneta/adapters/couch.rb', line 29 def initialize( = {}) @value_field = [:value_field] || 'value' @type_field = [:type_field] || 'type' 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)
18 19 20 |
# File 'lib/moneta/adapters/couch.rb', line 18 def backend @backend end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
75 76 77 78 79 |
# File 'lib/moneta/adapters/couch.rb', line 75 def clear( = {}) @backend.delete '' create_db self end |
#create(key, value, options = {}) ⇒ Boolean
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Atomically sets a key to value if it’s not set.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/moneta/adapters/couch.rb', line 82 def create(key, value, = {}) body = value_to_body(value, nil) response = @backend.put(key, body, 'Content-Type' => 'application/json') case response.status when 201 true when 409 false else raise "HTTP error #{response.status}" end rescue tries ||= 0 (tries += 1) < 10 ? retry : raise end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/moneta/adapters/couch.rb', line 61 def delete(key, = {}) response = @backend.get(key) if response.status == 200 value = body_to_value(response.body) 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
38 39 40 |
# File 'lib/moneta/adapters/couch.rb', line 38 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
43 44 45 46 |
# File 'lib/moneta/adapters/couch.rb', line 43 def load(key, = {}) response = @backend.get(key) response.status == 200 ? body_to_value(response.body) : nil end |
#store(key, value, options = {}) ⇒ Object
Store value with key
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/moneta/adapters/couch.rb', line 49 def store(key, value, = {}) response = @backend.head(key) body = value_to_body(value, response.status == 200 && response['etag'][1..-2]) response = @backend.put(key, body, 'Content-Type' => 'application/json') raise "HTTP error #{response.status}" unless response.status == 201 value rescue tries ||= 0 (tries += 1) < 10 ? retry : raise end |