Sha256: f629c2093ec662fbec0e0f1100ac97151aafe953c3177d6fdd22cd4cb5979cce

Contents?: true

Size: 927 Bytes

Versions: 2

Compression:

Stored size: 927 Bytes

Contents

require 'active_record'
require 'Base64'

module VinExploder
module Cache
  
  # A VinExploder cache adapter using ActiveRecord for saving the decoded vin attributes.
  #
  # this store assumes a model with 2 attributes:
  # - *key*: the first 8, 10th and 11th characters of the vin
  # - *data*: A hash of the decoded vin attributes
  class ActiveRecordCacheStore < Store
    
    def initialize(options = {})
      super
      @model = options[:model_class]
    end
    
    def read(vin)
      key = make_vin_cache_key(vin)
      obj = @model.find_by_key(key)
      obj.data unless obj.nil?
    end
    
    def write(vin, hash)
      key = make_vin_cache_key(vin)
      obj = @model.new :key => key, :data => hash
      obj.save
      hash
    end
    
    def delete(vin)
      key = make_vin_cache_key(vin)
      obj = @model.find_by_key(key)
      deleted = obj.delete()
      !deleted.nil?
    end
    
  end
  
end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vin_exploder-0.2.0 lib/vin_exploder/cache/activerecord_cache_store.rb
vin_exploder-0.1.0 lib/vin_exploder/cache/activerecord_cache_store.rb