Parent

Included Modules

Files

Class/Module Index [+]

Quicksearch

Moneta::Adapters::Couch

CouchDB backend

You can store hashes directly using this adapter.

@example Store hashes

db = Moneta::Adapters::Mongo.new
db['key'] = {a: 1, b: 2}

@api public

Attributes

backend[R]

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options @option options [String] :host ('127.0.0.1') Couch host @option options [String] :port (5984) Couch port @option options [String] :db ('moneta') Couch database @option options [String] :value_field ('value') Document field to store value @option options [String] :type_field ('type') Document field to store value type @option options [Faraday connection] :backend Use existing backend instance

# File lib/moneta/adapters/couch.rb, line 29
def initialize(options = {})
  @value_field = options[:value_field] || 'value'
  @type_field = options[:type_field] || 'type'
  url = "http://#{options[:host] || '127.0.0.1'}:#{options[:port] || 5984}/#{options[:db] || 'moneta'}"
  @backend = options[:backend] || ::Faraday.new(:url => url)
  create_db
end

Public Instance Methods

clear(options = {}) click to toggle source

(see Proxy#clear)

# File lib/moneta/adapters/couch.rb, line 75
def clear(options = {})
  @backend.delete ''
  create_db
  self
end
create(key, value, options = {}) click to toggle source

(see Proxy#create)

# File lib/moneta/adapters/couch.rb, line 82
def create(key, value, options = {})
  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 = {}) click to toggle source

(see Proxy#delete)

# File lib/moneta/adapters/couch.rb, line 61
def delete(key, options = {})
  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 = {}) click to toggle source

(see Proxy#key?)

# File lib/moneta/adapters/couch.rb, line 38
def key?(key, options = {})
  @backend.head(key).status == 200
end
load(key, options = {}) click to toggle source

(see Proxy#load)

# File lib/moneta/adapters/couch.rb, line 43
def load(key, options = {})
  response = @backend.get(key)
  response.status == 200 ? body_to_value(response.body) : nil
end
store(key, value, options = {}) click to toggle source

(see Proxy#store)

# File lib/moneta/adapters/couch.rb, line 49
def store(key, value, options = {})
  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

[Validate]

Generated with the Darkfish Rdoc Generator 2.