Class: JohnnyCache::MethodCache
- Inherits:
-
Object
- Object
- JohnnyCache::MethodCache
- Defined in:
- lib/johnny_cache.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.
88 89 90 91 92 93 94 |
# File 'lib/johnny_cache.rb', line 88 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
128 129 130 131 132 133 134 135 136 |
# File 'lib/johnny_cache.rb', line 128 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.
86 87 88 |
# File 'lib/johnny_cache.rb', line 86 def args @args end |
#cache_operation ⇒ Object
Returns the value of attribute cache_operation.
86 87 88 |
# File 'lib/johnny_cache.rb', line 86 def cache_operation @cache_operation end |
#caller_object ⇒ Object
Returns the value of attribute caller_object.
86 87 88 |
# File 'lib/johnny_cache.rb', line 86 def caller_object @caller_object end |
#method ⇒ Object
Returns the value of attribute method.
86 87 88 |
# File 'lib/johnny_cache.rb', line 86 def method @method end |
#options ⇒ Object
Returns the value of attribute options.
86 87 88 |
# File 'lib/johnny_cache.rb', line 86 def @options end |
Instance Method Details
#call_cache_operation(options = {}) ⇒ Object
Calls the cache based on the given cache_operation
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/johnny_cache.rb', line 112 def call_cache_operation( = {}) if cache_operation == :fetch JohnnyCache::STORE.fetch(key, ) do caller_object.send method.to_sym, *args end elsif cache_operation == :read JohnnyCache::STORE.read(key, ) elsif cache_operation == :write val = caller_object.send method.to_sym, *args JohnnyCache::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
103 104 105 106 107 |
# File 'lib/johnny_cache.rb', line 103 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 |