lib/ruby_ext/declarative_cache.rb in ruby-ext-0.2.16 vs lib/ruby_ext/declarative_cache.rb in ruby-ext-0.4.0

- old
+ new

@@ -1,79 +1,82 @@ require 'monitor' class Module - def cache! *methods - DeclarativeCache.cache! self, *methods - end - alias_method :cache, :cache! + def cache_method *methods + DeclarativeCache.cache_method self, *methods + end - def cache_with_params! *methods - DeclarativeCache.cache_with_params! self, *methods + def cache_method_with_params *methods + DeclarativeCache.cache_method_with_params self, *methods + end +end + +class Object + def clear_cache_method + instance_variables.each do |iv| + remove_instance_variable iv if iv =~ /^@cached_/ + end end - alias_method :cache_with_params, :cache_with_params! end -module DeclarativeCache - DISABLED = false +module DeclarativeCache + DISABLED = false - warn "CASHE DISABLED" if DISABLED + warn "CASHE DISABLED" if DISABLED unless DISABLED - class << self - def alias_counter - @alias_counter ||= 0 - @alias_counter += 1 - return "cached_method_#{@alias_counter}" - end - - def cache! klass, *methods - methods.each do |method| - klass.class_eval do - als = (method.to_s =~ /^[_a-zA-Z0-9]+$/) ? "cached_#{method}" : DeclarativeCache.alias_counter.to_sym - iv_check = "@#{als}_check" - iv = "@#{als}" - - alias_method als, method - - define_method method do |*args| - args.should! :be_empty - unless cached = instance_variable_get(iv) - unless check = instance_variable_get(iv_check) - cached = send als - instance_variable_set iv, cached - instance_variable_set iv_check, true - end - end - cached - end - end - end - end - alias_method :cache, :cache! - - def cache_with_params! klass, *methods - methods.each do |method| - klass.class_eval do - als = (method.to_s =~ /^[_a-zA-Z0-9]+$/) ? "cached_#{method}" : DeclarativeCache.alias_counter.to_sym - iv = "@#{als}" - - alias_method als, method - - define_method method do |*args| - unless results = instance_variable_get(iv) - results = {} - instance_variable_set iv, results - end - - unless results.include? args - results[args] = send als, *args - end - - results[args] - end - end - end - end - alias_method :cache_with_params, :cache_with_params! - - end + class << self + + def cache_method klass, *methods + methods.each do |method| + klass.class_eval do + als = "cached_#{escape_method(method)}" + iv_check = "@#{als}_check" + iv = "@#{als}" + + raise "Can't cache the #{method} twice!" if instance_methods.include?(als) + + alias_method als, method + + define_method method do |*args| + raise "You tried to use cache without params for method with params (use 'cache_method_with_params' instead)!" unless args.empty? + unless cached = instance_variable_get(iv) + unless check = instance_variable_get(iv_check) + cached = send als + instance_variable_set iv, cached + instance_variable_set iv_check, true + end + end + cached + end + end + end + end + + def cache_method_with_params klass, *methods + methods.each do |method| + klass.class_eval do + als = "cached_#{escape_method(method)}" + iv = "@#{als}" + + raise "Can't cache the '#{method}' twice!" if instance_methods.include?(als) + + alias_method als, method + + define_method method do |*args| + unless results = instance_variable_get(iv) + results = {} + instance_variable_set iv, results + end + + unless results.include? args + results[args] = send als, *args + end + + results[args] + end + end + end + end + + end end end \ No newline at end of file