Class: VinExploder::Cache::CouchrestCacheStore

Inherits:
Store
  • Object
show all
Defined in:
lib/vin_exploder/cache/couchrest_cache_store.rb

Overview

A VinExploder cache adapter using CouchDB for saving the decoded vin attributes.

this store assumes there is 1 attribute in the document:

  • data: A hash of the decoded vin attributes

Instance Attribute Summary

Attributes inherited from Store

#connection

Instance Method Summary collapse

Methods inherited from Store

#fetch

Constructor Details

#initialize(options = {}) ⇒ CouchrestCacheStore

Returns a new instance of CouchrestCacheStore.



12
13
14
15
16
17
# File 'lib/vin_exploder/cache/couchrest_cache_store.rb', line 12

def initialize(options = {})
  super
  srv = CouchRest.new options[:host]
  @db = srv.database!(options[:db_name])
  # vin_view = db.save_doc({"_id" => "_design/vins", :views => {:vins => {:map => 'function(doc){ if(doc.key){ emit(doc.key, doc.data) } }'}}}) unless db.get("_design/vins")
end

Instance Method Details

#delete(vin) ⇒ Object



32
33
34
35
36
# File 'lib/vin_exploder/cache/couchrest_cache_store.rb', line 32

def delete(vin)
  key = make_vin_cache_key(vin)
  result = @db.delete_doc(@db.get(key))
  !result.nil? && result['ok']
end

#read(vin) ⇒ Object



19
20
21
22
23
24
# File 'lib/vin_exploder/cache/couchrest_cache_store.rb', line 19

def read(vin)
  key = make_vin_cache_key(vin)
  result = @db.get(key)['data'] rescue nil
  hash = symbolize_result_hash(result) unless result.nil?
  hash
end

#write(vin, hash) ⇒ Object



26
27
28
29
30
# File 'lib/vin_exploder/cache/couchrest_cache_store.rb', line 26

def write(vin, hash)
  key = make_vin_cache_key(vin)
  @db.save_doc({"_id" => key, :data => hash})
  hash
end