Permalink
Browse files

Added nested routes & resources, cleaned up objects by stuffing more …

…shit in the prototype of each.
  • Loading branch information...
1 parent fa7e1c4 commit 22fd062cf761003a91c69197ae65ab7a77bc06ce @kieran committed Sep 5, 2012
Showing with 1,120 additions and 832 deletions.
  1. +7 −1 docs/docco.css
  2. +2 −2 docs/helpers.html
  3. +39 −47 docs/key.html
  4. +35 −0 docs/resource.html
  5. +195 −163 docs/route.html
  6. +78 −87 docs/router.html
  7. +79 −74 lib/key.js
  8. +36 −0 lib/resource.js
  9. +372 −269 lib/route.js
  10. +193 −183 lib/router.js
  11. +84 −6 tests/barista.test.js
View
8 docs/docco.css
@@ -21,6 +21,12 @@ h1, h2, h3, h4, h5, h6 {
h1 {
margin-top: 40px;
}
+hr {
+ border: 0 none;
+ border-top: 1px solid #e5e5ee;
+ height: 1px;
+ margin: 20px 0;
+}
#container {
position: relative;
}
@@ -115,7 +121,7 @@ table td {
}
pre, tt, code {
font-size: 12px; line-height: 18px;
- font-family: Monaco, Consolas, "Lucida Console", monospace;
+ font-family: Menlo, Monaco, Consolas, "Lucida Console", monospace;
margin: 0; padding: 0;
}
View
4 docs/helpers.html
@@ -1,4 +1,4 @@
-<!DOCTYPE html> <html> <head> <title>helpers.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="helpers.html"> helpers.js </a> <a class="source" href="key.html"> key.js </a> <a class="source" href="route.html"> route.js </a> <a class="source" href="router.html"> router.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> helpers.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> <p>CamelCase
+<!DOCTYPE html> <html> <head> <title>helpers.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="helpers.html"> helpers.js </a> <a class="source" href="key.html"> key.js </a> <a class="source" href="resource.html"> resource.js </a> <a class="source" href="route.html"> route.js </a> <a class="source" href="router.html"> router.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> helpers.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> <p>CamelCase
var camelize = function(str,capitalize){
var ret = str.replace( /[^a-zA-Z][a-zA-Z]/g, function(str){
return str[1].toUpperCase();
@@ -14,7 +14,7 @@
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>deep object mixer</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">exports</span><span class="p">.</span><span class="nx">mixin</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span>
<span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">)</span>
- <span class="k">for</span> <span class="p">(</span> <span class="nx">i</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">args</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span> <span class="p">)</span> <span class="p">{</span>
+ <span class="k">for</span> <span class="p">(</span> <span class="kd">var</span> <span class="nx">i</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">args</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span> <span class="p">)</span> <span class="p">{</span>
<span class="k">for</span> <span class="p">(</span> <span class="kd">var</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">args</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">kindof</span><span class="p">(</span><span class="nx">args</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="nx">prop</span><span class="p">])</span> <span class="o">==</span> <span class="s1">&#39;object&#39;</span> <span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>deep copy</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="nx">prop</span><span class="p">]</span> <span class="o">=</span> <span class="nx">mixin</span><span class="p">(</span> <span class="p">{},</span> <span class="nx">args</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="nx">prop</span><span class="p">]</span> <span class="p">)</span>
View
86 docs/key.html
@@ -1,73 +1,65 @@
-<!DOCTYPE html> <html> <head> <title>key.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="helpers.html"> helpers.js </a> <a class="source" href="key.html"> key.js </a> <a class="source" href="route.html"> route.js </a> <a class="source" href="router.html"> router.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> key.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> <h1>new Key( name, optional )</h1>
+<!DOCTYPE html> <html> <head> <title>key.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="helpers.html"> helpers.js </a> <a class="source" href="key.html"> key.js </a> <a class="source" href="resource.html"> resource.js </a> <a class="source" href="route.html"> route.js </a> <a class="source" href="router.html"> router.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> key.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> <h1>new Key( name, optional )</h1>
<p>those variable thingies</p>
<pre><code>key = new Key('name')
key = new Key('name', true)
-</code></pre> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">Key</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">optional</span><span class="p">,</span> <span class="nx">glob</span> <span class="p">)</span> <span class="p">{</span>
+</code></pre> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">Key</span> <span class="o">=</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">Key</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">optional</span><span class="p">,</span> <span class="nx">glob</span> <span class="p">)</span> <span class="p">{</span>
- <span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="nx">name</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">optional</span> <span class="o">=</span> <span class="p">(</span><span class="nx">optional</span><span class="o">===</span><span class="kc">true</span><span class="p">)</span> <span class="o">?</span> <span class="kc">true</span> <span class="o">:</span> <span class="kc">false</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">glob</span> <span class="o">=</span> <span class="p">(</span><span class="nx">glob</span><span class="o">===</span><span class="kc">true</span><span class="p">)</span> <span class="o">?</span> <span class="kc">true</span> <span class="o">:</span> <span class="kc">false</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">regex</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">glob</span> <span class="o">?</span> <span class="sr">/[\w\-\/]+?/</span> <span class="o">:</span> <span class="sr">/[\w\-]+/</span> <span class="c1">// default url-friendly regex</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>special defaults for controllers &amp; actions, which will always be function-name-safe</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">name</span> <span class="o">==</span> <span class="s1">&#39;controller&#39;</span> <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">name</span> <span class="o">==</span> <span class="s1">&#39;action&#39;</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">regex</span> <span class="o">=</span> <span class="sr">/[a-zA-Z_][\w\-]*/</span>
+ <span class="p">}</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="nx">name</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">optional</span> <span class="o">=</span> <span class="p">(</span><span class="nx">optional</span><span class="o">===</span><span class="kc">true</span><span class="p">)</span> <span class="o">?</span> <span class="kc">true</span> <span class="o">:</span> <span class="kc">false</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">glob</span> <span class="o">=</span> <span class="p">(</span><span class="nx">glob</span><span class="o">===</span><span class="kc">true</span><span class="p">)</span> <span class="o">?</span> <span class="kc">true</span> <span class="o">:</span> <span class="kc">false</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">regex</span> <span class="o">=</span> <span class="nx">self</span><span class="p">.</span><span class="nx">glob</span> <span class="o">?</span> <span class="sr">/[\w\-\/]+?/</span> <span class="o">:</span> <span class="sr">/[\w\-]+/</span> <span class="c1">// default url-friendly regex</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>special defaults for controllers &amp; actions, which will always be function-name-safe</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">name</span> <span class="o">==</span> <span class="s1">&#39;controller&#39;</span> <span class="o">||</span> <span class="nx">self</span><span class="p">.</span><span class="nx">name</span> <span class="o">==</span> <span class="s1">&#39;action&#39;</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">regex</span> <span class="o">=</span> <span class="sr">/[a-zA-Z_][\w\-]*/</span>
- <span class="p">}</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h2>key.regexString()</h2>
+ <span class="k">return</span> <span class="k">this</span> <span class="c1">// just in case we forgot the new operator</span>
+<span class="p">};</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h2>key.regexString()</h2>
<p>makes a regex string of the key - used by key.test()</p>
-<p>returns a string of this keys regex</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">regexString</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
- <span class="kd">var</span> <span class="nx">ret</span> <span class="o">=</span> <span class="nb">String</span><span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">regex</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/^\//</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\/[gis]?$/</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
- <span class="k">if</span> <span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">optional</span><span class="p">)</span> <span class="p">{</span>
- <span class="k">return</span> <span class="s1">&#39;(&#39;</span> <span class="o">+</span> <span class="nx">ret</span> <span class="o">+</span> <span class="s1">&#39;)?&#39;</span>
- <span class="p">}</span>
- <span class="k">return</span> <span class="s1">&#39;(&#39;</span> <span class="o">+</span> <span class="nx">ret</span> <span class="o">+</span> <span class="s1">&#39;)&#39;</span>
- <span class="p">};</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <h2>key.test( string )</h2>
+<p>returns a string of this keys regex</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Key</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">regexString</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">ret</span> <span class="o">=</span> <span class="nb">String</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">regex</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/^\//</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\/[gis]?$/</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
+ <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">optional</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="s1">&#39;(&#39;</span> <span class="o">+</span> <span class="nx">ret</span> <span class="o">+</span> <span class="s1">&#39;)?&#39;</span>
+ <span class="p">}</span>
+ <span class="k">return</span> <span class="s1">&#39;(&#39;</span> <span class="o">+</span> <span class="nx">ret</span> <span class="o">+</span> <span class="s1">&#39;)&#39;</span>
+<span class="p">};</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <h2>key.test( string )</h2>
<p>validates a string using the key's regex pattern</p>
-<p>returns true/false if the string matches</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">test</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">string</span> <span class="p">)</span> <span class="p">{</span>
- <span class="k">return</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s1">&#39;^&#39;</span><span class="o">+</span><span class="nx">self</span><span class="p">.</span><span class="nx">regexString</span><span class="p">()</span><span class="o">+</span><span class="s1">&#39;$&#39;</span><span class="p">).</span><span class="nx">test</span><span class="p">(</span><span class="nx">string</span><span class="p">)</span>
- <span class="p">};</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <h2>key.url( string )</h2>
+<p>returns true/false if the string matches</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Key</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">test</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">string</span> <span class="p">)</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s1">&#39;^&#39;</span><span class="o">+</span><span class="k">this</span><span class="p">.</span><span class="nx">regexString</span><span class="p">()</span><span class="o">+</span><span class="s1">&#39;$&#39;</span><span class="p">).</span><span class="nx">test</span><span class="p">(</span><span class="nx">string</span><span class="p">)</span>
+<span class="p">};</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <h2>key.url( string )</h2>
<p>returns a string for buulding the url
-if it matches the key conditions</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">url</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">string</span> <span class="p">)</span> <span class="p">{</span>
- <span class="k">if</span> <span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">string</span><span class="p">))</span> <span class="p">{</span>
- <span class="cm">/*</span>
-<span class="cm"> -- no longer needed</span>
-<span class="cm"> snake_caseify the controller, if there is one</span>
-<span class="cm"> if (self.name == &#39;controller&#39;) return snakeize(string)</span>
-<span class="cm"> */</span>
- <span class="k">return</span> <span class="nx">string</span>
- <span class="p">}</span>
- <span class="k">return</span> <span class="kc">false</span> <span class="c1">// doesn&#39;t match, let it go</span>
- <span class="p">};</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <h2>key.where( conditions )</h2>
+if it matches the key conditions</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Key</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">url</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">string</span> <span class="p">)</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">string</span><span class="p">)</span> <span class="o">?</span> <span class="nx">string</span> <span class="o">:</span> <span class="kc">false</span>
+<span class="p">};</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <h2>key.where( conditions )</h2>
<p>adds conditions that the key must match</p>
-<p>returns the key... because it can?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">where</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">conditions</span> <span class="p">)</span> <span class="p">{</span>
-
- <span class="kd">var</span> <span class="nx">condition</span> <span class="o">=</span> <span class="nx">conditions</span><span class="p">[</span><span class="nx">self</span><span class="p">.</span><span class="nx">name</span><span class="p">]</span>
+<p>returns the key... because it can?</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Key</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">where</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">conditions</span> <span class="p">)</span> <span class="p">{</span>
- <span class="k">if</span> <span class="p">(</span><span class="nx">condition</span> <span class="k">instanceof</span> <span class="nb">RegExp</span><span class="p">)</span> <span class="nx">self</span><span class="p">.</span><span class="nx">regex</span> <span class="o">=</span> <span class="nx">condition</span> <span class="c1">// e.g. /\d+/</span>
+ <span class="kd">var</span> <span class="nx">condition</span> <span class="o">=</span> <span class="nx">conditions</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">name</span><span class="p">]</span>
- <span class="k">if</span> <span class="p">(</span><span class="nx">condition</span> <span class="k">instanceof</span> <span class="nb">String</span><span class="p">)</span> <span class="nx">self</span><span class="p">.</span><span class="nx">regex</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="nx">condition</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/^\//</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\/[gis]?$/</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">))</span> <span class="c1">// e.g. &quot;\d+&quot;</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>an array of allowed values, e.g. ['stop','play','pause']</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">condition</span> <span class="k">instanceof</span> <span class="nb">Array</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">condition</span> <span class="o">=</span> <span class="nx">condition</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">cond</span><span class="p">){</span>
- <span class="k">if</span> <span class="p">(</span><span class="nx">cond</span> <span class="k">instanceof</span> <span class="nb">RegExp</span><span class="p">)</span> <span class="k">return</span> <span class="nx">cond</span><span class="p">.</span><span class="nx">toString</span><span class="p">()</span>
- <span class="k">return</span> <span class="nx">cond</span>
- <span class="p">}).</span><span class="nx">map</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">cond</span><span class="p">){</span>
- <span class="k">return</span> <span class="nx">cond</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/^\//</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\/[gis]?$/</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
- <span class="p">})</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">regex</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="nx">condition</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;|&#39;</span><span class="p">))</span>
- <span class="p">}</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">condition</span> <span class="k">instanceof</span> <span class="nb">RegExp</span><span class="p">)</span> <span class="k">this</span><span class="p">.</span><span class="nx">regex</span> <span class="o">=</span> <span class="nx">condition</span> <span class="c1">// e.g. /\d+/</span>
- <span class="k">return</span> <span class="nx">self</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">condition</span> <span class="k">instanceof</span> <span class="nb">String</span><span class="p">)</span> <span class="k">this</span><span class="p">.</span><span class="nx">regex</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="nx">condition</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/^\//</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\/[gis]?$/</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">))</span> <span class="c1">// e.g. &quot;\d+&quot;</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>an array of allowed values, e.g. ['stop','play','pause']</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">condition</span> <span class="k">instanceof</span> <span class="nb">Array</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">condition</span> <span class="o">=</span> <span class="nx">condition</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">cond</span><span class="p">){</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">cond</span> <span class="k">instanceof</span> <span class="nb">RegExp</span><span class="p">)</span> <span class="k">return</span> <span class="nx">cond</span><span class="p">.</span><span class="nx">toString</span><span class="p">()</span>
+ <span class="k">return</span> <span class="nx">cond</span>
+ <span class="p">}).</span><span class="nx">map</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">cond</span><span class="p">){</span>
+ <span class="k">return</span> <span class="nx">cond</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/^\//</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\/[gis]?$/</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
+ <span class="p">})</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">regex</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="nx">condition</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;|&#39;</span><span class="p">))</span>
<span class="p">}</span>
- <span class="k">return</span> <span class="nx">self</span> <span class="c1">// just in case we forgot the new operator</span>
-<span class="p">};</span>
+ <span class="k">return</span> <span class="k">this</span> <span class="c1">// chainable</span>
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <h2>key.toString()</h2>
-<span class="nx">exports</span><span class="p">.</span><span class="nx">Key</span> <span class="o">=</span> <span class="nx">Key</span>
+<p>returns a unique id that can be compared to other parts</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Key</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">toString</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span>
+ <span class="k">return</span> <span class="s1">&#39;key-&#39;</span><span class="o">+</span><span class="k">this</span><span class="p">.</span><span class="nx">name</span>
+<span class="p">}</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
35 docs/resource.html
@@ -0,0 +1,35 @@
+<!DOCTYPE html> <html> <head> <title>resource.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="helpers.html"> helpers.js </a> <a class="source" href="key.html"> key.js </a> <a class="source" href="resource.html"> resource.js </a> <a class="source" href="route.html"> route.js </a> <a class="source" href="router.html"> router.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> resource.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">snakeize</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./helpers&#39;</span><span class="p">).</span><span class="nx">snakeize</span>
+ <span class="p">,</span> <span class="nx">mixin</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./helpers&#39;</span><span class="p">).</span><span class="nx">mixin</span>
+
+<span class="kd">var</span> <span class="nx">Resource</span> <span class="o">=</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">Resource</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">base</span><span class="p">,</span> <span class="nx">controller</span> <span class="p">){</span>
+
+ <span class="kd">var</span> <span class="nx">controller_slug</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="nx">snakeize</span><span class="p">(</span><span class="nx">controller</span><span class="p">)</span>
+
+ <span class="k">this</span><span class="p">.</span><span class="nx">routes</span> <span class="o">=</span> <span class="p">[]</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>set up the actual routes for the resource</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">routes</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">collection_route</span> <span class="o">=</span>
+ <span class="nx">base</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">controller_slug</span><span class="o">+</span><span class="s1">&#39;(.:format)&#39;</span><span class="p">,</span> <span class="s1">&#39;GET&#39;</span><span class="p">).</span><span class="nx">to</span><span class="p">(</span><span class="nx">controller</span><span class="o">+</span><span class="s1">&#39;.index&#39;</span><span class="p">)</span>
+ <span class="p">,</span> <span class="nx">base</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">controller_slug</span><span class="o">+</span><span class="s1">&#39;/add(.:format)&#39;</span><span class="p">,</span> <span class="s1">&#39;GET&#39;</span><span class="p">).</span><span class="nx">to</span><span class="p">(</span><span class="nx">controller</span><span class="o">+</span><span class="s1">&#39;.add&#39;</span><span class="p">)</span>
+ <span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">member_route</span> <span class="o">=</span>
+ <span class="nx">base</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">controller_slug</span><span class="o">+</span><span class="s1">&#39;/:id(.:format)&#39;</span><span class="p">,</span> <span class="s1">&#39;GET&#39;</span><span class="p">).</span><span class="nx">to</span><span class="p">(</span><span class="nx">controller</span><span class="o">+</span><span class="s1">&#39;.show&#39;</span><span class="p">)</span>
+ <span class="p">,</span> <span class="nx">base</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">controller_slug</span><span class="o">+</span><span class="s1">&#39;/:id/edit(.:format)&#39;</span><span class="p">,</span> <span class="s1">&#39;GET&#39;</span><span class="p">).</span><span class="nx">to</span><span class="p">(</span><span class="nx">controller</span><span class="o">+</span><span class="s1">&#39;.edit&#39;</span><span class="p">)</span>
+ <span class="p">,</span> <span class="nx">base</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">controller_slug</span><span class="o">+</span><span class="s1">&#39;(.:format)&#39;</span><span class="p">,</span> <span class="s1">&#39;POST&#39;</span><span class="p">).</span><span class="nx">to</span><span class="p">(</span><span class="nx">controller</span><span class="o">+</span><span class="s1">&#39;.create&#39;</span><span class="p">)</span>
+ <span class="p">,</span> <span class="nx">base</span><span class="p">.</span><span class="nx">put</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">controller_slug</span><span class="o">+</span><span class="s1">&#39;/:id(.:format)&#39;</span><span class="p">,</span> <span class="s1">&#39;PUT&#39;</span><span class="p">).</span><span class="nx">to</span><span class="p">(</span><span class="nx">controller</span><span class="o">+</span><span class="s1">&#39;.update&#39;</span><span class="p">)</span>
+ <span class="p">,</span> <span class="nx">base</span><span class="p">.</span><span class="nx">del</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">controller_slug</span><span class="o">+</span><span class="s1">&#39;/:id(.:format)&#39;</span><span class="p">,</span> <span class="s1">&#39;DELETE&#39;</span><span class="p">).</span><span class="nx">to</span><span class="p">(</span><span class="nx">controller</span><span class="o">+</span><span class="s1">&#39;.destroy&#39;</span><span class="p">)</span>
+ <span class="p">)</span>
+
+ <span class="k">return</span> <span class="k">this</span>
+<span class="p">}</span>
+
+<span class="nx">Resource</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">collection</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">cb</span><span class="p">){</span>
+ <span class="k">if</span> <span class="p">(</span> <span class="k">typeof</span> <span class="nx">cb</span> <span class="o">!=</span> <span class="s1">&#39;function&#39;</span> <span class="p">)</span> <span class="k">throw</span> <span class="s1">&#39;resource.collection() requires a callback function&#39;</span>
+ <span class="nx">cb</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span> <span class="k">this</span><span class="p">.</span><span class="nx">collection_route</span> <span class="p">)</span>
+ <span class="k">return</span> <span class="k">this</span> <span class="c1">// for chaining</span>
+<span class="p">}</span>
+
+<span class="nx">Resource</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">member</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">cb</span><span class="p">){</span>
+ <span class="k">if</span> <span class="p">(</span> <span class="k">typeof</span> <span class="nx">cb</span> <span class="o">!=</span> <span class="s1">&#39;function&#39;</span> <span class="p">)</span> <span class="k">throw</span> <span class="s1">&#39;resource.member() requires a callback function&#39;</span>
+ <span class="nx">cb</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span> <span class="k">this</span><span class="p">.</span><span class="nx">member_route</span> <span class="p">)</span>
+ <span class="k">return</span> <span class="k">this</span> <span class="c1">// for chaining</span>
+<span class="p">}</span>
+
+</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
358 docs/route.html
195 additions, 163 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
165 docs/router.html
@@ -1,5 +1,6 @@
-<!DOCTYPE html> <html> <head> <title>router.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="helpers.html"> helpers.js </a> <a class="source" href="key.html"> key.js </a> <a class="source" href="route.html"> route.js </a> <a class="source" href="router.html"> router.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> router.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">Route</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./route&#39;</span><span class="p">).</span><span class="nx">Route</span><span class="p">,</span>
- <span class="nx">snakeize</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./helpers&#39;</span><span class="p">).</span><span class="nx">snakeize</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <h1>new Router()</h1>
+<!DOCTYPE html> <html> <head> <title>router.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="helpers.html"> helpers.js </a> <a class="source" href="key.html"> key.js </a> <a class="source" href="resource.html"> resource.js </a> <a class="source" href="route.html"> route.js </a> <a class="source" href="router.html"> router.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> router.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">Route</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./route&#39;</span><span class="p">).</span><span class="nx">Route</span>
+ <span class="p">,</span> <span class="nx">Resource</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./resource&#39;</span><span class="p">).</span><span class="nx">Resource</span>
+ <span class="p">,</span> <span class="nx">qstring</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;querystring&#39;</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>, snakeize = require('./helpers').snakeize</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h1>new Router</h1>
<p>Simple router for Node -- setting up routes looks like this:</p>
@@ -13,12 +14,13 @@
.to('users.read_message');
</code></pre>
-<p>Pretty familiar to anyone who's used Merb/Rails</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">Router</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
+<p>Pretty familiar to anyone who's used Merb/Rails</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">Router</span> <span class="o">=</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">Router</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
- <span class="kd">var</span> <span class="nx">METHODS</span> <span class="o">=</span> <span class="sr">/^(GET|POST|PUT|DELETE)$/i</span><span class="p">,</span>
- <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">METHODS</span> <span class="o">=</span> <span class="p">[</span> <span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;POST&#39;</span><span class="p">,</span> <span class="s1">&#39;PUT&#39;</span><span class="p">,</span> <span class="s1">&#39;DELETE&#39;</span> <span class="p">]</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">routes</span> <span class="o">=</span> <span class="p">[]</span>
- <span class="k">this</span><span class="p">.</span><span class="nx">routes</span> <span class="o">=</span> <span class="p">[]</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h2>router.match( path [, method] )</h2>
+ <span class="k">return</span> <span class="k">this</span>
+<span class="p">};</span> <span class="c1">// Router</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <h2>router.match( path [, method] )</h2>
<pre><code>router.match('/:controller/:action(/:id)(.:format)', 'GET')
.to(......)
@@ -27,63 +29,52 @@
<p>path is mandatory (duh)
method is optional, routes without a method will apply in all cases</p>
-<p>returns the route (for chaining)</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">match</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">path</span><span class="p">,</span> <span class="nx">method</span> <span class="p">)</span> <span class="p">{</span>
+<p>returns the route (for chaining)</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Router</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">match</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">path</span><span class="p">,</span> <span class="nx">method</span> <span class="p">)</span> <span class="p">{</span>
- <span class="k">if</span> <span class="p">(</span> <span class="k">typeof</span> <span class="nx">path</span> <span class="o">!=</span> <span class="s1">&#39;string&#39;</span> <span class="p">)</span> <span class="k">throw</span> <span class="s1">&#39;path must be a string&#39;</span>
+ <span class="k">if</span> <span class="p">(</span> <span class="k">typeof</span> <span class="nx">method</span> <span class="o">==</span> <span class="s1">&#39;string&#39;</span> <span class="p">)</span> <span class="nx">method</span> <span class="o">=</span> <span class="nx">method</span><span class="p">.</span><span class="nx">toUpperCase</span><span class="p">()</span>
- <span class="k">if</span> <span class="p">(</span> <span class="k">typeof</span> <span class="nx">method</span> <span class="o">!=</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="nx">METHODS</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">method</span><span class="p">))</span> <span class="k">throw</span> <span class="s1">&#39;method must be one of: GET, POST, PUT, DELETE&#39;</span>
+ <span class="k">if</span> <span class="p">(</span> <span class="k">typeof</span> <span class="nx">method</span> <span class="o">!=</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="k">this</span><span class="p">.</span><span class="nx">methods_regexp</span><span class="p">().</span><span class="nx">test</span><span class="p">(</span><span class="nx">method</span><span class="p">))</span> <span class="k">throw</span> <span class="s1">&#39;method must be one of: &#39;</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">METHODS</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="p">)</span>
- <span class="kd">var</span> <span class="nx">route</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Route</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="nx">method</span><span class="p">)</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">route</span><span class="p">)</span>
-
- <span class="k">return</span> <span class="nx">route</span>
- <span class="p">};</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <h2>convenience methods</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <h3>router.get( path )</h3>
+ <span class="kd">var</span> <span class="nx">route</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Route</span><span class="p">(</span> <span class="k">this</span><span class="p">,</span> <span class="nx">path</span><span class="p">,</span> <span class="nx">method</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>this.routes.push(route)</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">route</span>
+<span class="p">};</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <h2>convenience methods</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <h3>router.get( path )</h3>
<p>equivalent to</p>
<pre><code>router.match( path, 'GET' )
-</code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">get</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">path</span> <span class="p">){</span>
- <span class="k">return</span> <span class="nx">self</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="s1">&#39;GET&#39;</span><span class="p">)</span>
- <span class="p">}</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <h3>router.put( path )</h3>
+</code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Router</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">get</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">path</span> <span class="p">){</span>
+ <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="s1">&#39;GET&#39;</span><span class="p">)</span>
+ <span class="p">}</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <h3>router.put( path )</h3>
<p>equivalent to</p>
<pre><code>router.match( path, 'PUT' )
-</code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">put</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">path</span> <span class="p">){</span>
- <span class="k">return</span> <span class="nx">self</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="s1">&#39;PUT&#39;</span><span class="p">)</span>
- <span class="p">}</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <h3>router.post( path )</h3>
+</code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Router</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">put</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">path</span> <span class="p">){</span>
+ <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="s1">&#39;PUT&#39;</span><span class="p">)</span>
+ <span class="p">}</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <h3>router.post( path )</h3>
<p>equivalent to</p>
<pre><code>router.match( path, 'POST' )
-</code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">post</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">path</span> <span class="p">){</span>
- <span class="k">return</span> <span class="nx">self</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="s1">&#39;POST&#39;</span><span class="p">)</span>
- <span class="p">}</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <h3>router.del( path )</h3>
+</code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Router</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">post</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">path</span> <span class="p">){</span>
+ <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="s1">&#39;POST&#39;</span><span class="p">)</span>
+ <span class="p">}</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <h3>router.del( path )</h3>
<p>equivalent to</p>
<pre><code>router.match( path, 'DELETE' )
-</code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">del</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">path</span> <span class="p">){</span>
- <span class="k">return</span> <span class="nx">self</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="s1">&#39;DELETE&#39;</span><span class="p">)</span>
- <span class="p">}</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <h2>router.resource( controller )</h2>
+</code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Router</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">del</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">path</span> <span class="p">){</span>
+ <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="s1">&#39;DELETE&#39;</span><span class="p">)</span>
+ <span class="p">}</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <h2>router.resource( controller )</h2>
<p>generates standard resource routes for a controller name</p>
<pre><code>router.resource('products')
</code></pre>
-<p>returns an array of routes (for now, this may change)</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">resource</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">controller</span> <span class="p">)</span> <span class="p">{</span>
- <span class="kd">var</span> <span class="nx">controller_slug</span> <span class="o">=</span> <span class="nx">snakeize</span><span class="p">(</span><span class="nx">controller</span><span class="p">)</span>
- <span class="k">return</span> <span class="p">[</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">controller_slug</span><span class="o">+</span><span class="s1">&#39;(.:format)&#39;</span><span class="p">,</span> <span class="s1">&#39;GET&#39;</span><span class="p">).</span><span class="nx">to</span><span class="p">(</span><span class="nx">controller</span><span class="o">+</span><span class="s1">&#39;.index&#39;</span><span class="p">),</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">controller_slug</span><span class="o">+</span><span class="s1">&#39;/add(.:format)&#39;</span><span class="p">,</span> <span class="s1">&#39;GET&#39;</span><span class="p">).</span><span class="nx">to</span><span class="p">(</span><span class="nx">controller</span><span class="o">+</span><span class="s1">&#39;.add&#39;</span><span class="p">),</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">controller_slug</span><span class="o">+</span><span class="s1">&#39;/:id(.:format)&#39;</span><span class="p">,</span> <span class="s1">&#39;GET&#39;</span><span class="p">).</span><span class="nx">to</span><span class="p">(</span><span class="nx">controller</span><span class="o">+</span><span class="s1">&#39;.show&#39;</span><span class="p">),</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">controller_slug</span><span class="o">+</span><span class="s1">&#39;/:id/edit(.:format)&#39;</span><span class="p">,</span> <span class="s1">&#39;GET&#39;</span><span class="p">).</span><span class="nx">to</span><span class="p">(</span><span class="nx">controller</span><span class="o">+</span><span class="s1">&#39;.edit&#39;</span><span class="p">),</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">post</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">controller_slug</span><span class="o">+</span><span class="s1">&#39;(.:format)&#39;</span><span class="p">,</span> <span class="s1">&#39;POST&#39;</span><span class="p">).</span><span class="nx">to</span><span class="p">(</span><span class="nx">controller</span><span class="o">+</span><span class="s1">&#39;.create&#39;</span><span class="p">),</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">put</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">controller_slug</span><span class="o">+</span><span class="s1">&#39;/:id(.:format)&#39;</span><span class="p">,</span> <span class="s1">&#39;PUT&#39;</span><span class="p">).</span><span class="nx">to</span><span class="p">(</span><span class="nx">controller</span><span class="o">+</span><span class="s1">&#39;.update&#39;</span><span class="p">),</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">del</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="o">+</span><span class="nx">controller_slug</span><span class="o">+</span><span class="s1">&#39;/:id(.:format)&#39;</span><span class="p">,</span> <span class="s1">&#39;DELETE&#39;</span><span class="p">).</span><span class="nx">to</span><span class="p">(</span><span class="nx">controller</span><span class="o">+</span><span class="s1">&#39;.destroy&#39;</span><span class="p">)</span>
- <span class="p">];</span>
- <span class="p">};</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <h2>router.first( path, method, callback )</h2>
+<p>returns a Resource object</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Router</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">resource</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">controller</span> <span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">resource</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Resource</span><span class="p">(</span> <span class="k">this</span><span class="p">,</span> <span class="nx">controller</span> <span class="p">)</span>
+ <span class="k">return</span> <span class="nx">resource</span>
+<span class="p">};</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <h2>router.first( path, method, callback )</h2>
<p>find the first route that match the path &amp; method</p>
@@ -96,17 +87,17 @@
<p>If the options callback function is provided, it will be fired like so:</p>
<pre><code>callback( error, params )
-</code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">first</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">path</span><span class="p">,</span> <span class="nx">method</span><span class="p">,</span> <span class="nx">cb</span> <span class="p">)</span> <span class="p">{</span>
- <span class="kd">var</span> <span class="nx">params</span> <span class="o">=</span> <span class="kc">false</span>
-
- <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>attempt the parse</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">params</span> <span class="o">=</span> <span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">parse</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="nx">method</span><span class="p">)</span>
- <span class="k">if</span> <span class="p">(</span><span class="nx">params</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>fire the callback if given</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">cb</span> <span class="o">==</span> <span class="s1">&#39;function&#39;</span><span class="p">)</span> <span class="nx">cb</span><span class="p">(</span><span class="kc">false</span><span class="p">,</span> <span class="nx">params</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>may as well return this</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">params</span>
- <span class="p">}</span>
+</code></pre> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Router</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">first</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">path</span><span class="p">,</span> <span class="nx">method</span><span class="p">,</span> <span class="nx">cb</span> <span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">params</span> <span class="o">=</span> <span class="kc">false</span>
+ <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="k">this</span><span class="p">.</span><span class="nx">routes</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>attempt the parse</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">params</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">parse</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="nx">method</span><span class="p">)</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">params</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">&#182;</a> </div> <p>fire the callback if given</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">cb</span> <span class="o">==</span> <span class="s1">&#39;function&#39;</span><span class="p">)</span> <span class="nx">cb</span><span class="p">(</span><span class="kc">false</span><span class="p">,</span> <span class="nx">params</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <p>may as well return this</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">params</span>
<span class="p">}</span>
- <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">cb</span> <span class="o">==</span> <span class="s1">&#39;function&#39;</span><span class="p">)</span> <span class="nx">cb</span><span class="p">(</span><span class="s1">&#39;No matching routes found&#39;</span><span class="p">)</span>
- <span class="k">return</span> <span class="kc">false</span>
- <span class="p">};</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">&#182;</a> </div> <h2>router.all( path [, method] )</h2>
+ <span class="p">}</span>
+
+ <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">cb</span> <span class="o">==</span> <span class="s1">&#39;function&#39;</span><span class="p">)</span> <span class="nx">cb</span><span class="p">(</span><span class="s1">&#39;No matching routes found&#39;</span><span class="p">)</span>
+ <span class="k">return</span> <span class="kc">false</span>
+<span class="p">};</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <h2>router.all( path [, method] )</h2>
<p>find &amp; return a params hash from ALL routes that match</p>
@@ -119,16 +110,16 @@
]
</code></pre>
-<p>if there ares no matches, returns an empty array</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">all</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">path</span><span class="p">,</span> <span class="nx">method</span> <span class="p">)</span> <span class="p">{</span>
- <span class="kd">var</span> <span class="nx">ret</span> <span class="o">=</span> <span class="p">[],</span>
- <span class="nx">params</span> <span class="o">=</span> <span class="kc">false</span>
+<p>if there ares no matches, returns an empty array</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Router</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">all</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">path</span><span class="p">,</span> <span class="nx">method</span> <span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">ret</span> <span class="o">=</span> <span class="p">[],</span>
+ <span class="nx">params</span> <span class="o">=</span> <span class="kc">false</span>
- <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">params</span> <span class="o">=</span> <span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">parse</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="nx">method</span><span class="p">)</span> <span class="c1">// TODO: use call or apply (I forget which) to make &#39;method&#39; optional</span>
- <span class="k">if</span> <span class="p">(</span><span class="nx">params</span><span class="p">)</span> <span class="nx">ret</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">params</span><span class="p">)</span>
- <span class="p">}</span>
- <span class="k">return</span> <span class="nx">ret</span><span class="p">;</span>
- <span class="p">};</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <h2>router.url( params[, add_querystring=false] )</h2>
+ <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="k">this</span><span class="p">.</span><span class="nx">routes</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">params</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">parse</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span> <span class="nx">arguments</span><span class="p">)</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">params</span><span class="p">)</span> <span class="nx">ret</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">params</span><span class="p">)</span>
+ <span class="p">}</span>
+ <span class="k">return</span> <span class="nx">ret</span><span class="p">;</span>
+<span class="p">};</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">&#182;</a> </div> <h2>router.url( params[, add_querystring=false] )</h2>
<p>generates a URL from a params hash</p>
@@ -157,29 +148,33 @@
=&gt; '/products/5.json?love=cheese'
</code></pre>
-<p>returns false if there are no suitable routes</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">url</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">params</span><span class="p">,</span> <span class="nx">add_querystring</span> <span class="p">)</span> <span class="p">{</span>
- <span class="kd">var</span> <span class="nx">url</span> <span class="o">=</span> <span class="kc">false</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>iterate through the existing routes until a suitable match is found</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p>do the controller &amp; acton match?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span><span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">params</span><span class="p">.</span><span class="nx">controller</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">&amp;&amp;</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">params</span><span class="p">.</span><span class="nx">controller</span> <span class="o">!=</span> <span class="nx">params</span><span class="p">.</span><span class="nx">controller</span><span class="p">)</span> <span class="p">{</span>
- <span class="k">continue</span>
- <span class="p">}</span>
- <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span><span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">params</span><span class="p">.</span><span class="nx">action</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">&amp;&amp;</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">params</span><span class="p">.</span><span class="nx">action</span> <span class="o">!=</span> <span class="nx">params</span><span class="p">.</span><span class="nx">action</span><span class="p">)</span> <span class="p">{</span>
- <span class="k">continue</span>
- <span class="p">}</span>
- <span class="nx">url</span> <span class="o">=</span> <span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">params</span><span class="p">);</span>
- <span class="k">if</span> <span class="p">(</span><span class="nx">url</span><span class="p">)</span> <span class="p">{</span>
- <span class="k">break</span><span class="p">;</span>
- <span class="p">}</span>
+<p>returns false if there are no suitable routes</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Router</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">url</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">params</span><span class="p">,</span> <span class="nx">add_querystring</span> <span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">url</span> <span class="o">=</span> <span class="kc">false</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">&#182;</a> </div> <p>iterate through the existing routes until a suitable match is found</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="k">this</span><span class="p">.</span><span class="nx">routes</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">&#182;</a> </div> <p>do the controller &amp; acton match?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">params</span><span class="p">.</span><span class="nx">controller</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">&amp;&amp;</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">params</span><span class="p">.</span><span class="nx">controller</span> <span class="o">!=</span> <span class="nx">params</span><span class="p">.</span><span class="nx">controller</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">continue</span>
+ <span class="p">}</span>
+ <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">params</span><span class="p">.</span><span class="nx">action</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">&amp;&amp;</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">params</span><span class="p">.</span><span class="nx">action</span> <span class="o">!=</span> <span class="nx">params</span><span class="p">.</span><span class="nx">action</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">continue</span>
<span class="p">}</span>
+ <span class="nx">url</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">routes</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">params</span><span class="p">);</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">url</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">break</span><span class="p">;</span>
+ <span class="p">}</span>
+ <span class="p">}</span>
+
+ <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">url</span><span class="p">)</span> <span class="k">return</span> <span class="kc">false</span> <span class="c1">// no love? return false</span>
- <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">url</span><span class="p">)</span> <span class="k">return</span> <span class="kc">false</span> <span class="c1">// no love? return false</span>
+ <span class="kd">var</span> <span class="nx">qs</span> <span class="o">=</span> <span class="nx">qstring</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">url</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="c1">// build the possibly empty query string</span>
- <span class="kd">var</span> <span class="nx">qs</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;querystring&#39;</span><span class="p">).</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">url</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="c1">// build the possibly empty query string</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">add_querystring</span> <span class="o">&amp;&amp;</span> <span class="nx">qs</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="nx">url</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39;?&#39;</span> <span class="o">+</span> <span class="nx">qs</span> <span class="c1">// if there is a query string...</span>
- <span class="k">if</span> <span class="p">(</span><span class="nx">add_querystring</span> <span class="o">&amp;&amp;</span> <span class="nx">qs</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="k">return</span> <span class="nx">url</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39;?&#39;</span> <span class="o">+</span> <span class="nx">qs</span> <span class="c1">// if there is a query string...</span>
+ <span class="k">return</span> <span class="nx">url</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="c1">// just return the url</span>
+<span class="p">};</span>
- <span class="k">return</span> <span class="nx">url</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="c1">// just return the url</span>
- <span class="p">};</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">&#182;</a> </div> <h2>router.defer( testfn() )</h2>
+<span class="nx">Router</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">methods_regexp</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span>
+ <span class="k">return</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s1">&#39;^(&#39;</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">METHODS</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;|&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;)$&#39;</span><span class="p">,</span><span class="s1">&#39;i&#39;</span><span class="p">)</span>
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">&#182;</a> </div> <h2>router.defer( testfn() )</h2>
<pre><code>router.defer( test( path, method ) )
</code></pre>
@@ -190,19 +185,15 @@
<p>returns: DeferredRoute (for... reference? I dunno.)</p>
-<p><strong>THIS IS CURRENTLY COMPLETELY UNTESTED. IT MIGHT NOT EVEN WORK. SERIOUSLY.</strong></p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">defer</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">fn</span> <span class="p">)</span> <span class="p">{</span>
- <span class="k">if</span> <span class="p">(</span> <span class="k">typeof</span><span class="p">(</span><span class="nx">fn</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">&#39;function&#39;</span> <span class="p">)</span> <span class="k">throw</span> <span class="s1">&#39;Router.defer requires a function as the only argument&#39;</span>
-
- <span class="kd">var</span> <span class="nx">route</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Route</span><span class="p">(</span><span class="s1">&#39;deferred&#39;</span><span class="p">)</span>
- <span class="nx">route</span><span class="p">.</span><span class="nx">parse</span> <span class="o">=</span> <span class="nx">fn</span> <span class="c1">// add the custom parser</span>
- <span class="k">delete</span> <span class="nx">route</span><span class="p">.</span><span class="nx">test</span> <span class="c1">// = function(){return false};</span>
- <span class="k">delete</span> <span class="nx">route</span><span class="p">.</span><span class="nx">stringify</span> <span class="c1">//= function(){ throw &#39;Deferred routes are NOT generatable&#39;};</span>
- <span class="nx">self</span><span class="p">.</span><span class="nx">routes</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">route</span><span class="p">)</span>
- <span class="k">return</span> <span class="nx">route</span>
- <span class="p">}</span>
-
-<span class="p">};</span> <span class="c1">// Router</span>
+<p><strong>THIS IS CURRENTLY COMPLETELY UNTESTED. IT MIGHT NOT EVEN WORK. SERIOUSLY.</strong></p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Router</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">defer</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span> <span class="nx">fn</span> <span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span> <span class="k">typeof</span><span class="p">(</span><span class="nx">fn</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">&#39;function&#39;</span> <span class="p">)</span> <span class="k">throw</span> <span class="s1">&#39;Router.defer requires a function as the only argument&#39;</span>
-<span class="nx">exports</span><span class="p">.</span><span class="nx">Router</span> <span class="o">=</span> <span class="nx">Router</span>
+ <span class="kd">var</span> <span class="nx">route</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Route</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="s1">&#39;deferred&#39;</span><span class="p">)</span>
+ <span class="nx">route</span><span class="p">.</span><span class="nx">parse</span> <span class="o">=</span> <span class="nx">fn</span> <span class="c1">// add the custom parser</span>
+ <span class="k">delete</span> <span class="nx">route</span><span class="p">.</span><span class="nx">test</span> <span class="c1">// = function(){return false};</span>
+ <span class="k">delete</span> <span class="nx">route</span><span class="p">.</span><span class="nx">stringify</span> <span class="c1">//= function(){ throw &#39;Deferred routes are NOT generatable&#39;};</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">routes</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">route</span><span class="p">)</span>
+ <span class="k">return</span> <span class="nx">route</span>
+<span class="p">}</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
153 lib/key.js
@@ -5,86 +5,91 @@
// key = new Key('name')
// key = new Key('name', true)
//
-var Key = function( name, optional, glob ) {
+var Key = exports.Key = function( name, optional, glob ) {
- var self = this
-
- self.name = name
- self.optional = (optional===true) ? true : false
- self.glob = (glob===true) ? true : false
- self.regex = self.glob ? /[\w\-\/]+?/ : /[\w\-]+/ // default url-friendly regex
+ this.name = name
+ this.optional = (optional===true) ? true : false
+ this.glob = (glob===true) ? true : false
+ this.regex = this.glob ? /[\w\-\/]+?/ : /[\w\-]+/ // default url-friendly regex
// special defaults for controllers & actions, which will always be function-name-safe
- if (self.name == 'controller' || self.name == 'action') {
- self.regex = /[a-zA-Z_][\w\-]*/
+ if (this.name == 'controller' || this.name == 'action') {
+ this.regex = /[a-zA-Z_][\w\-]*/
}
- // key.regexString()
- // -----------------
- // makes a regex string of the key - used by key.test()
- //
- // returns a string of this keys regex
- this.regexString = function() {
- var ret = String(self.regex).replace(/^\//, '').replace(/\/[gis]?$/, '')
- if (self.optional) {
- return '(' + ret + ')?'
- }
- return '(' + ret + ')'
- };
-
- // key.test( string )
- // -----------------
- // validates a string using the key's regex pattern
- //
- // returns true/false if the string matches
- this.test = function( string ) {
- return new RegExp('^'+self.regexString()+'$').test(string)
- };
-
- // key.url( string )
- // -----------------
- // returns a string for buulding the url
- // if it matches the key conditions
- this.url = function( string ) {
- if (self.test(string)) {
- /*
- -- no longer needed
- snake_caseify the controller, if there is one
- if (self.name == 'controller') return snakeize(string)
- */
- return string
- }
- return false // doesn't match, let it go
- };
-
- // key.where( conditions )
- // -----------------
- // adds conditions that the key must match
- //
- // returns the key... because it can?
- this.where = function( conditions ) {
-
- var condition = conditions[self.name]
-
- if (condition instanceof RegExp) self.regex = condition // e.g. /\d+/
-
- if (condition instanceof String) self.regex = new RegExp(condition.replace(/^\//, '').replace(/\/[gis]?$/, '')) // e.g. "\d+"
-
- // an array of allowed values, e.g. ['stop','play','pause']
- if (condition instanceof Array) {
- condition = condition.map(function(cond){
- if (cond instanceof RegExp) return cond.toString()
- return cond
- }).map(function(cond){
- return cond.replace(/^\//, '').replace(/\/[gis]?$/, '')
- })
- self.regex = new RegExp(condition.join('|'))
- }
-
- return self
+ return this // just in case we forgot the new operator
+};
+
+
+// key.regexString()
+// -----------------
+// makes a regex string of the key - used by key.test()
+//
+// returns a string of this keys regex
+//
+Key.prototype.regexString = function() {
+ var ret = String(this.regex).replace(/^\//, '').replace(/\/[gis]?$/, '')
+ if (this.optional) {
+ return '(' + ret + ')?'
}
+ return '(' + ret + ')'
+};
+
+
+// key.test( string )
+// -----------------
+// validates a string using the key's regex pattern
+//
+// returns true/false if the string matches
+//
+Key.prototype.test = function( string ) {
+ return new RegExp('^'+this.regexString()+'$').test(string)
+};
+
- return self // just in case we forgot the new operator
+// key.url( string )
+// -----------------
+// returns a string for buulding the url
+// if it matches the key conditions
+//
+Key.prototype.url = function( string ) {
+ return this.test(string) ? string : false
};
-exports.Key = Key
+
+// key.where( conditions )
+// -----------------------
+// adds conditions that the key must match
+//
+// returns the key... because it can?
+//
+Key.prototype.where = function( conditions ) {
+
+ var condition = conditions[this.name]
+
+ if (condition instanceof RegExp) this.regex = condition // e.g. /\d+/
+
+ if (condition instanceof String) this.regex = new RegExp(condition.replace(/^\//, '').replace(/\/[gis]?$/, '')) // e.g. "\d+"
+
+ // an array of allowed values, e.g. ['stop','play','pause']
+ if (condition instanceof Array) {
+ condition = condition.map(function(cond){
+ if (cond instanceof RegExp) return cond.toString()
+ return cond
+ }).map(function(cond){
+ return cond.replace(/^\//, '').replace(/\/[gis]?$/, '')
+ })
+ this.regex = new RegExp(condition.join('|'))
+ }
+
+ return this // chainable
+}
+
+
+// key.toString()
+// --------------
+// returns a unique id that can be compared to other parts
+//
+Key.prototype.toString = function(){
+ return 'key-'+this.name
+}
View
36 lib/resource.js
@@ -0,0 +1,36 @@
+var snakeize = require('./helpers').snakeize
+ , mixin = require('./helpers').mixin
+
+var Resource = exports.Resource = function( base, controller ){
+
+ var controller_slug = this.name = snakeize(controller)
+
+ this.routes = []
+
+ // set up the actual routes for the resource
+ this.routes.push(
+ this.collection_route =
+ base.get('/'+controller_slug+'(.:format)', 'GET').to(controller+'.index')
+ , base.get('/'+controller_slug+'/add(.:format)', 'GET').to(controller+'.add')
+ , this.member_route =
+ base.get('/'+controller_slug+'/:id(.:format)', 'GET').to(controller+'.show')
+ , base.get('/'+controller_slug+'/:id/edit(.:format)', 'GET').to(controller+'.edit')
+ , base.post('/'+controller_slug+'(.:format)', 'POST').to(controller+'.create')
+ , base.put('/'+controller_slug+'/:id(.:format)', 'PUT').to(controller+'.update')
+ , base.del('/'+controller_slug+'/:id(.:format)', 'DELETE').to(controller+'.destroy')
+ )
+
+ return this
+}
+
+Resource.prototype.collection = function(cb){
+ if ( typeof cb != 'function' ) throw 'resource.collection() requires a callback function'
+ cb.call( this.collection_route )
+ return this // for chaining
+}
+
+Resource.prototype.member = function(cb){
+ if ( typeof cb != 'function' ) throw 'resource.member() requires a callback function'
+ cb.call( this.member_route )
+ return this // for chaining
+}
View
641 lib/route.js
@@ -1,327 +1,430 @@
-var Key = require('./key').Key,
- regExpEscape = require('./helpers').regExpEscape,
- mixin = require('./helpers').mixin,
- kindof = require('./helpers').kindof
-
-
-// new Route( path [, method] )
+var Key = require('./key').Key
+ , Resource = require('./resource').Resource
+ , regExpEscape = require('./helpers').regExpEscape
+ , mixin = require('./helpers').mixin
+ , kindof = require('./helpers').kindof
+ , snakeize = require('./helpers').snakeize
+
+// new Route( router, path [, method] )
// =================
// turns strings into magical ponies that come when you call them
//
-// route = new Route('/:controller/:action/:id(.:format)')
-// route = new Route('/:controller/:action(/:id)(.:format)', 'GET')
-// route = new Route('/:controller/:action(/:id)(.:format)',
-// route = new Route('/:controller/:action/:id(.:format)', 'GET')
+// route = new Route(router, '/:controller/:action/:id(.:format)')
+// route = new Route(router, '/:controller/:action(/:id)(.:format)', 'GET')
+// route = new Route(router, '/:controller/:action(/:id)(.:format)',
+// route = new Route(router, '/:controller/:action/:id(.:format)', 'GET')
//
// Pretty familiar to anyone who's used Merb/Rails - called by Router.match()
-var Route = function( path, method ) {
-
- var self = this,
- // !x! regexen crossing !x!
- // matches keys
- KEY = /:([a-zA-Z_][\w\-]*)/,
- // matches globs
- GLOB = /\*([a-zA-Z_][\w\-\/]*)/,
- // optional group (the part in parens)
- OGRP = /\(([^)]+)\)/,
- // breaks a string into atomic parts: ogrps, keys, then everything else
- PARTS = /\([^)]+\)|:[a-zA-Z_][\w\-]*|\*[a-zA-Z_][\w\-]*|[\w\-_\\\/\.]+/g
+//
+var Route = exports.Route = function( router, path, method ) {
+ if ( router && path ) this.match.apply( this, arguments )
+ return this
+}
+
+
+// !x! regexen crossing !x!
+// matches keys
+Route.prototype.KEY = /:([a-zA-Z_][\w\-]*)/
+// matches globs
+Route.prototype.GLOB = /\*([a-zA-Z_][\w\-\/]*)/
+// optional group (the part in parens)
+Route.prototype.OGRP = /\(([^)]+)\)/
+// breaks a string into atomic parts: ogrps, keys, then everything else
+Route.prototype.PARTS = /\([^)]+\)|:[a-zA-Z_][\w\-]*|\*[a-zA-Z_][\w\-]*|[\w\-_\\\/\.]+/g
+
+
+// route.match()
+// -------------------
+//
+// the actual route builder function, mostly called by `new Route`
+//
+Route.prototype.match = function( router, path, method, optional ){
+
+ if ( typeof path != 'string' ) throw 'path must be a string'
+
+ // is this a nested path?
+ if ( this.path != undefined ) {
+ var prefix = this.path
+
+ // get a list of key names in the new segment
+ var new_keys = []
+ Array.prototype.push.apply(new_keys, path.match(RegExp(this.KEY.source,'g')) ) // find all
+
+ // rename earlier keys
+ for (var i=0; i<new_keys.length; i++){
+ var replKey = new RegExp(':('+new_keys[i].substring(1)+'/?)')
+ prefix = prefix.replace(replKey,":"+snakeize(this.params['controller'])+"_$1")
+ }
+ // return the new awesomeness
+ return (new Route( router, prefix+path, method ))
+ }
// is this a nested, optional url segment like (.:format)
- self.optional = false
+ this.optional = optional == true
// uppercase the method name
- if (typeof(method) == 'string') self.method = method.toUpperCase()
+ if ( typeof(method) == 'string' ) this.method = method.toUpperCase()
// base properties
- self.params = {}
- self.parts = []
- self.route_name = null
- self.path = path
- /*self.regex = null // for caching of test() regex MAYBE*/
-
- // route.regexString()
- // -------------------
- //
- // returns a composite regex string of all route parts
- this.regexString = function() {
- var ret = ''
- // a route regex is a composite of its parts' regexe(s|n)
- for (var i in self.parts) {
- var part = self.parts[i]
- if (part instanceof Key) {
- ret += part.regexString()
- } else if (part instanceof Route) {
- ret += part.regexString()
- } else { // string
- ret += regExpEscape(part)
- }
+ this.router = router
+ this.params = {}
+ this.parts = []
+ this.route_name = null
+ this.path = path
+
+ // path parsing
+ var part;
+ while (part = this.PARTS.exec(path)) {
+ this.parts.push(part)
+ }
+
+ // have to do this in two passes due to RegExp execution limits
+ for (var i in this.parts) {
+ if (this.OGRP.test(this.parts[i])) { // optional group
+ this.parts[i] = new Route(this.router, this.OGRP.exec(this.parts[i])[1], true, true)
+ } else if(this.KEY.test(this.parts[i])) { // key
+ var keyname = this.KEY.exec(this.parts[i])[1]
+ this.parts[i] = new Key(keyname)
+ } else if(this.GLOB.test(this.parts[i])) { // glob
+ var keyname = this.GLOB.exec(this.parts[i])[1]
+ this.parts[i] = new Key(keyname, false, true)
+ } else { // string
+ this.parts[i] = String(this.parts[i])
}
- return '('+ret+')'+(self.optional ? '?' : '')
- };
+ }
+
+ if (!this.optional) this.router.routes.push(this)
+
+ return this
+
+}; // Route
+
+
+// convenience methods
+// -------------------
- // route.test( string )
- // -----------
- // builds & tests on a full regex of the entire path
+ // ### route.get( path )
+ // equivalent to
//
- // route.test( '/products/19/edit' )
- // => true
+ // route.match( path, 'GET' )
//
- // returns true/false depending on whether the url matches
- this.test = function( string ) {
- /*
- TODO cache this if it makes sense, code below:
- if(self.regex == null) self.regex = RegExp('^' + self.regexString() + '(\\\?.*)?$')
- return self.regex.test(string)
- */
- return RegExp('^' + self.regexString() + '(\\\?.*)?$').test(string)
- };
-
- // route.to( endpoint [, extra_params ] )
- // ------------------------------------------------------------------------------------
- // defines the endpoint & mixes in optional params
+ Route.prototype.get = function( path ){
+ return this.match(this.router, path, 'GET')
+ }
+
+ // ### route.put( path )
+ // equivalent to
//
- // route.to( 'controller.action' )
+ // route.match( path, 'PUT' )
+ //
+ Route.prototype.put = function( path ){
+ return this.match(this.router, path, 'PUT')
+ }
+
+ // ### route.post( path )
+ // equivalent to
//
- // route.to( 'controller.action', {lang:'en'} )
+ // route.match( path, 'POST' )
//
- // returns the route for chaining
- this.to = function( endpoint, extra_params ) {
+ Route.prototype.post = function( path ){
+ return this.match(this.router, path, 'POST')
+ }
- if ( !extra_params && typeof endpoint != 'string' ) {
- extra_params = endpoint
- endpoint = undefined
- }
+ // ### route.del( path )
+ // equivalent to
+ //
+ // route.match( path, 'DELETE' )
+ //
+ Route.prototype.del = function( path ){
+ return this.match(this.router, path, 'DELETE')
+ }
- /*
- TODO: make endpoint optional, since you can have the
- controller & action in the URL utself,
- even though that's a terrible idea...
- */
-
- if ( endpoint ){
- endpoint = endpoint.split('.')
- if( kindof(endpoint) == 'array' && endpoint.length != 2 ) throw 'syntax should be in the form: controller.action'
- this.params.controller = endpoint[0]
- this.params.action = endpoint[1]
- }
- extra_params = kindof(extra_params) == 'object' ? extra_params : {}
- mixin(self.params, extra_params)
+// route.resource( controller )
+// -----------------------------
+// generates standard resource routes for a controller name
+//
+// route.resource('products')
+//
+// returns a Resource object
+//
+Route.prototype.resource = function( controller ) {
+ var resource = new Resource( this, controller )
+ return resource
+};
- return this // chainable
- };
- // route.name( name )
- // ------------------
- // just sets the route name - NAMED ROUTES ARE NOT CURRENTLY USED
- //
- // route.name( 'login' )
- // route.name( 'homepage' ) // etc...
- //
- // returns: the route for chaining
- this.name = function( name ) {
- self.route_name = name
- return self // chainable
- };
-
- // route.where( conditions )
- // ---------------------
- // sets conditions that each url variable must match for the URL to be valid
- //
- // route.where( { id:/\d+/, username:/\w+/ } )
- //
- // returns: the route for chaining
- this.where = function( conditions ) {
+// route.regexString()
+// -------------------
+//
+// returns a composite regex string of all route parts
+//
+Route.prototype.regexString = function() {
+ var ret = ''
+ // a route regex is a composite of its parts' regexe(s|n)
+ for (var i in this.parts) {
+ var part = this.parts[i]
+ ret += part.regexString ? part.regexString() : regExpEscape(part)
+ }
+ return '('+ret+')'+(this.optional ? '?' : '')
+};
- var self = this
- if ( kindof(conditions) != 'object' ) throw 'conditions must be an object'
+// route.test( string )
+// -----------
+// builds & tests on a full regex of the entire path
+//
+// route.test( '/products/19/edit' )
+// => true
+//
+// returns true/false depending on whether the url matches
+//
+Route.prototype.test = function( string ) {
+ // with caching
+ if(!this.regex) this.regex = RegExp('^' + this.regexString() + '(\\\?.*)?$')
+ return this.regex.test(string)
- for (var i in self.parts) {
- if (self.parts[i] instanceof Key || self.parts[i] instanceof Route) {
- // recursively apply all conditions to sub-parts
- self.parts[i].where(conditions)
- }
+ // without caching
+ // return RegExp('^' + this.regexString() + '(\\\?.*)?$').test(string)
+};
+
+
+// route.to( endpoint [, extra_params ] )
+// ------------------------------------------------------------------------------------
+// defines the endpoint & mixes in optional params
+//
+// route.to( 'controller.action' )
+//
+// route.to( 'controller.action', {lang:'en'} )
+//
+// returns the route for chaining
+//
+Route.prototype.to = function( endpoint, extra_params ) {
+
+ if ( !extra_params && typeof endpoint != 'string' ) {
+ extra_params = endpoint
+ endpoint = undefined
+ }
+
+ /*
+ TODO: make endpoint optional, since you can have the
+ controller & action in the URL utself,
+ even though that's a terrible idea...
+ */
+
+ if ( endpoint ){
+ endpoint = endpoint.split('.')
+ if( kindof(endpoint) == 'array' && endpoint.length != 2 ) throw 'syntax should be in the form: controller.action'
+ this.params.controller = endpoint[0]
+ this.params.action = endpoint[1]
+ }
+
+ extra_params = kindof(extra_params) == 'object' ? extra_params : {}
+ mixin(this.params, extra_params)
+
+ return this // chainable
+};
+
+
+// route.name( name )
+// ------------------
+// just sets the route name - NAMED ROUTES ARE NOT CURRENTLY USED
+//
+// route.name( 'login' )
+// route.name( 'homepage' ) // etc...
+//
+// returns: the route for chaining
+//
+Route.prototype.name = function( name ) {
+ this.route_name = name
+ return this // chainable
+};
+
+
+// route.where( conditions )
+// ---------------------
+// sets conditions that each url variable must match for the URL to be valid
+//
+// route.where( { id:/\d+/, username:/\w+/ } )
+//
+// returns: the route for chaining
+//
+Route.prototype.where = function( conditions ) {
+
+ if ( kindof(conditions) != 'object' ) throw 'conditions must be an object'
+
+ for (var i in this.parts) {
+ if (this.parts[i] instanceof Key || this.parts[i] instanceof Route) {
+ // recursively apply all conditions to sub-parts
+ this.parts[i].where(conditions)
}
+ }
- return self // chainable
- };
+ return this // chainable
+};
- // route.stringify( params )
- // -------------------------
- // builds a string url for this Route from a params object
- //
- // returns: [ "url", [leftover params] ]
- //
- // **this is meant to be called & modified by router.url()**
- this.stringify = function( params ) {
- var url = [] // urls start life as an array to enble a second pass
-
- for (var i in self.parts) {
- var part = self.parts[i]
- if (part instanceof Key) {
- if (typeof(params[part.name]) != 'undefined' &&
- part.regex.test(params[part.name])) {
- // there's a param named this && the param matches the key's regex
- url.push(part.url(params[part.name])); // push it onto the stack