Class: MethodCacheable::MethodCache
- Inherits:
-
Object
- Object
- MethodCacheable::MethodCache
- Defined in:
- lib/method_cacheable.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#cache_operation ⇒ Object
Returns the value of attribute cache_operation.
-
#caller ⇒ Object
Returns the value of attribute caller.
-
#method ⇒ Object
Returns the value of attribute method.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#call ⇒ Object
Calls the cache based on the given cache_operation.
-
#delete(method, *args) ⇒ Object
Removes the current key from the cache store.
-
#exist?(method, *args) ⇒ boolean
(also: #exists?)
Checks to see if the key exists in the cache store.
- #for(method, *args) ⇒ Object
-
#initialize(caller, *method_cache_args) ⇒ MethodCache
constructor
A new instance of MethodCache.
-
#key(tmp_method = nil, *tmp_args) ⇒ Object
Uses keytar to create a key based on the method and caller if no method_key exits Method and arguement can optionally be supplied.
-
#method_missing(method, *args, &blk) ⇒ Object
Methods caught by method_missing are passed to the caller and used to :write, :read, or :fetch from the cache.
- #read(method, *args) ⇒ Object
- #send_to_caller ⇒ Object
- #write(method, *args, &block) ⇒ Object
Constructor Details
#initialize(caller, *method_cache_args) ⇒ MethodCache
Returns a new instance of MethodCache.
97 98 99 100 101 |
# File 'lib/method_cacheable.rb', line 97 def initialize(caller, *method_cache_args) self.caller = caller self.cache_operation = method_cache_args.map {|x| x if x.is_a? Symbol }.compact.first||:fetch self. = method_cache_args.map {|x| x if x.is_a? Hash }.compact.first end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &blk) ⇒ Object
Methods caught by method_missing are passed to the caller and used to :write, :read, or :fetch from the cache
177 178 179 180 181 182 183 184 185 |
# File 'lib/method_cacheable.rb', line 177 def method_missing(method, *args, &blk) self.method = method self.args = args if caller.respond_to? method call else send_to_caller end end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
90 91 92 |
# File 'lib/method_cacheable.rb', line 90 def args @args end |
#cache_operation ⇒ Object
Returns the value of attribute cache_operation.
90 91 92 |
# File 'lib/method_cacheable.rb', line 90 def cache_operation @cache_operation end |
#caller ⇒ Object
Returns the value of attribute caller.
90 91 92 |
# File 'lib/method_cacheable.rb', line 90 def caller @caller end |
#method ⇒ Object
Returns the value of attribute method.
90 91 92 |
# File 'lib/method_cacheable.rb', line 90 def method @method end |
#options ⇒ Object
Returns the value of attribute options.
90 91 92 |
# File 'lib/method_cacheable.rb', line 90 def @options end |
Instance Method Details
#call ⇒ Object
Calls the cache based on the given cache_operation
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/method_cacheable.rb', line 106 def call case cache_operation when :fetch MethodCacheable.store.fetch(key, ) do send_to_caller end when :read read(method, args) when :write write(method, args) do send_to_caller end end end |
#delete(method, *args) ⇒ Object
Removes the current key from the cache store
159 160 161 162 163 |
# File 'lib/method_cacheable.rb', line 159 def delete(method, *args) self.method = method self.args = args MethodCacheable.store.delete(key, ) end |
#exist?(method, *args) ⇒ boolean Also known as: exists?
Checks to see if the key exists in the cache store
167 168 169 170 171 |
# File 'lib/method_cacheable.rb', line 167 def exist?(method, *args) self.method = method self.args = args MethodCacheable.store.exist?(key) end |
#for(method, *args) ⇒ Object
134 135 136 137 138 |
# File 'lib/method_cacheable.rb', line 134 def for(method , *args) self.method = method self.args = args self end |
#key(tmp_method = nil, *tmp_args) ⇒ Object
Uses keytar to create a key based on the method and caller if no method_key exits Method and arguement can optionally be supplied
150 151 152 153 154 155 156 |
# File 'lib/method_cacheable.rb', line 150 def key(tmp_method = nil, *tmp_args) tmp_method = method if tmp_method.blank? tmp_args = args if tmp_args.blank? key_method = "#{tmp_method}_key".to_sym key = caller.send key_method, *tmp_args if caller.respond_to? key_method key ||= caller.build_key(:name => tmp_method, :args => tmp_args) end |
#read(method, *args) ⇒ Object
130 131 132 |
# File 'lib/method_cacheable.rb', line 130 def read(method, *args) MethodCacheable.store.read(key, ) end |
#send_to_caller ⇒ Object
121 122 123 |
# File 'lib/method_cacheable.rb', line 121 def send_to_caller caller.send method.to_sym, *args end |
#write(method, *args, &block) ⇒ Object
125 126 127 128 |
# File 'lib/method_cacheable.rb', line 125 def write(method, *args, &block) val = block.call MethodCacheable.store.write(key, val, ) end |