Skip to content

@rowoot /lokki.js
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
lokki
// Lokki.js 0.2.0
// (c) 2011 Micheal
// Load Important Libs
var FS = require('fs')
, INFLECTION = require('./inflection')
, Express = require('express')
, App = Express.createServer();
// Constants
var RESOURCE_URLS = ["index","new","create","edit","update","destroy","show"],
APP_DIR = process.cwd();
// Lokki Var
var Lokki = {};
// Current Version
Lokki.VERSION = '0.2.0';
// Lokki Controllers
Lokki.Controllers = [];
// Lokki Routes
Lokki.Routes = {};
// Lokki.Route
// --------------
// Create New Route
Lokki.Route = function() {
this._controller = "";
this._action = "";
this._url = "";
Lokki.Routes.push(this);
}
// Root
Lokki.Route.prototype.add = function(type, route) {
if (route.indexOf("#") === -1) throw new Error("root url is not defined properly");
var self = this,
temp = route.split("#"),
controller = temp[0],
action = temp[1];
// Assign Controller
self._controller = controller;
// Assign Action
self._action = action;
// Assign Route
self._url = "/";
// create Express Routes
switch(type) {
case 'root':
break;
}
}
// Lokki.Controller
// --------------
// Create New Controller
Lokki.Controller = function(name, opts) {
this._controller = name;
this._filters = opts.filters; // Get Filter Function if present
this._actions = opts.actions || {}; // Get Actions
// Initialize Controller
this.initialize();
// Will be used internally
this._filters_for_actions = opts.actions.keys().toObject() || {};
}
// Initialize New Controller
Lokki.Controller.prototype.initialize = function() {
var self = this;
// Execute Filters
if (typeof this._filters != 'undefined')
this._filters();
}
Lokki.Controller.prototype.apply_before_filter = function(fn, opts) {
// Lokki
var self = this,
actions = self._actions.keys();
// Error Handling
// No function Exists
if (typeof fn === 'undefined') throw new Error("function is not defined");
// Cannot Define Both Except and Only Constraints for filter
if (opts && opts.except && opts.only) throw new Error("cannot define both except and only");
// Convert Constraint Data to Array, if string
// and then populate relevent filter functions
// to associated action
if(opts) {
if (opts.except) {
if (typeof opts.except === "string") opts.except = opts.except.split(",");
// Run across all actions except the above and push the function as before filter
actions.map(function(action) {
if (opts.except.indexOf(action) === -1) {
self._filters_for_actions[action].push(fn);
}
});
} else if (opts.only) {
if (typeof opts.only === "string") opts.only = opts.only.split(",");
// Run across only particular actions and push the function as before filter
opts.only.map(function(action) {
self._filters_for_actions[action].push(fn);
});
}
} else {
// Run across all actions and push the function as before filter
actions.map(function(action) {
self._filters_for_actions[action].push(fn);
});
}
}
/**
* Boots the Controller
*/
function bootControllers() {
// Default controller path
var controller_path = APP_DIR + "/controllers/";
// Boot Each Controller
try {
var files = FS.readdirSync(controller_path);
// If no controllers are found
if (files.length === 1) return false;
// Load Application Controller First
require(controller_path + "application");
// Load rest of the controller
files.forEach(function(file){
file = file.replace('.js', '');
console.log('loading controller =>', file);
if (file != "application") require(controller_path + file);
});
} catch (e) {
console.log("===> Boot Controller ==>", e.message);
}
}
/**
* Boots the Lib Files from Lib Folder
*/
function bootLibs() {
// Default Lib path
var lib_path = APP_DIR + "/libs/";
// Boot Each Lib
try {
var files = FS.readdirSync(lib_path);
// If no Lib are found
if (files.length === 1) return false;
// Load rest of the controller
files.forEach(function(file){
file = file.replace('.js', '');
console.log('loading Lib =>', file);
require(lib_path + file);
});
} catch (e) {
console.log("===> Boot LIBS ==>", e.message);
}
}
// Lokki Public Methods
Lokki.public = {
Controller: Lokki.Controller
, Init: function(port,cb) {
console.log("Running Lokki => ", Lokki.VERSION);
// Boot Libs
bootLibs();
// Boot Controllers
bootControllers();
// CallBack if any
if (typeof cb != "undefined") cb();
console.log("Server Listening at port ", port);
// Start Server and listen on PORT
App.listen(port);
}
}
// The top-level namespace. All public Lokki classes and modules will
// be attached to this. Exported for Nodejs.
// GLOBAL
GLOBAL["Lokki"] = {
Express: Express
, App: merge(App,Lokki.public)
, Middleware: {}
, Vars: {}
}
module.exports = GLOBAL["Lokki"];
// UTIL
//-----
// Merge Object with another Object
function merge(a, b){
var keys = Object.keys(b);
for (var i = 0, len = keys.length; i < len; ++i) {
var key = keys[i];
a[key] = b[key]
}
return a;
};
// Get Keys of Object
Object.prototype.keys = function() {
var o = this
, acc = [];
for (var propertyName in o) {
if(o.hasOwnProperty(propertyName)) acc.push(propertyName);
}
return acc;
}
// Converts Array to Object
Array.prototype.toObject = function() {
var o = this
acc = {};
for (var i = 0, len = o.length; i < len; i++) {
acc[o[i]] = [];
}
return acc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.