Class: CacheKeeper::Manager
- Inherits:
-
Object
- Object
- CacheKeeper::Manager
- Defined in:
- lib/cache_keeper/manager.rb
Instance Attribute Summary collapse
-
#cached_methods ⇒ Object
Returns the value of attribute cached_methods.
Instance Method Summary collapse
- #activate_if_handling(klass, method_name) ⇒ Object
- #handle(klass, method_name, options, &block) ⇒ Object
- #handled?(klass, method_name) ⇒ Boolean
-
#initialize ⇒ Manager
constructor
A new instance of Manager.
Constructor Details
#initialize ⇒ Manager
Returns a new instance of Manager.
5 6 7 |
# File 'lib/cache_keeper/manager.rb', line 5 def initialize self.cached_methods = CacheKeeper::Store.new end |
Instance Attribute Details
#cached_methods ⇒ Object
Returns the value of attribute cached_methods.
3 4 5 |
# File 'lib/cache_keeper/manager.rb', line 3 def cached_methods @cached_methods end |
Instance Method Details
#activate_if_handling(klass, method_name) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/cache_keeper/manager.rb', line 23 def activate_if_handling(klass, method_name) cached_method = cached_methods.find_by(klass, method_name) or return if requires_activation?(cached_method) if unsupported_arity?(cached_method) raise "You're trying to cache a method with parameters, which we don't currently support." end CacheKeeper::ReplaceMethod.replace(cached_method) do instance_variable_get(:"@#{method_name}") || instance_variable_set(:"@#{method_name}", cached_method.call(self)) end end end |
#handle(klass, method_name, options, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/cache_keeper/manager.rb', line 13 def handle(klass, method_name, , &block) CacheKeeper::CachedMethod.new(klass, method_name, , &block).tap do |cached_method| if (cached_method) raise "You're trying to autorefresh an ActiveRecord model, which we don't currently support." end cached_methods << cached_method end end |
#handled?(klass, method_name) ⇒ Boolean
9 10 11 |
# File 'lib/cache_keeper/manager.rb', line 9 def handled?(klass, method_name) cached_methods.find_by(klass, method_name).present? end |