Skip to content

Instantly share code, notes, and snippets.

@fed135
Created October 24, 2017 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fed135/b4c497984cc71f039a58899a20c5bfce to your computer and use it in GitHub Desktop.
Save fed135/b4c497984cc71f039a58899a20c5bfce to your computer and use it in GitHub Desktop.
Reveal framework (IV)
/**
* IV Reveal framework
*
* I wanted to simplify and standardize the way I was building applications and leverage some great patterns like dependency injection, reveal and factories.
* Unfortunatly, conveinience and "protecting the user from himself" by de-listing some private methods caused a few headaches during testing.
* This aims at solving all those.
*
* Let's consider a module 'Server'
*
* --- Module
*
* const iv = require('iv');
*
* function Server(options) {
* return iv.compose((scope) => [
* // Public members
* {
* connect: () => {
* // scope allows me full access to public and private components
* // I could also have passed options, or any given object as a first parameter of compose to make add it to the mixin
* }
* },
* // Private members
* {
* resolveIp: () => {
* // This method will not be made visible, although accessible.
* // By passing the environment variable IV_MODE=testing, this property will magically become visible
* }
* }
* ]);
* }
*
*/
function compose(scope, fn) {
if (typeof scope === 'function') {
fn = scope;
scope = {};
}
let localScope = {};
const model = fn(localScope);
localScope = Object.assign(localScope, scope, model[0] || {}, model[1] || {});
if (process.env.IV_MODE !== 'testing') Object.keys(model[1] || {}).forEach(key => Object.defineProperty(localScope, key, { enumerable: false }));
return localScope;
}
module.exports = { compose };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment