Class: Card::Cache::Temporary

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeTemporary

Returns a new instance of Temporary.



16
17
18
# File 'lib/card/cache/temporary.rb', line 16

def initialize
  @store = {}
end

Instance Attribute Details

#storeObject (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

Parameters:

  • key (String)


38
39
40
# File 'lib/card/cache/temporary.rb', line 38

def delete key
  @store.delete key
end

#dumpObject



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

Parameters:

  • key (String)

Returns:

  • (Boolean)


53
54
55
# File 'lib/card/cache/temporary.rb', line 53

def exist? key
  @store.key? key
end

#fetch(key, &_block) ⇒ Object

Parameters:

  • key (String)


33
34
35
# File 'lib/card/cache/temporary.rb', line 33

def fetch key, &_block
  read(key) || write(key, yield)
end

#read(key) ⇒ Object

Parameters:

  • key (String)


21
22
23
24
25
# File 'lib/card/cache/temporary.rb', line 21

def read key
  return unless @store.key? key

  @store[key]
end

#resetObject



48
49
50
# File 'lib/card/cache/temporary.rb', line 48

def reset
  @store = {}
end

#write(key, value) ⇒ Object

Parameters:

  • key (String)


28
29
30
# File 'lib/card/cache/temporary.rb', line 28

def write key, value
  @store[key] = value
end