Class: TheArrayComparator::Cache
- Inherits:
-
StrategyDispatcher
- Object
- StrategyDispatcher
- TheArrayComparator::Cache
- Defined in:
- lib/the_array_comparator/cache.rb
Overview
caching class
Instance Attribute Summary collapse
-
#caches ⇒ Object
readonly
Returns the value of attribute caches.
Instance Method Summary collapse
-
#[](cache) ⇒ Object
Retrieve cache.
-
#add(cache, strategy) ⇒ Object
Add a new cache.
- #class_must_have_methods ⇒ Object
- #exception_to_raise_for_invalid_strategy ⇒ Object
-
#initialize ⇒ Cache
constructor
Variable to store available caches.
Methods inherited from StrategyDispatcher
#each, #register, strategy_reader
Constructor Details
#initialize ⇒ Cache
Variable to store available caches
12 13 14 15 16 17 18 |
# File 'lib/the_array_comparator/cache.rb', line 12 def initialize super() @caches = {} register :anonymous_cache, CachingStrategies::AnonymousCache register :single_value_cache, CachingStrategies::SingleValueCache end |
Instance Attribute Details
#caches ⇒ Object (readonly)
Returns the value of attribute caches.
9 10 11 |
# File 'lib/the_array_comparator/cache.rb', line 9 def caches @caches end |
Instance Method Details
#[](cache) ⇒ Object
Retrieve cache
41 42 43 44 45 46 |
# File 'lib/the_array_comparator/cache.rb', line 41 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(cache, strategy) ⇒ Object
Add a new cache
55 56 57 58 59 60 61 62 63 |
# File 'lib/the_array_comparator/cache.rb', line 55 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 |
#class_must_have_methods ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/the_array_comparator/cache.rb', line 21 def class_must_have_methods [ :add, :clear, :stored_objects, :new_objects?, :delete_object, :fetch_object, ] end |
#exception_to_raise_for_invalid_strategy ⇒ Object
33 34 35 |
# File 'lib/the_array_comparator/cache.rb', line 33 def exception_to_raise_for_invalid_strategy Exceptions::IncompatibleCachingStrategy end |