Permalink
Browse files

use notFound callback, simpler code

  • Loading branch information...
joshbuddy committed Jun 30, 2011
1 parent c8b6cdd commit 37d8b6bdac1786900e9520156f40ce3a1a9272bf
Showing with 162 additions and 161 deletions.
  1. +55 −60 lib/sherpa.coffee
  2. +96 −95 lib/sherpa.js
  3. +11 −5 lib/sherpa/nodejs.js
  4. +0 −1 spec/run.sh
View
@@ -19,15 +19,8 @@ root.Sherpa = class Sherpa
subparts.push part.slice(0, match[0].length)
part = part.slice(match[0].length, part.length)
subparts
- url: (name, params) ->
- @routes[name].url(params)
- add: (rawPath, opts) ->
- paths = [""]
- chars = rawPath.split('')
- startIndex = 0
- endIndex = 1
- matchesWith = opts?.matchesWith || {}
- routeName = opts?.name
+ generatePaths: (path) ->
+ [paths, chars, startIndex, endIndex] = [[''], path.split(''), 0, 1]
for charIndex in [0...chars.length]
c = chars[charIndex]
switch c
@@ -41,8 +34,57 @@ root.Sherpa = class Sherpa
startIndex -= endIndex - startIndex
else
paths[pathIndex] += c for pathIndex in [startIndex...endIndex]
-
- pathSet = for path in paths
+ paths
+ url: (name, params) ->
+ @routes[name].url(params)
+ addComplexPart: (subparts, compiledPath, matchesWith, variableNames) ->
+ [capturingIndicies, splittingIndicies, captures, spans] = [[], [], 0, false]
+ regexSubparts = for part in subparts
+ switch part[0]
+ when '\\' then escape(part[1].chr)
+ when ':', '*'
+ spans = true if part[0] == '*'
+ captures += 1
+ name = part.slice(1, part.length)
+ variableNames.push(name)
+ if part[0] == '*'
+ splittingIndicies.push(captures)
+ compiledPath.push "params['#{name}'].join('/')"
+ else
+ capturingIndicies.push(captures)
+ compiledPath.push "params['#{name}']"
+ if spans
+ if matchesWith[name]? then "((?:#{matchesWith[name].source}\\/?)+)" else '(.*?)'
+ else
+ "(#{(matchesWith[name]?.source || '[^/]*?')})"
+ else
+ compiledPath.push "'#{part}'"
+ escape(part)
+ regexp = new RegExp("#{regexSubparts.join('')}$")
+ if spans
+ new SpanningRegexMatcher(regexp, capturingIndicies, splittingIndicies)
+ else
+ new RegexMatcher(regexp, capturingIndicies, splittingIndicies)
+ addSimplePart: (subparts, compiledPath, matchesWith, variableNames) ->
+ part = subparts[0]
+ switch part[0]
+ when ':'
+ variableName = part.slice(1, part.length)
+ compiledPath.push "params['#{variableName}']"
+ variableNames.push(variableName)
+ if matchesWith[variableName]? then new SpanningRegexMatcher(matchesWith[variableName], [0], []) else new Variable()
+ when '*'
+ compiledPath.push "params['#{variableName}'].join('/')"
+ variableName = part.slice(1, part.length)
+ variableNames.push(variableName)
+ new Glob()
+ else
+ compiledPath.push "'#{part}'"
+ new Lookup(part)
+ add: (rawPath, opts) ->
+ matchesWith = opts?.matchesWith || {}
+ routeName = opts?.name
+ pathSet = for path in @generatePaths(rawPath)
node = @root
variableNames = []
parts = path.split('/')
@@ -54,55 +96,8 @@ root.Sherpa = class Sherpa
else
compiledPath.push "'/'"
subparts = @findSubparts(part)
- nextNode = if subparts.length == 1
- part = subparts[0]
- switch part[0]
- when ':'
- variableName = part.slice(1, part.length)
- compiledPath.push "params['#{variableName}']"
- variableNames.push(variableName)
- if matchesWith[variableName]? then new SpanningRegexMatcher(matchesWith[variableName], [0], []) else new Variable()
- when '*'
- compiledPath.push "params['#{variableName}'].join('/')"
- variableName = part.slice(1, part.length)
- variableNames.push(variableName)
- new Glob()
- else
- compiledPath.push "'#{part}'"
- new Lookup(part)
- else
- capturingIndicies = []
- splittingIndicies = []
- captures = 0
- spans = false
- regexSubparts = for part in subparts
- switch part[0]
- when '\\' then escape(part[1].chr)
- when ':', '*'
- if part[0] == '*'
- spans = true
- captures += 1
- name = part.slice(1, part.length)
- variableNames.push(name)
- if part[0] == '*'
- splittingIndicies.push(captures)
- compiledPath.push "params['#{name}'].join('/')"
- else
- capturingIndicies.push(captures)
- compiledPath.push "params['#{name}']"
- if spans
- if matchesWith[name]? then "((?:#{matchesWith[name].source}\\/?)+)" else '(.*?)'
- else
- "(#{(matchesWith[name]?.source || '[^/]*?')})"
- else
- compiledPath.push "'#{part}'"
- escape(part)
- regexp = new RegExp("#{regexSubparts.join('')}$")
- if spans
- new SpanningRegexMatcher(regexp, capturingIndicies, splittingIndicies)
- else
- new RegexMatcher(regexp, capturingIndicies, splittingIndicies)
- node = node.add(nextNode)
+ nextNodeFn = if subparts.length == 1 then @addSimplePart else @addComplexPart
+ node = node.add(nextNodeFn(subparts, compiledPath, matchesWith, variableNames))
if opts?.conditions?
node = node.add(new RequestMatcher(opts.conditions))
path = new Path(node, variableNames)
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -11,31 +11,37 @@ Sherpa.NodeJs = function (routes) {
Sherpa.NodeJs.prototype = {
listener: function() {
var router = new Sherpa.Router();
- var notFound = function(req, res) {
+ router.callback = function(req, res) {
res.writeHead(404, {});
res.end();
}
for(var key in this.routes) {
if (this.routes[key][0] == 'not found') {
- notFound = this.routes[key][1];
+ router.callback = function() { this.routes[key][1](req, res) };
} else {
switch(this.routes[key].length) {
case 2:
- router.add(this.routes[key][0]).to(this.routes[key][1]);
+ router.add(this.routes[key][0]).to(function(req, params) {
+ this.routes[key][1](req, res, params)
+ });
break;
case 3:
- router.add(this.routes[key][0]).withOptions(this.routes[key][1]).to(this.routes[key][2]);
+ router.add(this.routes[key][0], this.routes[key][1]).to(function(req, params) {
+ this.routes[key][2](req, res, params)
+ });
break;
default:
throw("must be 2 or 3");
}
}
}
+ router.callback = notFound
+
return function(httpRequest, httpResponse) {
var requestUrl = url.parse(httpRequest.url)
- var response = router.recognize(requestUrl.pathname, httpRequest);
+ var response = router.match(httpRequest);
if (response) {
httpRequest.sherpaResponse = response;
response.destination(httpRequest, httpResponse);
View
@@ -1,6 +1,5 @@
#!/bin/sh
-#coffee spec/spec_generate2.js
coffee -c spec/spec_generate.coffee && node spec/spec_generate.js
coffee -c spec/spec_recognize.coffee && node spec/spec_recognize.js

0 comments on commit 37d8b6b

Please sign in to comment.