module Howitzer::Cache

Data can be stored in memory using this class

Attributes

data[R]

Public Class Methods

clear_all_ns(exception_list = SPECIAL_NS_LIST) click to toggle source

Deletes all namespaces with data

@param exception_list [Array] a namespace list for excluding

# File lib/howitzer/cache.rb, line 50
def clear_all_ns(exception_list = SPECIAL_NS_LIST)
  (@data.keys - exception_list).each { |ns| clear_ns(ns) }
end
clear_ns(namespace) click to toggle source

Deletes all data from a namespace

@param namespace [String] a namespace

# File lib/howitzer/cache.rb, line 42
def clear_ns(namespace)
  init_ns(namespace)
end
extract(namespace, key = nil) click to toggle source

Gets data from memory. Can get all namespace or single data value in namespace using key

@param namespace [String] a namespace @param key [String] key that isn’t necessary required @return [Object, Hash] all data from the namespace if the key is ommited, otherwise returs

all data for the namespace

@raise [NoDataError] if the namespace missing

# File lib/howitzer/cache.rb, line 33
def extract(namespace, key = nil)
  check_ns(namespace)
  key ? @data[namespace][key] : @data[namespace]
end
store(namespace, key, value) click to toggle source

Saves data into memory. Marking by a namespace and a key

@param namespace [String] a namespace @param key [String] a key that should be uniq within the namespace @param value [Object] everything you want to store in Memory @raise [NoDataError] if the namespace missing

# File lib/howitzer/cache.rb, line 20
def store(namespace, key, value)
  check_ns(namespace)
  @data[namespace][key] = value
end

Private Class Methods

check_ns(namespace) click to toggle source
# File lib/howitzer/cache.rb, line 56
def check_ns(namespace)
  raise Howitzer::NoDataError, 'Data storage namespace can not be empty' unless namespace

  init_ns(namespace) if ns_absent?(namespace)
end
init_ns(namespace) click to toggle source
# File lib/howitzer/cache.rb, line 66
def init_ns(namespace)
  @data[namespace] = {}
end
ns_absent?(namespace) click to toggle source
# File lib/howitzer/cache.rb, line 62
def ns_absent?(namespace)
  !@data.key?(namespace)
end