lib/arid_cache/store.rb in arid_cache-1.3.3 vs lib/arid_cache/store.rb in arid_cache-1.3.4

- old
+ new

@@ -67,11 +67,19 @@ end class InstanceCacheConfiguration < Configuration; end #:nodoc: class ClassCacheConfiguration < Configuration; end #:nodoc: def has?(object, key) - self.include?(object_store_key(object, key)) + return true if self.include?(object_store_key(object, key)) + + store_key = object.is_a?(Class) ? :class_store_key : :instance_store_key + klass = object.is_a?(Class) ? object : object.class + while klass.superclass + return true if self.include?(send(store_key, klass.superclass, key)) + klass = klass.superclass + end + false end # Empty the proc store def delete! delete_if { true } @@ -80,11 +88,11 @@ def self.instance @@singleton_instance ||= self.new end def find(object, key) - self[object_store_key(object, key)] + inherited_find(object, key) end def add_instance_cache_configuration(klass, key, opts, proc) add_generic_cache_configuration(instance_store_key(klass, key), klass, key, opts, proc) end @@ -95,30 +103,43 @@ def add_object_cache_configuration(object, key, opts, proc) add_generic_cache_configuration(object_store_key(object, key), object, key, opts, proc) end - def find_or_create(object, key) - store_key = object_store_key(object, key) - if self.include?(store_key) - self[store_key] - else - self[store_key] = AridCache::Store::Blueprint.new(object, key) - end - end - protected - def add_generic_cache_configuration(store_key, *args) - self[store_key] = AridCache::Store::Blueprint.new(*args) + def add_generic_cache_configuration(store_key, object, key, opts, proc) + self[store_key] = AridCache::Store::Blueprint.new(object, key, opts, proc) end def initialize end def class_store_key(klass, key); klass.name.downcase + '-' + key.to_s; end def instance_store_key(klass, key); AridCache::Inflector.pluralize(klass.name.downcase) + '-' + key.to_s; end def object_store_key(object, key) case object; when Class; class_store_key(object, key); else; instance_store_key(object.class, key); end end + + def inherited_find(object, key) + blueprint = self[object_store_key(object, key)] || AridCache::Store::Blueprint.new(object, key) + inherit_options(blueprint, object, key) + if blueprint.opts.empty? && blueprint['proc'].nil? + nil + else + blueprint + end + end + + def inherit_options(blueprint, object, key) + klass = object.is_a?(Class) ? object : object.class + store_key = object.is_a?(Class) ? :class_store_key : :instance_store_key + while klass.superclass + if super_blueprint = self[send(store_key, klass.superclass, key)] + blueprint.opts = super_blueprint.opts.merge(blueprint.opts) + blueprint['proc'] ||= super_blueprint['proc'] + end + klass = klass.superclass + end + end end -end \ No newline at end of file +end