Sha256: fe990b23e6e7bfd5e17f66df15149c4ee01481469e320e7d32c1cb3904917991

Contents?: true

Size: 895 Bytes

Versions: 3

Compression:

Stored size: 895 Bytes

Contents

class Proton
  # Module: Cache
  module Cacheable
    def self.enable!
      @enabled = true
    end

    def self.disable!
      @enabled = false
    end

    def self.enabled?()
      !! @enabled
    end
    
    # Enable by default
    enable!

    def self.cache(*args)
      if enabled?
        @cache ||= Hash.new
        args   = args.map { |s| s.to_s }.join('-')
        @cache[args] ||= yield
      else
        yield
      end
    end

    def cache_method(*methods)
      methods.each do |method|
        alias_method :"real_#{method}", method

        class_eval do
          define_method(method) { |*args|
            id = nil
            id ||= self.file  if respond_to?(:file)
            id ||= self.to_s

            Cacheable.cache self.class, method, id, args do
              send :"real_#{method}", *args
            end
          }
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
proton-0.3.6 lib/proton/cacheable.rb
proton-0.3.5 lib/proton/cacheable.rb
proton-0.3.4 lib/proton/cacheable.rb