Module: MethodCache
- Defined in:
- lib/method_cache.rb,
lib/method_cache/proxy.rb
Defined Under Namespace
Modules: InvalidationMethods, MethodAdded, ModuleAdded, SingletonMethodAdded
Classes: Proxy
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.default_cache ⇒ Object
74
75
76
|
# File 'lib/method_cache.rb', line 74
def self.default_cache
@default_cache ||= {}
end
|
.disable(&block) ⇒ Object
114
115
116
117
118
119
|
# File 'lib/method_cache.rb', line 114
def self.disable(&block)
@disabled, old = true, @disabled
yield
ensure
@disabled = old
end
|
.disabled? ⇒ Boolean
121
122
123
|
# File 'lib/method_cache.rb', line 121
def self.disabled?
@disabled
end
|
Instance Method Details
#cache_class_method(method_name, opts = {}) ⇒ Object
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
65
|
# File 'lib/method_cache.rb', line 39
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
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
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
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
37
|
# 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
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
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
extend(ModuleAdded) if cached_module_methods.empty?
cached_module_methods[method_name.to_sym] = proxy
end
end
|
#cached_class_methods(method_name = nil) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/method_cache.rb', line 92
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
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/method_cache.rb', line 78
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
106
107
108
109
110
111
112
|
# File 'lib/method_cache.rb', line 106
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
67
68
69
70
71
72
|
# File 'lib/method_cache.rb', line 67
def class_method_defined?(method_name)
method(method_name)
true
rescue NameError
false
end
|