doc/api/classes/ActiveSupport/CachingTools/HashCaching.html
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Module: ActiveSupport::CachingTools::HashCaching</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
<script type="text/javascript">
// <![CDATA[
function popupCode( url ) {
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
}
function toggleCode( id ) {
if ( document.getElementById )
elem = document.getElementById( id );
else if ( document.all )
elem = eval( "document.all." + id );
else
return false;
elemStyle = elem.style;
if ( elemStyle.display != "block" ) {
elemStyle.display = "block"
} else {
elemStyle.display = "none"
}
return true;
}
// Make codeblocks hidden by default
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
// ]]>
</script>
</head>
<body>
<div id="classHeader">
<table class="header-table">
<tr class="top-aligned-row">
<td><strong>Module</strong></td>
<td class="class-name-in-header">ActiveSupport::CachingTools::HashCaching</td>
</tr>
<tr class="top-aligned-row">
<td><strong>In:</strong></td>
<td>
<a href="../../../files/vendor/rails/activesupport/lib/active_support/caching_tools_rb.html">
vendor/rails/activesupport/lib/active_support/caching_tools.rb
</a>
<br />
</td>
</tr>
</table>
</div>
<!-- banner header -->
<div id="bodyContent">
<div id="contextContent">
<div id="description">
<p>
Provide shortcuts to simply the creation of nested default hashes. This
pattern is useful, common practice, and unsightly when done manually.
</p>
</div>
</div>
<div id="method-list">
<h3 class="section-bar">Methods</h3>
<div class="name-list">
<a href="#M000319">hash_cache</a>
</div>
</div>
</div>
<!-- if includes -->
<div id="section">
<!-- if method_list -->
<div id="methods">
<h3 class="section-bar">Public Instance methods</h3>
<div id="method-M000319" class="method-detail">
<a name="M000319"></a>
<div class="method-heading">
<a href="#M000319" class="method-signature">
<span class="method-name">hash_cache</span><span class="method-args">(method_name, options = {})</span>
</a>
</div>
<div class="method-description">
<p>
Dynamically create a nested hash structure used to cache calls to
<tt>method_name</tt> The cache method is named +#{method_name}_cache+
unless :as => :alternate_name is given.
</p>
<p>
The hash structure is created using nested Hash.new. For example:
</p>
<pre>
def slow_method(a, b) a ** b end
</pre>
<p>
can be cached using <a href="HashCaching.html#M000319">hash_cache</a>
:slow_method, which will define the method slow_method_cache. We can then
find the result of a ** b using:
</p>
<pre>
slow_method_cache[a][b]
</pre>
<p>
The hash structure returned by slow_method_cache would look like this:
</p>
<pre>
Hash.new do |as, a|
as[a] = Hash.new do |bs, b|
bs[b] = slow_method(a, b)
end
end
</pre>
<p>
The generated code is actually compressed onto a single line to maintain
sensible backtrace signatures.
</p>
<p><a class="source-toggle" href="#"
onclick="toggleCode('M000319-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000319-source">
<pre>
<span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/caching_tools.rb, line 31</span>
31: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">hash_cache</span>(<span class="ruby-identifier">method_name</span>, <span class="ruby-identifier">options</span> = {})
32: <span class="ruby-identifier">selector</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:as</span>] <span class="ruby-operator">||</span> <span class="ruby-node">"#{method_name}_cache"</span>
33: <span class="ruby-identifier">method</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">instance_method</span>(<span class="ruby-identifier">method_name</span>)
34:
35: <span class="ruby-identifier">args</span> = []
36: <span class="ruby-identifier">code</span> = <span class="ruby-node">"def #{selector}(); @#{selector} ||= "</span>
37:
38: (<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-identifier">method</span>.<span class="ruby-identifier">arity</span>).<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">n</span><span class="ruby-operator">|</span>
39: <span class="ruby-identifier">args</span> <span class="ruby-operator"><<</span> <span class="ruby-node">"v#{n}"</span>
40: <span class="ruby-identifier">code</span> <span class="ruby-operator"><<</span> <span class="ruby-node">"Hash.new {|h#{n}, v#{n}| h#{n}[v#{n}] = "</span>
41: <span class="ruby-keyword kw">end</span>
42:
43: <span class="ruby-comment cmt"># Add the method call with arguments, followed by closing braces and end.</span>
44: <span class="ruby-identifier">code</span> <span class="ruby-operator"><<</span> <span class="ruby-node">"#{method_name}(#{args * ', '}) #{'}' * method.arity} end"</span>
45:
46: <span class="ruby-comment cmt"># Extract the line number information from the caller. Exceptions arising</span>
47: <span class="ruby-comment cmt"># in the generated code should point to the +hash_cache :...+ line.</span>
48: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">caller</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">&&</span> <span class="ruby-regexp re">/^(.*):(\d+)$/</span> <span class="ruby-operator">=~</span> <span class="ruby-identifier">caller</span>[<span class="ruby-value">0</span>]
49: <span class="ruby-identifier">file</span>, <span class="ruby-identifier">line_number</span> = <span class="ruby-identifier">$1</span>, <span class="ruby-identifier">$2</span>.<span class="ruby-identifier">to_i</span>
50: <span class="ruby-keyword kw">else</span> <span class="ruby-comment cmt"># We can't give good trackback info; fallback to this line:</span>
51: <span class="ruby-identifier">file</span>, <span class="ruby-identifier">line_number</span> = <span class="ruby-keyword kw">__FILE__</span>, <span class="ruby-keyword kw">__LINE__</span>
52: <span class="ruby-keyword kw">end</span>
53:
54: <span class="ruby-comment cmt"># We use eval rather than building proc's because it allows us to avoid</span>
55: <span class="ruby-comment cmt"># linking the Hash's to this method's binding. Experience has shown that</span>
56: <span class="ruby-comment cmt"># doing so can cause obtuse memory leaks.</span>
57: <span class="ruby-identifier">class_eval</span> <span class="ruby-identifier">code</span>, <span class="ruby-identifier">file</span>, <span class="ruby-identifier">line_number</span>
58: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div id="validator-badges">
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>
</body>
</html>