class ReVIEW::Book::Cache

Public Class Methods

new() click to toggle source
# File lib/review/book/cache.rb, line 12
def initialize
  @store = {}
end

Public Instance Methods

cached?(key) click to toggle source
# File lib/review/book/cache.rb, line 31
def cached?(key)
  @store.key?(key)
end
fetch(key, &block) click to toggle source

key should be Symbol, not String

# File lib/review/book/cache.rb, line 21
def fetch(key, &block)
  raise ArgumentError, 'Key should be Symbol' unless key.is_a?(Symbol)

  if cached?(key)
    read(key)
  else
    exec_block_and_save(key, &block)
  end
end
reset() click to toggle source
# File lib/review/book/cache.rb, line 16
def reset
  @store.clear
end

Private Instance Methods

exec_block_and_save(key) { |key| ... } click to toggle source
# File lib/review/book/cache.rb, line 45
def exec_block_and_save(key)
  result = yield(key)

  write(key, result)

  result
end
read(key) click to toggle source
# File lib/review/book/cache.rb, line 37
def read(key)
  @store[key]
end
write(key, value) click to toggle source
# File lib/review/book/cache.rb, line 41
def write(key, value)
  @store[key] = value
end