Skip to content

Commit

Permalink
*fixed persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
jspears committed Apr 11, 2012
1 parent 175c752 commit fd114c8
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 72 deletions.
41 changes: 25 additions & 16 deletions lib/display-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function findAll(field, args) {
function toSubSchema(key) {
var keys = key.split('.');
var str = keys[0];
for (var i = 1, l = keys.length; i<l; i++) {
for (var i = 1, l = keys.length; i < l; i++) {
str += '.subSchema.' + keys[i];
}
return str;
Expand All @@ -50,21 +50,36 @@ var global_options = {
}
function App(options) {
this.options = _u.extend({}, global_options, options);
var args = this._args = Array.prototype.slice.call(arguments, 1);
['title', 'version', 'description'].forEach(easyget(args), this);
this._modelPaths = {};
args.forEach(this.add, this);
Array.prototype.slice.call(arguments, 1).forEach(this.add, this);

this._args = [];
['title', 'version', 'description'].forEach(easyget(this._args), this);
this.__defineGetter__('models', function () {
return _u.map(this.modelPaths, function (k, v) {
return k
});
});

var ret;
//TODO hrm do I need to worry about thread safety?
this.__defineGetter__('modelPaths', function () {
var ret = {};
_u.each(this._modelPaths, function onModelPaths(v, k) {
if (ret &! this._updated) {
return ret;
}
ret = {};
var _modelPaths = {};

this._args.forEach(function (k, v) {
_u.each(k.modelPaths, function onAppArgs(vv, kk) {
var key = vv.modelName || kk;
(_modelPaths[key] || (_modelPaths[key] = [])).push(vv);
}, this);

}, this);

_u.each(_modelPaths, function onModelPaths(v, k) {
ret[k] = new Model(k, v);
}, this);
this._updated = false;
return ret;
});
this.__defineGetter__('editors', function () {
Expand All @@ -74,13 +89,7 @@ function App(options) {
App.prototype.add = function (App) {
if (!App) return this;
this._args.push(App);
this._args.forEach(function (k, v) {
_u.each(k.modelPaths, function onAppArgs(vv, kk) {
var key = vv.modelName || kk;
(this._modelPaths[key] || (this._modelPaths[key] = [])).push(vv);
}, this);

}, this);
this._updated = true;
return this;
}
App.prototype.modelFor = function (model) {
Expand Down Expand Up @@ -165,7 +174,7 @@ function Model(modelName, args) {
});
}
Model.prototype.pathFor = function (path) {
var ret = util.depth(this.paths,toSubSchema(path));
var ret = util.depth(this.paths, toSubSchema(path));
return ret;
}
function contains(arr, value) {
Expand Down
18 changes: 15 additions & 3 deletions lib/file-persistence.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var fs = require('fs'), path = require('path');
var fs = require('fs'), path = require('path'), crypto = require('crypto');


var FilePersistence = function (file) {
Expand All @@ -10,8 +10,9 @@ module.exports = FilePersistence;

FilePersistence.prototype.save = function (key, data, callback) {
var conf = this.read(this.filename);
var now = Date.now();
if (conf) {
fs.renameSync(this.filename, this.filename + '.' + Date.now());
fs.renameSync(this.filename, this.filename + '.' + now);
} else {
conf = {};
var dir = path.dirname(this.filename);
Expand All @@ -20,7 +21,18 @@ FilePersistence.prototype.save = function (key, data, callback) {
}
}
(conf.plugins || (conf.plugins = {}))[key] = data;
fs.writeFile(this.filename, JSON.stringify(conf), 'utf-8', callback);
var conf_str = JSON.stringify(conf);
var sha = crypto.createHash('sha1').update(conf_str).digest('base64');

fs.writeFile(this.filename, conf_str, 'utf-8', function(err, stuff){
if (err){
fs.renameSync(this.filename+'.'+now, this.filename);
}
callback(err, {_id:sha, timestamp:now});
}.bind(this));
}
FilePersistence.prototype.list = function(callback){
fs.readdir(path.dirname(this.filename), callback);
}

FilePersistence.prototype.read = function (filename) {
Expand Down
11 changes: 7 additions & 4 deletions lib/plugin-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,27 @@ function cleanurl(str) {
* @param path //path defaults basename(dirname(module.parent.filename))
* @param pluginManager // a reference to the pluginManager.
*/
var Plugin = function (options, app, name, path, pluginManager) {
var Plugin = function (options, app, name, p, pluginManager) {
this.pluginManager = pluginManager;
this.path = path;
this.path = p;
this.options = _u.extend({}, options);
var _baseUrl;
this.__defineSetter__('baseUrl', function (val) {
_baseUrl = val == '/' ? val : val && '/' + cleanurl(val) || '/';
});
this.baseUrl = this.options.baseUrl;
this.name = name || this.options.name || path.basename(path.dirname(module.parent.filename));

console.log('name', this.name);
this.__defineGetter__('baseUrl', function () {
var ret = _baseUrl;
return ret;
});

this.app = app;
}
Plugin.prototype.configure = function(conf){

}

Plugin.prototype.__defineGetter__('pluginUrl', function () {
var ret = this.options.pluginUrl || this.baseUrl + this.name;
Expand Down Expand Up @@ -144,4 +147,4 @@ Plugin.prototype.generate = function (res, view, options, next) {
var search = path.join(this.path, '/views/', view);
res.render(search, _u.extend({relative:false, hint:true}, options, {layout:false, defaultEngine:this.defaultEngine}));
}
module.exports = Plugin;
module.exports = Plugin;
31 changes: 26 additions & 5 deletions lib/plugin-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,29 @@ var path = require('path'), _u = require('underscore'), DisplayModel = require('
*/
var PluginManager = function (options, express) {
this.options = _u.extend({}, options);
this.persist = options.persist || new FilePersistence();
this.plugins = this.loadPlugins(this.options, express);
this.appModel = new DisplayModel(this.options);
this.configFile = this.options.configFile || path.join(process.cwd, 'conf', 'bobamo.json');
this.persist = options.persist || new FilePersistence(this.configFile);
this.loadAppModels();
this.loadFilters();
this.loadRoutes();
this.configure();
}
PluginManager.prototype.save = function(plugin, data, callback){
PluginManager.prototype.save = function (plugin, data, callback) {
this.persist.save(plugin.name, data, callback);
}
PluginManager.prototype.configure = function () {
var obj = this.persist.read(this.configFile)
if (obj && obj.plugins)
this.plugins.forEach(function (plugin) {
plugin.configure(obj.plugins[plugin.name]);
}, this)
}
/**
* Default plugins to load.
*/
PluginManager.prototype.defaultPlugins = ['less', 'modeleditor', 'appeditor', 'generator', 'rest', 'mongoose', 'static', 'package'];
PluginManager.prototype.defaultPlugins = [ 'appeditor', 'modeleditor', 'less', 'generator', 'rest', 'mongoose', 'static', 'package'];
/**
*
*/
Expand Down Expand Up @@ -58,6 +67,7 @@ PluginManager.prototype.loadFilters = function () {
*/
PluginManager.prototype.loadAppModels = function () {
this.plugins.forEach(function (plugin) {

this.appModel.add(plugin.appModel(this.options))
}, this);

Expand All @@ -80,7 +90,15 @@ PluginManager.prototype.loadRoutes = function () {
* }
* @param express
*/

function isPluginApi(clz) {
var _super = clz._super;
while (_super) {
if (_super == PluginApi)
return true;
_super = _super._super;
}
return false;
}
PluginManager.prototype.loadPlugins = function (options, express) {
var defDirs = [path.join(path.dirname(module.filename), '../plugins'), path.join(process.cwd(), '../plugins')];
var dirs = options.pluginDirs || options.pluginDir && [].concat(options.pluginDir).concat(defDirs) || defDirs;
Expand All @@ -94,7 +112,7 @@ PluginManager.prototype.loadPlugins = function (options, express) {
_u.unique(plugins).forEach(function (pdir) {
if (_u.isFunction(pdir)) {

var plugin = (pdir instanceof PluginApi) ? new pdir(options, express, pdir, path.join(dir, pdir), this) :pdir(options, express, pdir, path.join(dir, pdir), this) ;
var plugin = isPluginApi(pdir) ? new pdir(options, express, null, null, this) : pdir(options, express, null, null, this);

ret.push(plugin);
loaded[plugin.name || pdir] = true;
Expand Down Expand Up @@ -147,6 +165,9 @@ PluginManager.prototype.pluginFor = function (path, property, object) {
}
}
}
PluginManager.prototype.pluginNames = function(){
return _u(this.plugins).map(function(plugin){ return plugin.name});
}


module.exports = PluginManager;
Expand Down
41 changes: 31 additions & 10 deletions plugins/appeditor/appeditor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var Plugin = require('../../lib/plugin-api');
var Plugin = require('../../lib/plugin-api'), _u = require('underscore');
var AppEditorPlugin = function (options, app, name) {
Plugin.apply(this, arguments);
this._appModel = {};
}
require('util').inherits(AppEditorPlugin, Plugin);
AppEditorPlugin.prototype.admin = function(){
Expand All @@ -9,28 +10,48 @@ AppEditorPlugin.prototype.admin = function(){
title:'Application Details'
};
}
AppEditorPlugin.prototype.appModel = function(){
return this._appModel;
}
AppEditorPlugin.prototype.configure = function(options){
_u.extend(this._appModel,options);

}
AppEditorPlugin.prototype.filters = function(){
this.app.get(this.pluginUrl+'*', function(req,res,next){
res.local('pluginManager', this.pluginManager);
next();
}.bind(this));
Plugin.prototype.filters.apply(this, arguments);
}
AppEditorPlugin.prototype.routes = function (options) {

this.app.get(this.pluginUrl + '/admin/:id', function (req, res, next) {
var appModel = this.pluginManager.appModel;
var appModel = this._appModel;
res.send({
payload:appModel,
status:1
})
}.bind(this));

this.app.post(this.pluginUrl + '/admin', function (req, res, next) {
res.send({
status:0,
payload:{}
})
this.save(req.body, function(err, obj){
this.configure(req.body);
res.send({
status:0,
payload:obj
})
}.bind(this));
}.bind(this));

this.app.put(this.pluginUrl + '/admin', function (req, res, next) {
res.send({
status:0,
payload:{}
})
this.save(req.body, function(err, obj){
this.configure(req.body);
res.send({
status:0,
payload:obj
})
}.bind(this));
}.bind(this));

Plugin.prototype.routes.apply(this, arguments);
Expand Down
10 changes: 8 additions & 2 deletions plugins/appeditor/views/admin/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ define([
type:'MultiEditor',
help:'Which Models to allow users to view',
options:eval('({{html JSON.stringify(Object.keys(appModel.modelPaths))}})')
},
plugins:{
type:'List',
help:'The order in which to process plugins'
}
}
var Model = Backbone.Model.extend({
Expand All @@ -43,7 +47,8 @@ define([
return EditView.extend({
fieldsets:[
{legend:'Application', fields:['title', 'version', 'description']},
{'legend':'Models', fields:['models']}
{'legend':'Models', fields:['models']},
{'legend': 'Plugins', fields:['plugins']}
],
template:_.template(template),
model:Model,
Expand All @@ -58,7 +63,8 @@ define([
title:'${appModel.title}',
description:'${appModel.description}',
version:'${appModel.version}',
models:{{html JSON.stringify(Object.keys(appModel.modelPaths))}}
models:eval('({{html JSON.stringify(Object.keys(appModel.modelPaths))}})'),
plugins:eval('({{html JSON.stringify(pluginManager.pluginNames())}})')
});
}
});
Expand Down
4 changes: 2 additions & 2 deletions plugins/generator/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<link href="js/libs/backbone-forms/test/lib/jquery-ui/flick/jquery-ui-1.8.14.custom.css" rel="stylesheet">
<link href="js/libs/jquery-miniColors/jquery.miniColors.css" rel="stylesheet">
<!-- Le fav and touch icons -->
<!--<link rel="shortcut icon" href="${baseUrl}/images/favicon.ico">-->
<link rel="shortcut icon" href="${baseUrl}/favicon.ico">
<!--<link rel="apple-touch-icon" href="${baseUrl}/images/apple-touch-icon.png">-->
<!--<link rel="apple-touch-icon" sizes="72x72" href="${baseUrl}/images/apple-touch-icon-72x72.png">-->
<!--<link rel="apple-touch-icon" sizes="114x114" href="${baseUrl}/images/apple-touch-icon-114x114.png">-->
Expand Down Expand Up @@ -60,4 +60,4 @@
<script data-main="js/main" src="js/libs/require/require-jquery.js"></script>

</body>
</html>
</html>
Loading

0 comments on commit fd114c8

Please sign in to comment.