Class: VinExploder::Cache::CouchrestCacheStore
- 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
Instance Method Summary collapse
- #delete(vin) ⇒ Object
-
#initialize(options = {}) ⇒ CouchrestCacheStore
constructor
A new instance of CouchrestCacheStore.
- #read(vin) ⇒ Object
- #write(vin, hash) ⇒ Object
Methods inherited from Store
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( = {}) super srv = CouchRest.new [:host] @db = srv.database!([: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 |