module Pakiderm
Public: Mixin that adds memoization functionality to a plain ruby object. Memoization is selectively applied to methods by using the `memoize` method.
Examples
class PersonQuery extend Pakiderm memoize \ def recent_activity Activities.get("/api/#{person.id}") end end
Constants
- VERSION
Public Class Methods
memoized_ivar_for(name)
click to toggle source
Internal: Creates a valid name for the instance variable used to store the memoized value for a method.
name - Name of the method to memoize.
Returns a String.
# File lib/pakiderm.rb, line 47 def self.memoized_ivar_for(name) "@pakiderm_#{ name.to_s.gsub('?', '_question_mark').sub('!', '_bang') }" end
Public Instance Methods
memoize(*names)
click to toggle source
Public: Adds memoization to methods without parameters using Module#prepend.
names - Names of the methods to be memoized.
Examples
memoize :ready?, :complex_formula, :long_running_method!
Returns nothing.
Calls superclass method
# File lib/pakiderm.rb, line 25 def memoize(*names) prepend Module.new { names.each do |name| ivar = Pakiderm.memoized_ivar_for(name) define_method(name) { if instance_variable_defined?(ivar) instance_variable_get(ivar) else instance_variable_set(ivar, super()) end } end } end