Module: XCCache::Cacheable
- Defined in:
- lib/xccache/core/cacheable.rb
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.included(base) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/xccache/core/cacheable.rb', line 20 def self.included(base) base.extend(self) module_name = base.send(:__cacheable_module_name) remove_const(module_name) if const_defined?(module_name) base.prepend(const_set(module_name, Module.new)) end |
Instance Method Details
#__cacheable_module_name ⇒ Object
16 17 18 |
# File 'lib/xccache/core/cacheable.rb', line 16 def __cacheable_module_name "#{name}_Cacheable".gsub(':', '_') end |
#cacheable(*method_names) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/xccache/core/cacheable.rb', line 3 def cacheable(*method_names) method_names.each do |method_name| const_get(__cacheable_module_name).class_eval do define_method(method_name) do |*args, **kwargs| @_cache ||= {} @_cache[method_name] ||= {} @_cache[method_name][args.hash | kwargs.hash] ||= method(method_name).super_method.call(*args, **kwargs) end end end end |