Skip to content

Instantly share code, notes, and snippets.

@dvv
Created June 8, 2010 14:56
Show Gist options
  • Select an option

  • Save dvv/430135 to your computer and use it in GitHub Desktop.

Select an option

Save dvv/430135 to your computer and use it in GitHub Desktop.
/**
* This is a staring point for an application written on Pintura
*/
function dir(){var sys=require('sys');for(var i=0,l=arguments.length;i<l;i++)sys.debug(sys.inspect(arguments[i]));}
function apply(o, c, defaults){
if (defaults) {
apply(o, defaults);
}
if (o && c && typeof c == 'object') {
for (var p in c) {
o[p] = c[p];
}
}
return o;
}
function now() {
var x = new Date().toISOString();
return x;
}
function lorem() {
return 'new-' + Math.random().toString().substring(2);
}
Function.prototype.after = function(fn, scope){
var method = this;
return (typeof fn !== 'function') ?
this :
function(){
var self = this, args = arguments;
var result = method.apply(self, args);
var args = []; for (var i = 0, l = arguments.length; i < l; i++) args.push(arguments[i]);
//fn.apply(scope || self, Array.prototype.concat(args));
fn.apply(scope || self, args);
return result;
};
};
// Defines the capabilities of the users, uncomment once you define access levels
//require("access");
var Facet = require('perstore/facet'),
Model = require('perstore/model').Model,
Store = require('perstore/store/redis').Redis; // require('perstore/store/mongodb').MongoDB;
Notifying = function(store, methods){
// monkey-patch store accessors
// TODO: snoop into request info
var hub = require("tunguska/hub");
(methods || ['get', 'put', 'delete', 'query']).forEach(function(method){
if (store[method]) {
store[method] = store[method].after(function(){
var object;
if (method == 'put' || method == 'delete')
object = arguments;
hub.publish({
channel: 'event',
clientId: 'store',
result: {store: store, action: method, args: arguments},
//type: method,
});
});
}
});
return store;
};
var publicFacets = [Register], userFacets = [], adminFacets = [];
defineEntity = function(name, entityDefinition, logMethods){
var store = Notifying(Store({collection: 'test:'+name}), logMethods);
var model = exports[name] = Model(name, store, entityDefinition || {
});
var publicFacet = Facet.Permissive(model, {
maxQueryLimit: 100
});
publicFacets.push(publicFacet);
var userFacet = Facet.Permissive(model, {
maxQueryLimit: 100
});
userFacets.push(userFacet);
var adminFacet = Facet.Permissive(model, {
maxQueryLimit: 100
});
adminFacets.push(adminFacet);
return {
store: store,
model: model,
publicFacet: publicFacet,
userFacet: userFacet,
adminFacet: adminFacet
};
}
/**
* define the model
*/
var Event = defineEntity('event', {
// model
properties: {
date: {type: 'string', format: 'date-time', 'default': now},
type: {type: 'string', 'default': 'info'},
text: {type: 'string', 'default': 'ping'},
object: {type: 'string', optional: true}, // TODO: ->COLLECTION:ID
user: {type: 'string', optional: true}, // TODO: ->user:ID
ip: {type: 'string', optional: true}, // TODO: from request
comment: {type: 'string', optional: true}, // N.B. for later filling
}
}, []);
var Foo = defineEntity('foo', {
// model
properties: {
text: {type: 'string', 'default': lorem},
}
});
/**
* define logging
*/
// subscribe to event channel
require('tunguska/hub').subscribe('event', function(message){
var msg = message.result;
dir('LOG: ', msg);
Event.publicFacet.put({text: msg.action, object: msg.object});
});
/**
* defines capabilities
*/
var admins = require('commonjs-utils/settings').admins,
Register = require('pintura/security').Register,
security = require('pintura/pintura').config.security;
security.getAllowedFacets = function(user, request){
if (user) {
if (admins && admins.indexOf(user.name) >= 0) {
return adminFacets;
}
return userFacets;
}
return publicFacets;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment