Skip to content

Commit

Permalink
added option to pass caller to modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Feldmann committed Jul 9, 2016
1 parent 7e1e82c commit b42dcbe
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions .npmignore
@@ -1,4 +1,5 @@
src/
test/
.git
doc
manual
Expand Down
2 changes: 2 additions & 0 deletions manual/changelog.md
Expand Up @@ -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
2 changes: 2 additions & 0 deletions manual/example_ModuleManager.md
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions manual/usage_ModuleManager.md
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion 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": {
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Expand Up @@ -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<string, Object>} [options.moduleList] - array of modules to activate
* @param {Object} [options.moduleList.name] - module name
* @param {Object} [options.moduleList.path] - path to the module
Expand All @@ -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
Expand Down Expand Up @@ -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}
Expand Down
11 changes: 11 additions & 0 deletions test/modulemanager.spec.js
Expand Up @@ -148,6 +148,7 @@ describe('ModuleManager', () => {
beforeEach(() => {
mmanager = new ModuleManager({
folder: './Modules',
caller: true,
options: {
test,
},
Expand All @@ -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}
*/
Expand Down

0 comments on commit b42dcbe

Please sign in to comment.