Module: MethodCache

Defined in:
lib/method_cache.rb

Defined Under Namespace

Modules: InvalidationMethods, MethodAdded, ModuleAdded, SingletonMethodAdded

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_cacheObject



63
64
65
# File 'lib/method_cache.rb', line 63

def self.default_cache
  @default_cache ||= {}
end

Instance Method Details

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



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

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(InvalidationMethods)
    extend(SingletonMethodAdded)
  end

  method_name = method_name.to_sym
  cached_class_methods[method_name] = nil
  begin
    # Replace class method.
    (class << self; self; end).module_eval do
      alias_method proxy.method_name_without_caching, method_name
      define_method method_name, proxy.method_with_caching
    end
  rescue NameError => e
    # The method has not been defined yet. We will alias it in singleton_method_added.
    # pp e, e.backtrace
  end
  cached_class_methods[method_name] = proxy
end

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



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
# File 'lib/method_cache.rb', line 7

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(InvalidationMethods)
      extend(MethodAdded)
    end
    
    cached_instance_methods[method_name] = nil
    begin
      # Replace instance method.
      alias_method proxy.method_name_without_caching, method_name
      define_method method_name, proxy.method_with_caching
    rescue NameError => e
      # The method has not been defined yet. We will alias it in method_added.
      # pp e, e.backtrace
    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



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/method_cache.rb', line 81

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



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/method_cache.rb', line 67

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



95
96
97
98
99
100
101
# File 'lib/method_cache.rb', line 95

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