Module: Cachetier::ClassMethods
- Defined in:
- lib/cachetier/cachetier.rb
Instance Method Summary collapse
- #alias_class_method(new_name, original_name) ⇒ Object
- #cachetier(method_name, options = nil, &block) ⇒ Object
- #create_class_method(name, &block) ⇒ Object
Instance Method Details
#alias_class_method(new_name, original_name) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/cachetier/cachetier.rb', line 25 def alias_class_method(new_name, original_name) class_eval %Q{ class << self alias_method :#{new_name}, :#{original_name} end } end |
#cachetier(method_name, options = nil, &block) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/cachetier/cachetier.rb', line 33 def cachetier(method_name, = nil, &block) # given a block, create a new class method called method_name # if not given a block, a method already exists. create a method called X_with_cachetier cached_method_name = block ? method_name : "#{method_name}_with_cachetier" # create a class method that uses cachetier create_class_method cached_method_name do |key| @@cachetiers[method_name][key] end # no block given - need to rename the existing method if !block # the original method will be called via X_without_cachetier uncached_method_name = "#{method_name}_without_cachetier" alias_class_method uncached_method_name, method_name # calling the original method name will call X_with_cachetier alias_class_method method_name, cached_method_name # create a block for cachetier that calls the uncached version block = proc { |key| self.send(uncached_method_name, key) } end = Cachetier::config.merge( || {}) cache = Cachetier::Cache.new(, &block) (@@cachetiers ||= {})[method_name] = cache end |
#create_class_method(name, &block) ⇒ Object
19 20 21 22 23 |
# File 'lib/cachetier/cachetier.rb', line 19 def create_class_method(name, &block) self.class.instance_eval do define_method(name, &block) end end |