Class: VinExploder::Cache::ActiveRecordCacheStore

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

Overview

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

Instance Attribute Summary

Attributes inherited from Store

#connection

Instance Method Summary collapse

Methods inherited from Store

#fetch

Constructor Details

#initialize(options = {}) ⇒ ActiveRecordCacheStore

Returns a new instance of ActiveRecordCacheStore.



14
15
16
17
# File 'lib/vin_exploder/cache/active_record_cache_store.rb', line 14

def initialize(options = {})
  super
  @model_class = options[:model_class]
end

Instance Method Details

#delete(vin) ⇒ Object



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

def delete(vin)
  key = make_vin_cache_key(vin)
  obj = @model_class.find_by_key(key)
  deleted = obj.delete()
  !deleted.nil?
end

#read(vin) ⇒ Object



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

def read(vin)
  key = make_vin_cache_key(vin)
  obj = @model_class.find_by_key(key)
  obj.data unless obj.nil?
end

#write(vin, hash) ⇒ Object



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

def write(vin, hash)
  key = make_vin_cache_key(vin)
  obj = @model_class.new :key => key, :data => hash
  obj.save
  hash
end