Sha256: 002c350791a92a908b01a9cc56ccf6256eb0e8a16d73bf760d6a224ccbacc6cb

Contents?: true

Size: 1.91 KB

Versions: 5

Compression:

Stored size: 1.91 KB

Contents

module Howitzer
  ##
  #
  # Data can be stored in memory using this class
  #
  module Cache
    SPECIAL_NS_LIST = [:cloud].freeze # :nodoc:
    @data ||= {}

    class << self
      attr_reader :data

      # 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

      def store(namespace, key, value)
        check_ns(namespace)
        @data[namespace][key] = value
      end

      # 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

      def extract(namespace, key = nil)
        check_ns(namespace)
        key ? @data[namespace][key] : @data[namespace]
      end

      # Deletes all data from a namespace
      #
      # @param namespace [String] a namespace

      def clear_ns(namespace)
        init_ns(namespace)
      end

      # Deletes all namespaces with data
      #
      # @param exception_list [Array] a namespace list for excluding

      def clear_all_ns(exception_list = SPECIAL_NS_LIST)
        (@data.keys - exception_list).each { |ns| clear_ns(ns) }
      end

      private

      def check_ns(namespace)
        raise Howitzer::NoDataError, 'Data storage namespace can not be empty' unless namespace

        init_ns(namespace) if ns_absent?(namespace)
      end

      def ns_absent?(namespace)
        !@data.key?(namespace)
      end

      def init_ns(namespace)
        @data[namespace] = {}
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
howitzer-2.6.1 lib/howitzer/cache.rb
howitzer-2.6.0 lib/howitzer/cache.rb
howitzer-2.5.0 lib/howitzer/cache.rb
howitzer-2.4.0 lib/howitzer/cache.rb
howitzer-2.3.0 lib/howitzer/cache.rb