Created
October 24, 2017 23:50
-
-
Save fed135/b4c497984cc71f039a58899a20c5bfce to your computer and use it in GitHub Desktop.
Reveal framework (IV)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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