Sha256: 7837ee8d49228bce48646a4325407869c02096da7d83c263c49313edc5b49c4d

Contents?: true

Size: 1.7 KB

Versions: 41

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module EacRubyUtils
  module SimpleCache
    UNCACHED_METHOD_NAME_SUFFIX = '_uncached'
    UNCACHED_METHOD_PATTERN = /
      \A(\s+)_#{::Regexp.quote(UNCACHED_METHOD_NAME_SUFFIX)}([\!\?]?)\z
    /x.freeze

    class << self
      def uncached_method_name(method_name)
        method_name = method_name.to_s
        end_mark = nil
        if %w[! ?].any? { |mark| method_name.end_with?(mark) }
          end_mark = method_name[-1]
          method_name = method_name[0..-2]
        end
        "#{method_name}#{UNCACHED_METHOD_NAME_SUFFIX}#{end_mark}"
      end
    end

    def method_missing(method, *args, &block)
      if respond_to?(::EacRubyUtils::SimpleCache.uncached_method_name(method), true)
        call_method_with_cache(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(*keys)
      if keys.any?
        keys.each { |key| cache_keys.delete(key) }
      else
        @cache_keys = nil
      end
    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('@@@')
      unless cache_keys.key?(key)
        uncached_value = call_uncached_method(method, args)
        cache_keys[key] = uncached_value
      end
      cache_keys[key]
    end

    def call_uncached_method(method, args)
      send(::EacRubyUtils::SimpleCache.uncached_method_name(method), *args)
    end

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

Version data entries

41 entries across 41 versions & 3 rubygems

Version Path
eac_ruby_utils-0.80.1 lib/eac_ruby_utils/simple_cache.rb
eac_ruby_utils-0.80.0 lib/eac_ruby_utils/simple_cache.rb
eac_ruby_utils-0.79.0 lib/eac_ruby_utils/simple_cache.rb
ehbrs-tools-0.28.3 vendor/eac_ruby_utils/lib/eac_ruby_utils/simple_cache.rb
eac_ruby_utils-0.78.0 lib/eac_ruby_utils/simple_cache.rb
avm-tools-0.110.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/simple_cache.rb
eac_ruby_utils-0.77.1 lib/eac_ruby_utils/simple_cache.rb
avm-tools-0.109.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/simple_cache.rb
eac_ruby_utils-0.77.0 lib/eac_ruby_utils/simple_cache.rb
avm-tools-0.109.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/simple_cache.rb
ehbrs-tools-0.28.2 vendor/eac_ruby_utils/lib/eac_ruby_utils/simple_cache.rb
avm-tools-0.108.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/simple_cache.rb
ehbrs-tools-0.28.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/simple_cache.rb
avm-tools-0.107.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/simple_cache.rb
eac_ruby_utils-0.76.0 lib/eac_ruby_utils/simple_cache.rb
ehbrs-tools-0.28.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/simple_cache.rb
avm-tools-0.106.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/simple_cache.rb
ehbrs-tools-0.27.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/simple_cache.rb
eac_ruby_utils-0.75.0 lib/eac_ruby_utils/simple_cache.rb
ehbrs-tools-0.26.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/simple_cache.rb