Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Init

  • Loading branch information...
commit 3f150e011f94b20fae269babad91ffe7f75da363 0 parents
Anatoliy Chakkaev authored
5 Makefile
@@ -0,0 +1,5 @@
+
+test:
+ @nodeunit test/*_test.*
+
+.PHONY: test
100 lib/railway_routes.js
@@ -0,0 +1,100 @@
+exports.Map = Map;
+
+function Map(app, bridge) {
+ this.app = app;
+ this.bridge = bridge;
+ this.paths = [];
+ this.ns = '';
+ // wtf???
+ this.glob_path = '/';
+}
+
+Map.prototype.urlHelperName = function (path, action) {
+ if (path instanceof RegExp) {
+ path = path.toString().replace(/[^a-z]+/ig, '/');
+ }
+
+ // remove trailing slashes and split to parts
+ path = path.replace(/^\/|\/$/g, '').split('/');
+
+ // handle root paths
+ if (path === '' || path === '/') return 'root';
+
+ var helperName = [];
+ path.forEach(function (token, index, all) {
+ // skip variables
+ if (token[0] == ':') return;
+
+ var nextToken = all[index + 1] || '';
+ // current token is last?
+ if (index == all.length - 1) {
+ token = token.replace(/\.:format\??$/, '');
+ // same as action? - prepend
+ if (token == action) {
+ helperName.unshift(token);
+ return;
+ }
+ }
+ if (nextToken[0] == ':' || nextToken == 'new.:format?') {
+ token = singularize(token);
+ }
+ helperName.push(token);
+ });
+ return helperName.join('_');
+};
+
+['get', 'post', 'put', 'delete', 'del', 'all'].forEach(function (method) {
+ Map.prototype[method] = function (subpath, handler, middleware, options) {
+
+ var controller, action;
+ if (typeof handler === 'string') {
+ controller = handler.split('#')[0];
+ action = handler.split('#')[1];
+ }
+
+ var path;
+ if (typeof subpath === 'string') {
+ path = this.glob_path + subpath.replace(/^\/|\/$/, '');
+ } else { // regex???
+ path = subpath;
+ }
+
+ // only accept functions in before filter when it's an array
+ if (middleware instanceof Array) {
+ var before_filter_functions = middleware.filter(function(filter) {
+ return (typeof filter === 'function');
+ });
+ middleware = before_filter_functions.length > 0 ? before_filter_functions : null;
+ }
+
+ if (!(typeof middleware === 'function' || (middleware instanceof Array)) && typeof options === 'undefined') {
+ options = middleware;
+ middleware = null;
+ }
+
+ if (!options) {
+ options = {};
+ }
+
+ path = options.collection ? path.replace(/\/:.*_id/, '') : path;
+
+ var args = [path];
+ if (middleware) {
+ args = args.concat(middleware);
+ }
+ args = args.concat(this.bridge(this.ns, controller, action));
+
+ // this.bridge.dump.push({
+ // helper: options.as || this.urlHelperName(path, action),
+ // method: method,
+ // path: path,
+ // file: this.ns + controller,
+ // name: controller,
+ // action: action
+ // });
+
+ // this.paths.push([path, action]);
+
+ this.app[method].apply(this.app, args);
+ };
+});
43 test/railway_routes_test.js
@@ -0,0 +1,43 @@
+require('./spec_helper').init(module.exports);
+var pathetic = require('../lib/railway_routes');
+
+function fakeApp(container) {
+ var app = {};
+ ['get', 'post', 'put', 'del', 'delete', 'all'].forEach(function (m) {
+ app[m] = function () {
+ var args = [].slice.call(arguments);
+ args.unshift(m.toUpperCase());
+ container.push(args);
+ };
+ });
+ return app;
+}
+
+function fakeBridge() {
+ return function (ns, controller, action) {
+ return ns + controller + '#' + action;
+ };
+}
+
+it('should produce routes for all methods', function (test) {
+ var paths = [];
+ var map = new pathetic.Map(fakeApp(paths), fakeBridge());
+ map.get('/signin', 'session#new');
+ map.post('/signin', 'session#create');
+ map.del('/signout', 'session#destroy');
+ map.get('/signup', 'users#new');
+ map.put('/signup', 'users#create');
+ map.all('/path', 'controller#action');
+
+ test.deepEqual(paths, [
+ [ 'GET', '/signin', 'session#new' ],
+ [ 'POST', '/signin', 'session#create' ],
+ [ 'DEL', '/signout', 'session#destroy' ],
+ [ 'GET', '/signup', 'users#new' ],
+ [ 'PUT', '/signup', 'users#create' ],
+ [ 'ALL', '/path', 'controller#action' ]
+ ]);
+ test.done();
+});
+
+
43 test/spec_helper.js
@@ -0,0 +1,43 @@
+var group_name = false, EXT_EXP;
+
+function it(should, test_case) {
+ check_external_exports();
+ if (group_name) {
+ EXT_EXP[group_name][should] = test_case;
+ } else {
+ EXT_EXP[should] = test_case;
+ }
+}
+
+global.it = it;
+
+function context(name, tests) {
+ check_external_exports();
+ EXT_EXP[name] = {};
+ group_name = name;
+ tests({
+ before: function (f) {
+ it('setUp', f);
+ },
+ after: function (f) {
+ it('tearDown', f);
+ }
+ });
+ group_name = false;
+}
+
+global.context = context;
+
+exports.init = function (external_exports) {
+ EXT_EXP = external_exports;
+ if (external_exports.done) {
+ external_exports.done();
+ }
+};
+
+function check_external_exports() {
+ if (!EXT_EXP) throw new Error(
+ 'Before run this, please ensure that ' +
+ 'require("spec_helper").init(exports); called');
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.