Skip to content

Instantly share code, notes, and snippets.

@RameshRM
Created March 7, 2016 23:31
Show Gist options
  • Save RameshRM/cb14f9b32b2c7d9cfab2 to your computer and use it in GitHub Desktop.
Save RameshRM/cb14f9b32b2c7d9cfab2 to your computer and use it in GitHub Desktop.
'use strict';
var util = require('util');
var async = require('async');
function FeatureStrategy(values) {
this._values = values;
this._strategyId;
}
FeatureStrategy.prototype.resolve = function (context) {
};
FeatureStrategy.prototype.match = function (context) {
var strategyValue = context[this._strategyId];
return this._values.indexOf(strategyValue) > -1;
};
function CompanyStrategy(values) {
this._strategyId = 'company';
this.companies = [];
FeatureStrategy.call(this, values);
}
function UserRoleStrategy(values) {
this._strategyId = 'userrole';
FeatureStrategy.call(this, values);
}
function CustomStrategy(values) {
this._strategyId = 'custom';
FeatureStrategy.call(this, values);
}
CustomStrategy.prototype.match = function (context) {
return true;
};
function UserIdStrategy() {
this._strategyId = 'userid';
this.users = [];
}
util.inherits(CompanyStrategy, FeatureStrategy);
util.inherits(UserRoleStrategy, FeatureStrategy);
util.inherits(UserIdStrategy, FeatureStrategy);
function StrategyResolver() {
this.strategyType = 'inclusive';
this._strategies = [];
var _this = this;
this.addStrategies = function add(strategy) {
_this._strategies.push(strategy);
};
}
StrategyResolver.prototype.resolve = function (context, callback) {
if (typeof callback === 'function') {
callback(undefined, true);
}
};
function Feature() {
this.name;
this.state = false;
this.descriptor;
this.expirationInDays;
this.effectiveDt = new Date();
this.isSuppressed = false;
}
Feature.prototype.StrategyResolver = StrategyResolver;
Feature.prototype.check = function check(context) {
var total = 0;
var target = this.StrategyResolver._strategies.length;
this.StrategyResolver
this.StrategyResolver._strategies.forEach(function forEach(strategy) {
if (strategy.match(context)) {
total++;
}
});
if (this.StrategyResolver.strategyType === 'inclusive') {
return total === target;
} else {
return total > 0;
}
};
var foo = new Feature();
foo.name = 'Foo';
foo.state = false;
foo.descriptor = 'Property foo';
foo.expirationInDays = 30;
foo.effectiveDt = new Date();
foo.StrategyResolver = new StrategyResolver();
foo.StrategyResolver.addStrategies(new CompanyStrategy(['apple']));
foo.StrategyResolver.addStrategies(new UserRoleStrategy(['systemAdmin']));
foo.StrategyResolver.addStrategies(new CustomStrategy(['systemAdmin']));
foo.check({
company: 'apple',
userrole: 'systemAdmin'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment