Add plugin base class

This commit is contained in:
Brandon Keepers 2016-11-24 21:02:40 -06:00
parent 838f4801d9
commit cc18091356
No known key found for this signature in database
GPG Key ID: F9533396D5FACBF6
4 changed files with 34 additions and 8 deletions

7
lib/plugin.js Normal file
View File

@ -0,0 +1,7 @@
module.exports = class Plugin {
get api() {
const properties = Object.getOwnPropertyNames(this.constructor.prototype);
properties.splice(properties.indexOf('constructor'), 1);
return properties;
}
};

View File

@ -1,6 +1,7 @@
const handlebars = require('handlebars');
const Plugin = require('../plugin');
module.exports = class IssuesPlugin {
module.exports = class Issues extends Plugin {
// checkIfEventApplies(event) {
// return event.issue !== undefined || event.pull_request !== undefined;
// }

View File

@ -11,14 +11,10 @@ module.exports = class Workflow {
this.filterFn = () => true;
this.api = {};
// Define a new function in the API for each plugin method
for (const plugin of plugins) {
// Get all the property names of the plugin
for (const method of Object.getOwnPropertyNames(plugin.constructor.prototype)) {
if (method !== 'constructor') {
// Define a new function in the API for this plugin method, forcing
// the binding to this to prevent any tampering.
this.api[method] = this.proxy(plugin[method]).bind(this);
}
for (const method of plugin.api) {
this.api[method] = this.proxy(plugin[method]).bind(this);
}
}

22
test/plugin.js Normal file
View File

@ -0,0 +1,22 @@
const expect = require('expect');
const Plugin = require('../lib/plugin');
class TestPlugin extends Plugin {
foo() {
}
}
describe('Plugin', () => {
const plugin = new TestPlugin();
describe('api', () => {
it('includes methods', () => {
expect(plugin.api.indexOf('foo') >= 0).toBe(true);
});
it('exludes constructor', () => {
expect(plugin.api.indexOf('constructor')).toBe(-1);
});
});
});