Module: Eigenjoy::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
79
80
81
|
# File 'lib/method_cache.rb', line 79
def self.default_cache
@default_cache ||= {}
end
|
.default_cache=(new_cache) ⇒ Object
83
84
85
|
# File 'lib/method_cache.rb', line 83
def self.default_cache=(new_cache)
@default_cache = new_cache
end
|
.disable(&block) ⇒ Object
133
134
135
136
137
138
|
# File 'lib/method_cache.rb', line 133
def self.disable(&block)
@disabled, old = true, @disabled
yield
ensure
@disabled = old
end
|
.disabled? ⇒ Boolean
140
141
142
|
# File 'lib/method_cache.rb', line 140
def self.disabled?
@disabled
end
|
.verbose(&block) ⇒ Object
144
145
146
147
148
149
|
# File 'lib/method_cache.rb', line 144
def self.verbose(&block)
@verbose, old = true, @verbose
yield
ensure
@verbose = old
end
|
.verbose=(v) ⇒ Object
151
152
153
|
# File 'lib/method_cache.rb', line 151
def self.verbose=(v)
@verbose = v
end
|
.verbose? ⇒ Boolean
155
|
# File 'lib/method_cache.rb', line 155
def self.verbose?; @verbose; end
|
Instance Method Details
#cache_class_method(method_name, opts = {}) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/method_cache.rb', line 44
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
38
39
40
41
42
|
# File 'lib/method_cache.rb', line 7
def cache_method(method_name, opts = {})
method_name = method_name.to_sym
(opts[:version] ||= self.to_s) if self.class == Module 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(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
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/method_cache.rb', line 111
def cached_class_methods(method_name = nil)
if method_name
method_name = method_name.to_sym
get_ancestors.each do |klass|
next unless klass.kind_of?(Eigenjoy::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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/method_cache.rb', line 96
def cached_instance_methods(method_name = nil)
if method_name
method_name = method_name.to_sym
get_ancestors.each do |klass|
next unless klass.kind_of?(Eigenjoy::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
125
126
127
128
129
130
131
|
# File 'lib/method_cache.rb', line 125
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
72
73
74
75
76
77
|
# File 'lib/method_cache.rb', line 72
def class_method_defined?(method_name)
method(method_name)
true
rescue NameError
false
end
|
#get_ancestors ⇒ Object
87
88
89
90
91
92
93
94
|
# File 'lib/method_cache.rb', line 87
def get_ancestors
ancestors = if self.respond_to?(:ancestors)
self.ancestors
else
self.class.ancestors
end
ancestors + extended_modules
end
|