Class: Card::Cache::Temporary
- Inherits:
-
Object
- Object
- Card::Cache::Temporary
- Defined in:
- lib/card/cache/temporary.rb
Overview
The Temporary cache is intended for a single request, script, migration, etc. It allows you to alter a card and then retrieve the card with those alterations intact without saving those changes to the database.
In practice, it's a set of Cache-like methods for using a simple Hash.
Unlike the Persistent cache, the Temporary cache can handle objects with singleton classes.
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #dump ⇒ Object
- #exist?(key) ⇒ Boolean
- #fetch(key, &_block) ⇒ Object
-
#initialize ⇒ Temporary
constructor
A new instance of Temporary.
- #read(key) ⇒ Object
- #reset ⇒ Object
- #write(key, value) ⇒ Object
Constructor Details
#initialize ⇒ Temporary
Returns a new instance of Temporary.
16 17 18 |
# File 'lib/card/cache/temporary.rb', line 16 def initialize @store = {} end |
Instance Attribute Details
#store ⇒ Object (readonly)
Returns the value of attribute store.
14 15 16 |
# File 'lib/card/cache/temporary.rb', line 14 def store @store end |
Instance Method Details
#delete(key) ⇒ Object
37 38 39 |
# File 'lib/card/cache/temporary.rb', line 37 def delete key @store.delete key end |
#dump ⇒ Object
41 42 43 44 45 |
# File 'lib/card/cache/temporary.rb', line 41 def dump @store.each do |k, v| p "#{k} --> #{v.inspect[0..30]}" end end |
#exist?(key) ⇒ Boolean
52 53 54 |
# File 'lib/card/cache/temporary.rb', line 52 def exist? key @store.key? key end |
#fetch(key, &_block) ⇒ Object
32 33 34 |
# File 'lib/card/cache/temporary.rb', line 32 def fetch key, &_block read(key) || write(key, yield) end |
#read(key) ⇒ Object
21 22 23 24 |
# File 'lib/card/cache/temporary.rb', line 21 def read key return unless @store.key? key @store[key] end |
#reset ⇒ Object
47 48 49 |
# File 'lib/card/cache/temporary.rb', line 47 def reset @store = {} end |
#write(key, value) ⇒ Object
27 28 29 |
# File 'lib/card/cache/temporary.rb', line 27 def write key, value @store[key] = value end |