Module: MethodCacher::Base::ClassMethods

Defined in:
lib/method_cacher/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cached_methodsObject

Returns the value of attribute cached_methods.



47
48
49
# File 'lib/method_cacher/base.rb', line 47

def cached_methods
  @cached_methods
end

#methods_to_be_cachedObject

Returns the value of attribute methods_to_be_cached.



47
48
49
# File 'lib/method_cacher/base.rb', line 47

def methods_to_be_cached
  @methods_to_be_cached
end

#obj_keyObject

Returns the value of attribute obj_key.



47
48
49
# File 'lib/method_cacher/base.rb', line 47

def obj_key
  @obj_key
end

#singleton_cached_methodsObject

Returns the value of attribute singleton_cached_methods.



48
49
50
# File 'lib/method_cacher/base.rb', line 48

def singleton_cached_methods
  @singleton_cached_methods
end

#singleton_methods_to_be_cachedObject

Returns the value of attribute singleton_methods_to_be_cached.



48
49
50
# File 'lib/method_cacher/base.rb', line 48

def singleton_methods_to_be_cached
  @singleton_methods_to_be_cached
end

Instance Method Details

#caches_method(*method_names) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/method_cacher/base.rb', line 50

def caches_method(*method_names)
  initialize_variables

  # process parameters
  options = method_names.extract_options!

  self.obj_key = options[:obj_key] || self.obj_key # if obj_key is given in the option, replace it with what's given
  self.methods_to_be_cached += method_names
  self.singleton_methods_to_be_cached += [options[:singleton]].flatten

  # Cache all currently defined instance methods that are given in the parameters.
  self.methods_to_be_cached.clone.each do |method_name|
    add_cached_method(method_name) if (private_instance_methods + protected_instance_methods + public_instance_methods).include?(method_name)
  end

  # Cache all currently defined singleton methods given in the parameters.
  self.singleton_methods_to_be_cached.clone.each do |method_name|
    singleton_add_cached_method(method_name) if singleton_methods.include?(method_name)
  end
end

#method_added(method_name) ⇒ Object

This callback adds caching to the methods that were not defined yet when caches_method was called.



72
73
74
75
76
# File 'lib/method_cacher/base.rb', line 72

def method_added(method_name)
  super
  initialize_variables
  add_cached_method(method_name) if self.methods_to_be_cached.include?(method_name) #and  public_instance_methods.include?(method_name) # commented section is for testing the related spec
end

#singleton_method_added(method_name) ⇒ Object

Same as method_added but for singleton methods.



79
80
81
82
83
# File 'lib/method_cacher/base.rb', line 79

def singleton_method_added(method_name)
  super
  initialize_variables
  singleton_add_cached_method(method_name) if self.singleton_methods_to_be_cached.include?(method_name)
end