Sha256: 3c40b45b9e7c4d714a81bcbf74a0f77a26556e1a0c75d489ea1967dcbf60982d

Contents?: true

Size: 1.52 KB

Versions: 5

Compression:

Stored size: 1.52 KB

Contents

#encoding: utf-8

# the main module
module TheArrayComparator
  #caching class
  class Cache < StrategyDispatcher

    strategy_reader :caching_strategies
    attr_reader :caches

    # Variable to store available caches
    def initialize
      super()
      @caches = {}

      register :anonymous_cache, CachingStrategies::AnonymousCache
      register :single_value_cache, CachingStrategies::SingleValueCache
    end

    # @see [StrategyDispatcher]
    def class_must_have_methods
      [
        :add,
        :clear,
        :stored_objects,
        :new_objects?,
        :delete_object,
        :fetch_object,
      ]
    end

    # @see [StrategyDispatcher]
    def exception_to_raise_for_invalid_strategy
      Exceptions::IncompatibleCachingStrategy
    end

    # Retrieve cache 
    #
    # @param [Symbol] cache
    #   the cache to be used
    def [](cache)
      c = cache.to_sym

      raise Exceptions::CacheDoesNotExist, "Unknown cache \":#{c}\" given. Did you create it in advance?"  unless caches.has_key?(c)
      caches[c]
    end

    # Add a new cache
    #
    # @param [Symbol] cache
    #   the cache to be created
    #
    # @param [Symbol] strategy
    #   the cache strategy to be used
    def add(cache,strategy)
      c = cache.to_sym
      s = strategy.to_sym

      raise Exceptions::UnknownCachingStrategy, "Unknown caching strategy \":#{strategy}\" given. Did you register it in advance?"  unless caching_strategies.has_key?(strategy)

      caches[c] = caching_strategies[s].new 
      caches[c]
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
the_array_comparator-0.5.0 lib/the_array_comparator/cache.rb
the_array_comparator-0.4.0 lib/the_array_comparator/cache.rb
the_array_comparator-0.3.4 lib/the_array_comparator/cache.rb
the_array_comparator-0.3.1 lib/the_array_comparator/cache.rb
the_array_comparator-0.3.0 lib/the_array_comparator/cache.rb