Sha256: 06015da847e1f2d83b701af2971735533dcb5cec939f5b18a189bd315cfcb6d6

Contents?: true

Size: 1.08 KB

Versions: 9

Compression:

Stored size: 1.08 KB

Contents

require 'couchrest'

module VinExploder
module Cache
  
  # 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
  class CouchrestCacheStore < Store
    
    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
    
    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
    
    def write(vin, hash)
      key = make_vin_cache_key(vin)
      @db.save_doc({"_id" => key, :data => hash})
      hash
    end
    
    def delete(vin)
      key = make_vin_cache_key(vin)
      result = @db.delete_doc(@db.get(key))
      !result.nil? && result['ok']
    end
    
  end
  
end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
vin_exploder-0.4.8 lib/vin_exploder/cache/couchrest_cache_store.rb
vin_exploder-0.4.7 lib/vin_exploder/cache/couchrest_cache_store.rb
vin_exploder-0.4.6 lib/vin_exploder/cache/couchrest_cache_store.rb
vin_exploder-0.4.5 lib/vin_exploder/cache/couchrest_cache_store.rb
vin_exploder-0.4.4 lib/vin_exploder/cache/couchrest_cache_store.rb
vin_exploder-0.4.3 lib/vin_exploder/cache/couchrest_cache_store.rb
vin_exploder-0.4.2 lib/vin_exploder/cache/couchrest_cache_store.rb
vin_exploder-0.4.1 lib/vin_exploder/cache/couchrest_cache_store.rb
vin_exploder-0.4.0 lib/vin_exploder/cache/couchrest_cache_store.rb