Class: MethodCachable::MethodCache
- Inherits:
-
Object
- Object
- MethodCachable::MethodCache
- Defined in:
- lib/method_cachable.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 ⇒ Object
Returns the value of attribute caller_object.
-
#method ⇒ Object
Returns the value of attribute method.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#call_cache_operation(options = {}) ⇒ Object
Calls the cache based on the given cache_operation.
-
#initialize(caller_object, *method_cache_args) ⇒ MethodCache
constructor
A new instance of MethodCache.
-
#key ⇒ Object
Uses keytar to create a key based on the method and caller if no method_key exits.
-
#method_missing(method, *args, &blk) ⇒ Object
Methods caught by method_missing are passed to the caller_object and used to :write, :read, or :fetch from the cache.
Constructor Details
#initialize(caller_object, *method_cache_args) ⇒ MethodCache
Returns a new instance of MethodCache.
86 87 88 89 90 91 92 |
# File 'lib/method_cachable.rb', line 86 def initialize(caller_object, *method_cache_args) cache_operation = method_cache_args.map {|x| x if x.is_a? Symbol }.compact.first = method_cache_args.map {|x| x if x.is_a? Hash }.compact.first self.cache_operation = cache_operation||:fetch self. = self.caller_object = caller_object 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_object and used to :write, :read, or :fetch from the cache
126 127 128 129 130 131 132 133 134 |
# File 'lib/method_cachable.rb', line 126 def method_missing(method, *args, &blk) if caller_object.respond_to? method self.method = method self.args = args call_cache_operation() else super end end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
84 85 86 |
# File 'lib/method_cachable.rb', line 84 def args @args end |
#cache_operation ⇒ Object
Returns the value of attribute cache_operation.
84 85 86 |
# File 'lib/method_cachable.rb', line 84 def cache_operation @cache_operation end |
#caller_object ⇒ Object
Returns the value of attribute caller_object.
84 85 86 |
# File 'lib/method_cachable.rb', line 84 def caller_object @caller_object end |
#method ⇒ Object
Returns the value of attribute method.
84 85 86 |
# File 'lib/method_cachable.rb', line 84 def method @method end |
#options ⇒ Object
Returns the value of attribute options.
84 85 86 |
# File 'lib/method_cachable.rb', line 84 def @options end |
Instance Method Details
#call_cache_operation(options = {}) ⇒ Object
Calls the cache based on the given cache_operation
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/method_cachable.rb', line 110 def call_cache_operation( = {}) if cache_operation == :fetch MethodCachable::STORE.fetch(key, ) do caller_object.send method.to_sym, *args end elsif cache_operation == :read MethodCachable::STORE.read(key, ) elsif cache_operation == :write val = caller_object.send method.to_sym, *args MethodCachable::STORE.write(key, val, ) end end |
#key ⇒ Object
Uses keytar to create a key based on the method and caller if no method_key exits
101 102 103 104 105 |
# File 'lib/method_cachable.rb', line 101 def key key_method = "#{method}_key".to_sym key = caller_object.send key_method, *args if caller_object.respond_to? key_method key ||= caller_object.build_key(:name => method, :args => args) end |