Sha256: 9404989cf9417a6482808536cc1c57b90641c98d80052dd024eaa350ab38505d

Contents?: true

Size: 1.92 KB

Versions: 4

Compression:

Stored size: 1.92 KB

Contents

module CacheKeeper
  class Manager
    attr_accessor :cached_methods

    def initialize
      self.cached_methods = CacheKeeper::Store.new
    end

    def handled?(klass, method_name)
      cached_methods.find_by(klass, method_name).present?
    end

    def handle(klass, method_name, options, &block)
      CacheKeeper::CachedMethod.new(klass, method_name, options, &block).tap do |cached_method|
        if unsupported_options?(cached_method)
          raise "You're trying to autorefresh an ActiveRecord model, which we don't currently support."
        end

        cached_methods << cached_method
      end
    end

    def activate_if_handling(klass, method_name)
      cached_method = cached_methods.find_by(klass, method_name) or return

      if requires_activation?(cached_method)
        if unsupported_arity?(cached_method)
          raise "You're trying to cache a method with parameters, which we don't currently support."
        end

        CacheKeeper::ReplaceMethod.replace(cached_method) do
          instance_variable_get(:"@#{method_name}") || instance_variable_set(:"@#{method_name}", cached_method.call(self))
        end
      end
    end

    private

    def requires_activation?(cached_method)
      return false if cached_method.klass.instance_methods.exclude?(cached_method.method_name) && cached_method.klass.private_instance_methods.exclude?(cached_method.method_name)
      return false if cached_method.klass.private_instance_methods.include?(cached_method.alias_for_original_method)

      true
    end

    def unsupported_options?(cached_method)
      cached_method.klass < ActiveRecord::Base && cached_method.options[:autorefresh].present?
    end

    def unsupported_arity?(cached_method)
      original_method =
        cached_method.klass.instance_method(cached_method.method_name) ||
        cached_method.klass.privateinstance_method(cached_method.method_name)

      original_method.arity.nonzero?
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cache_keeper-0.6.1 lib/cache_keeper/manager.rb
cache_keeper-0.6.0 lib/cache_keeper/manager.rb
cache_keeper-0.5.1 lib/cache_keeper/manager.rb
cache_keeper-0.5.0 lib/cache_keeper/manager.rb