Module: MethodCache

Defined in:
lib/method_cache.rb,
lib/method_cache/proxy.rb,
lib/method_cache/local_cache.rb

Defined Under Namespace

Modules: HelperMethods, MethodAdded, ModuleAdded, SingletonMethodAdded Classes: LocalCache, Proxy

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_cacheObject



73
74
75
# File 'lib/method_cache.rb', line 73

def self.default_cache
  @default_cache ||= LocalCache.new
end

.disable(value = true, &block) ⇒ Object



113
114
115
116
117
118
# File 'lib/method_cache.rb', line 113

def self.disable(value = true, &block)
  @disabled, old = true, @disabled
  yield
ensure
  @disabled = old
end

.disabled?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/method_cache.rb', line 120

def self.disabled?
  @disabled
end

Instance Method Details

#cache_class_method(method_name, opts = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/method_cache.rb', line 38

def cache_class_method(method_name, opts = {})
  method_name = method_name.to_sym
  proxy = opts.kind_of?(Proxy) ? opts : Proxy.new(method_name, opts)

  return if methods.include?(proxy.method_name_without_caching)

  if cached_class_methods.empty?
    extend(HelperMethods)
    extend(SingletonMethodAdded)
  end

  method_name = method_name.to_sym
  cached_class_methods[method_name] = nil
  if class_method_defined?(method_name)
    (class << self; self; end).module_eval do
      if proxy.opts[:counter]
        define_method "increment_#{method_name}", proxy.counter_method(:increment)
        define_method "decrement_#{method_name}", proxy.counter_method(:decrement)
      end

      # Replace class method.
      alias_method proxy.method_name_without_caching, method_name
      define_method method_name, proxy.method_with_caching
    end
  end
  cached_class_methods[method_name] = proxy
end

#cache_method(method_name, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/method_cache.rb', line 6

def cache_method(method_name, opts = {})
  method_name = method_name.to_sym
  proxy = opts.kind_of?(Proxy) ? opts : Proxy.new(method_name, opts)

  if self.class == Class
    return if instance_methods.include?(proxy.method_name_without_caching)

    if cached_instance_methods.empty?
      include(HelperMethods)
      extend(MethodAdded)
    end

    cached_instance_methods[method_name] = nil
    if method_defined?(method_name) or private_method_defined?(method_name)
      if proxy.opts[:counter]
        define_method "increment_#{method_name}", proxy.counter_method(:increment)
        define_method "decrement_#{method_name}", proxy.counter_method(:decrement)
      end

      # Replace instance method.
      alias_method proxy.method_name_without_caching, method_name
      define_method method_name, proxy.method_with_caching
    end
    cached_instance_methods[method_name] = proxy

  elsif self.class == Module
    # We will alias all methods when the module is mixed-in.
    extend(ModuleAdded) if cached_module_methods.empty?
    cached_module_methods[method_name.to_sym] = proxy
  end
end

#cached_class_methods(method_name = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/method_cache.rb', line 91

def cached_class_methods(method_name = nil)
  if method_name
    method_name = method_name.to_sym
    ancestors.each do |klass|
      next unless klass.kind_of?(MethodCache)
      proxy = klass.cached_class_methods[method_name]
      return proxy if proxy
    end
    nil
  else
    @cached_class_methods ||= {}
  end
end

#cached_instance_methods(method_name = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/method_cache.rb', line 77

def cached_instance_methods(method_name = nil)
  if method_name
    method_name = method_name.to_sym
    ancestors.each do |klass|
      next unless klass.kind_of?(MethodCache)
      proxy = klass.cached_instance_methods[method_name]
      return proxy if proxy
    end
    nil
  else
    @cached_instance_methods ||= {}
  end
end

#cached_module_methods(method_name = nil) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/method_cache.rb', line 105

def cached_module_methods(method_name = nil)
  if method_name
    cached_module_methods[method_name.to_sym]
  else
    @cached_module_methods ||= {}
  end
end

#class_method_defined?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
# File 'lib/method_cache.rb', line 66

def class_method_defined?(method_name)
  method(method_name)
  true
rescue NameError
  false
end