module EacRubyUtils::SimpleCache

Constants

UNCACHED_METHOD_NAME_SUFFIX
UNCACHED_METHOD_PATTERN

Public Class Methods

uncached_method_name(method_name) click to toggle source
# File lib/eac_ruby_utils/simple_cache.rb, line 11
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

Public Instance Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/eac_ruby_utils/simple_cache.rb, line 22
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
reset_cache(*keys) click to toggle source
# File lib/eac_ruby_utils/simple_cache.rb, line 38
def reset_cache(*keys)
  if keys.any?
    keys.each { |key| cache_keys.delete(key) }
  else
    @cache_keys = nil
  end
end
respond_to_missing?(method, include_all = false) click to toggle source
Calls superclass method
# File lib/eac_ruby_utils/simple_cache.rb, line 30
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

Private Instance Methods

cache_keys() click to toggle source
# File lib/eac_ruby_utils/simple_cache.rb, line 63
def cache_keys
  @cache_keys ||= {}
end
call_method_with_cache(method, args, &block) click to toggle source
# File lib/eac_ruby_utils/simple_cache.rb, line 48
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
call_uncached_method(method, args) click to toggle source
# File lib/eac_ruby_utils/simple_cache.rb, line 59
def call_uncached_method(method, args)
  send(::EacRubyUtils::SimpleCache.uncached_method_name(method), *args)
end