Sha256: bfed9179b75c9880ccba3c9c5824aad66f10cdcead3e2f9650d640a4bd5973f7

Contents?: true

Size: 973 Bytes

Versions: 7

Compression:

Stored size: 973 Bytes

Contents

# frozen_string_literal: true

module EacRubyUtils
  module SimpleCache
    UNCACHED_METHOD_PATTERN = /\A(\s+)_uncached\z/

    def method_missing(method, *args, &block)
      uncached_method = "#{method}_uncached"
      if respond_to?(uncached_method, true)
        call_method_with_cache(uncached_method, args, &block)
      else
        super
      end
    end

    def respond_to_missing?(method, include_all = false)
      if method.to_s.end_with?('_uncached')
        super
      else
        respond_to?("#{method}_uncached", true) || super
      end
    end

    def reset_cache
      @cache_keys = nil
    end

    private

    def call_method_with_cache(method, args, &block)
      raise 'Não é possível realizar o cache de métodos com bloco' if block
      key = ([method] + args).join('@@@')
      cache_keys[key] = send(method, *args) unless cache_keys.key?(key)
      cache_keys[key]
    end

    def cache_keys
      @cache_keys ||= {}
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
eac_ruby_utils-0.12.0 lib/eac_ruby_utils/simple_cache.rb
eac_ruby_utils-0.11.0 lib/eac_ruby_utils/simple_cache.rb
eac_ruby_utils-0.10.1 lib/eac_ruby_utils/simple_cache.rb
eac_ruby_utils-0.10.0 lib/eac_ruby_utils/simple_cache.rb
eac_ruby_utils-0.9.0 lib/eac_ruby_utils/simple_cache.rb
eac_ruby_utils-0.8.0 lib/eac_ruby_utils/simple_cache.rb
eac_ruby_utils-0.7.0 lib/eac_ruby_utils/simple_cache.rb