From b42dcbe7d17b77cf277ee2562b32b51d14fcf297 Mon Sep 17 00:00:00 2001 From: Matthias Feldmann Date: Sat, 9 Jul 2016 21:26:13 +0200 Subject: [PATCH] added option to pass caller to modules --- .npmignore | 1 + manual/changelog.md | 2 ++ manual/example_ModuleManager.md | 2 ++ manual/usage_ModuleManager.md | 3 +++ package.json | 2 +- src/index.js | 4 ++++ test/modulemanager.spec.js | 11 +++++++++++ 7 files changed, 24 insertions(+), 1 deletion(-) diff --git a/.npmignore b/.npmignore index d54e773..f2b1794 100644 --- a/.npmignore +++ b/.npmignore @@ -1,4 +1,5 @@ src/ +test/ .git doc manual diff --git a/manual/changelog.md b/manual/changelog.md index 71c5be5..bd77fba 100644 --- a/manual/changelog.md +++ b/manual/changelog.md @@ -4,3 +4,5 @@ * added node specification # 1.0.2 (2016-07-08) Fixed babel sourcemaps +# 1.1.0 (2016-07-09) +Added option to pass the caller to the modules diff --git a/manual/example_ModuleManager.md b/manual/example_ModuleManager.md index 4bff141..1298961 100644 --- a/manual/example_ModuleManager.md +++ b/manual/example_ModuleManager.md @@ -6,6 +6,8 @@ const options = { folder: './folder/plugins', // loads plugin1 and plugin2 from ./folder/plugins and plugin3 from ../ moduleList: ['plugin1', 'plugin2', {name: 'plugin3', path: '../plug3'}], + // pass modulemanager down to modules + passCaller: true, // the options will be available through this.KEY options: { app: app, // available through this.app diff --git a/manual/usage_ModuleManager.md b/manual/usage_ModuleManager.md index 927e448..4e60909 100644 --- a/manual/usage_ModuleManager.md +++ b/manual/usage_ModuleManager.md @@ -14,6 +14,7 @@ Place the variables in a JSON Object | --------- | ------------ | --------- | ------------------------------------ | | yes | folder | ./modules | folderpath relative to current file as string | | yes | moduleList | | List of modules by name or name and path to load | +| yes | passCaller | true | pass the modulemanager as this.parent to modules | | yes | options | | JSON Object for options to pass to modules | ### Example @@ -22,6 +23,8 @@ const options = { folder: './folder/plugins', // loads plugin1 and plugin2 from ./folder/plugins and plugin3 from ../ moduleList: ['plugin1', 'plugin2', {name: 'plugin3', path: '../plug3'}], + // pass modulemanager down to modules + passCaller: true, // the options will be available through this.KEY options: { app: app, // available through this.app diff --git a/package.json b/package.json index ec2155b..c8bd536 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "modulemanager", - "version": "1.0.2", + "version": "1.1.0", "description": "a module manager to enable a modular structure", "main": "dist/index.js", "scripts": { diff --git a/src/index.js b/src/index.js index 363d0ae..7b27545 100644 --- a/src/index.js +++ b/src/index.js @@ -14,6 +14,8 @@ export default class ModuleManager { * @param {!Object} options - requires a json object with options * @param {string} [options.folder='modules'] - string with the folderPath relative to caller * @param {boolean} [options.logging=false] - logs if a method is run + * @param {boolean} [options.done] - called when constructor completed + * @param {boolean} [options.passCaller=true] - pass modulemanager as parrent to modules * @param {Array} [options.moduleList] - array of modules to activate * @param {Object} [options.moduleList.name] - module name * @param {Object} [options.moduleList.path] - path to the module @@ -28,6 +30,7 @@ export default class ModuleManager { const stack = callsite(); const blank = () => {}; const done = options.done || blank(); + const caller = options.passCaller || true; /** * Creates a module from the module subclass * @private @@ -64,6 +67,7 @@ export default class ModuleManager { * @type {Object} */ this.options = options.options || {}; + if (caller === true) this.options.parent = this; /** * logging when function is run * @type {boolean} diff --git a/test/modulemanager.spec.js b/test/modulemanager.spec.js index f8e6187..3abfa1c 100644 --- a/test/modulemanager.spec.js +++ b/test/modulemanager.spec.js @@ -148,6 +148,7 @@ describe('ModuleManager', () => { beforeEach(() => { mmanager = new ModuleManager({ folder: './Modules', + caller: true, options: { test, }, @@ -156,6 +157,16 @@ describe('ModuleManager', () => { afterEach(() => { test.reset(); }); + /** + * @test {ModuleManager#createModule} + */ + it('ModuleManager#createModule passes caller', + mochaAsync(async() => { + const mod = await mmanager + .createModule('Module2', './Modules/Mod2'); + expect(mod.module.parent).to.be.equal(mmanager); + }) + ); /** * @test {ModuleManager} */