Skip to content

Commit

Permalink
nested factories fully working
Browse files Browse the repository at this point in the history
  • Loading branch information
mastilver committed Jun 21, 2015
1 parent ccbfb11 commit 4785027
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 47 deletions.
53 changes: 34 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = function(sails){
},

parsePolicies: parsePolicies,
parsePolicy: parsePolicy,
parseEsprima: parseEsprima,
};
}

Expand All @@ -37,37 +37,52 @@ function parsePolicies(input){


if(type === 'string'){
var parsedString = esprima.parse(input).body[0].expression;
return this.parseEsprima(parsedString);
}

var functionMatches = input.match(/^(.*)\((.*)\)$/);

return input;
}

// the policy is a function
if(functionMatches !== null){

var parsed = esprima.parse(functionMatches[0]);
/*
input [Esprima Object]
*/
function parseEsprima(input){

var type = input.type;

var functionName = parsed.body[0].expression.callee.name;
var args = parsed.body[0].expression.arguments.map(function(arg){
return arg.value;
});
if(type === esprimaType.value){
return input.value;
}


var policyFactory = require(sails.config.paths.policiesFactories + '/' + functionName);
if(type === esprimaType.policy){

var policy = policyFactory.apply(this, args);
var policyName = input.name;

return policy;
}
return require(sails.config.paths.policies + '/' + policyName);
}


return input;
}
if(type === esprimaType.factory){

var factoryName = input.callee.name;

var factory = require(sails.config.paths.policiesFactories + '/' + factoryName);
var args = input.arguments.map(this.parseEsprima, this);

return factory.apply(this, args);
}


throw new Error('esprima type unhandled: ' + type);
}

/*
input [Esprima Object]
*/
function parsePolicy(input){

var esprimaType = {
value: 'Literal',
policy: 'Identifier',
factory: 'CallExpression',
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sails-hook-parametized-policies",
"version": "0.1.1",
"version": "0.2.0",
"description": "",
"main": "index.js",
"directories": {
Expand Down
6 changes: 3 additions & 3 deletions test/nestedSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ describe('parametized policies hook - nested factories', function(){
},
policies: {
'NestedController': {
'policiesArgs': ['or(isAdmin, accept)'],
'mixedArgs': 'or(isAdmin, is(\'User\'))',
'factoriesArgs': 'or(is(\'Admin\'), is(\'User\'))',
'policiesArgs': ['and(isAdmin, accept)'],
'mixedArgs': 'and(isAdmin, is(\'User\'))',
'factoriesArgs': 'and(is(\'Admin\'), is(\'User\'))',
}
}
}, function(err, _sails){
Expand Down
11 changes: 11 additions & 0 deletions test/policiesFactories/and.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

module.exports = function(firstFn, secondFn){

return function(req, res, next){

firstFn(req, res, function(){

secondFn(req, res, next);
});
};
};
24 changes: 0 additions & 24 deletions test/policiesFactories/or.js

This file was deleted.

0 comments on commit 4785027

Please sign in to comment.