Class: ProstoCache::ProstoModelCache
- Inherits:
-
Object
- Object
- ProstoCache::ProstoModelCache
- Defined in:
- lib/prosto_cache/prosto_model_cache.rb
Overview
cache itself, contains pretty much all the logic
Constant Summary collapse
- MAX_CACHE_LIFE =
seconds
60
Class Method Summary collapse
Instance Method Summary collapse
- #[](*keys) ⇒ Object
-
#initialize(model_class, accessor_keys, sort_keys) ⇒ ProstoModelCache
constructor
A new instance of ProstoModelCache.
- #invalidate ⇒ Object
- #keys ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(model_class, accessor_keys, sort_keys) ⇒ ProstoModelCache
Returns a new instance of ProstoModelCache.
65 66 67 68 69 70 71 |
# File 'lib/prosto_cache/prosto_model_cache.rb', line 65 def initialize(model_class, accessor_keys, sort_keys) raise ArgumentError, "No model class provided" unless model_class @model_class = model_class @accessor_keys = [*(accessor_keys || :name)] @sort_keys = sort_keys ? [*(sort_keys)] : @accessor_keys end |
Class Method Details
.fail_on_missing_value?(litmus) ⇒ Boolean
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/prosto_cache/prosto_model_cache.rb', line 52 def self.fail_on_missing_value?(litmus) case litmus when Symbol true when String false else raise ArgumentError, "Unknown type of cache key #{litmus.inspect}" end end |
Instance Method Details
#[](*keys) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/prosto_cache/prosto_model_cache.rb', line 77 def [](*keys) unless keys.length == accessor_keys.length raise BadCacheKeyError, "Cached accessed by #{keys.length} keys, expected #{accessor_keys.length}" end keys.zip((1..keys.length)).inject(safe_cache) do |memo, (key, index)| value = memo[key] unless value if ProstoModelCache.fail_on_missing_value?(keys.last) raise BadCacheKeyError, key else value = ProstoHash.new unless index == keys.length end end value end end |
#invalidate ⇒ Object
73 74 75 |
# File 'lib/prosto_cache/prosto_model_cache.rb', line 73 def invalidate self.cache = self.signature = self.validated_at = nil end |
#keys ⇒ Object
96 97 98 |
# File 'lib/prosto_cache/prosto_model_cache.rb', line 96 def keys safe_cache.keys end |
#values ⇒ Object
100 101 102 |
# File 'lib/prosto_cache/prosto_model_cache.rb', line 100 def values safe_cache.values end |