Module: Pakiderm + + + +
+ +-
+
+
+
+
+
+
+
+
+
- Defined in: +
- lib/pakiderm.rb + +
Overview
Mixin that adds memoization functionality to a plain ruby object. +Memoization is selectively applied to methods by using the `memoize` +method.
+ + ++ Class Method Summary + (collapse) +
+ +-
+
+
-
+
+
+ + (Object) assignment_method(name, ivar)
+
+
+
+
+
+
+
+
+
+
+ private
+
+
+ ++ +
Generates an assignment method for the memoized value.
+
+
+
+ -
+
+
+ + (Object) memoized_ivar_for(name)
+
+
+
+
+
+
+
+
+
+
+ private
+
+
+ ++ +
Creates a valid instance variable name from a method name.
+
+
+
+ -
+
+
+ + (Object) memoized_method(name, ivar)
+
+
+
+
+
+
+
+
+
+
+ private
+
+
+ ++ +
Generates a memoized method string.
+
+
+
+
+ Instance Method Summary + (collapse) +
+ +-
+
+
-
+
+
+ - (void) memoize(*names)
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ++ +
Adds memoization to methods without parameters.
+
+
+
+
Class Method Details
+ + ++ + + (Object) assignment_method(name, ivar) + + + + + +
+ This method is part of a private API. + You should avoid using this method if possible, as it may be removed or be changed in the future. +
+ +Generates an assignment method for the memoized value.
+ + +
+ + + +67 +68 +69 +70 +71 +72 +73+ |
+
+ # File 'lib/pakiderm.rb', line 67 + +def self.assignment_method(name, ivar) + <<-ASSIGNMENT + def #{name}=(value) + #{ivar} = [value] + end + ASSIGNMENT +end+ |
+
+ + + (Object) memoized_ivar_for(name) + + + + + +
+ This method is part of a private API. + You should avoid using this method if possible, as it may be removed or be changed in the future. +
+ +Creates a valid instance variable name from a method name
+ + +
+ + + +85 +86 +87+ |
+
+ # File 'lib/pakiderm.rb', line 85 + +def self.memoized_ivar_for(name) + "@_memoized_#{name.to_s.sub(/\?$/, '_query').sub(/!$/, '_bang')}" +end+ |
+
+ + + (Object) memoized_method(name, ivar) + + + + + +
+ This method is part of a private API. + You should avoid using this method if possible, as it may be removed or be changed in the future. +
+ +Generates a memoized method string.
+ + +
+ + + +49 +50 +51 +52 +53 +54 +55 +56 +57 +58+ |
+
+ # File 'lib/pakiderm.rb', line 49 + +def self.memoized_method(name, ivar) + <<-MEMOIZATION + def #{name} + unless #{ivar} + #{ivar} = [super] + end + #{ivar}[0] + end + MEMOIZATION +end+ |
+
Instance Method Details
+ + ++ + - (void) memoize(*names) + + + + + +
This method returns an undefined value.
+Adds memoization to methods without parameters. Can receive a single method +name, or a list of methods, and an :assignable option that when true will +create an assignment method to modify the memoized value.
+ + +
+ + + +31 +32 +33 +34 +35 +36 +37 +38 +39 +40+ |
+
+ # File 'lib/pakiderm.rb', line 31 + +def memoize(*names) + assignable = names.last.is_a?(Hash) ? names.pop[:assignable] : false + memoized = Module.new + names.each do |name| + ivar = Pakiderm.memoized_ivar_for(name) + memoized.module_eval Pakiderm.memoized_method(name, ivar) + memoized.module_eval Pakiderm.assignment_method(name, ivar) if assignable + end + prepend memoized +end+ |
+