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
38 39 40 |
# File 'lib/card/cache/temporary.rb', line 38 def delete key @store.delete key end |
#dump ⇒ Object
42 43 44 45 46 |
# File 'lib/card/cache/temporary.rb', line 42 def dump @store.each do |k, v| p "#{k} --> #{v.inspect[0..30]}" end end |
#exist?(key) ⇒ Boolean
53 54 55 |
# File 'lib/card/cache/temporary.rb', line 53 def exist? key @store.key? key end |
#fetch(key, &_block) ⇒ Object
33 34 35 |
# File 'lib/card/cache/temporary.rb', line 33 def fetch key, &_block read(key) || write(key, yield) end |
#read(key) ⇒ Object
21 22 23 24 25 |
# File 'lib/card/cache/temporary.rb', line 21 def read key return unless @store.key? key @store[key] end |
#reset ⇒ Object
48 49 50 |
# File 'lib/card/cache/temporary.rb', line 48 def reset @store = {} end |
#write(key, value) ⇒ Object
28 29 30 |
# File 'lib/card/cache/temporary.rb', line 28 def write key, value @store[key] = value end |