Module: EacRubyUtils::SimpleCache

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



22
23
24
25
26
27
28
# 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

Class Method Details

.uncached_method_name(method_name) ⇒ Object



11
12
13
14
15
16
17
18
19
# 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

Instance Method Details

#reset_cache(*keys) ⇒ Object



38
39
40
41
42
43
44
# 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) ⇒ Boolean

Returns:



30
31
32
33
34
35
36
# 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