/** * Expose `AsteroidModule`. */ module.exports = AsteroidModule; /** * Module dependencies. */ var Module = require('module-loader').Module , debug = require('debug')('asteroid-module') , util = require('util') , inherits = util.inherits , assert = require('assert'); /** * Create a new `AsteroidModule` with the given `options`. * * @param {Object} options * @return {AsteroidModule} */ function AsteroidModule(options) { Module.apply(this, arguments); // throw an error if args are not supplied assert(typeof options === 'object', 'AsteroidModule requires an options object'); this.options = validateOptions(options, this.constructor.optionsDefinition); debug('created with options', options); } /** * Inherit from `Module`. */ inherits(AsteroidModule, Module); /** * Define an option of the given key/name with the provided type. */ AsteroidModule.defineOption = function (key, type, options) { var od = this.optionsDefinition = this.optionsDefinition || {}; options = options || {}; options.type = type; od[key] = options; } function validateOptions(options, def) { if(!def) { return options; } Object.keys(def).forEach(function (key) { var val = options[key]; var keyDef = def[key] || {}; if(keyDef.required) { assert(val, key + ' is required!'); } // type assert(typeof val == keyDef.type, key + ' must be a ' + keyDef.type); // size / length if(typeof val.length === 'number') { if(keyDef.min) { assert(val.length >= keyDef.min, key + ' length must be greater than or equal to ', keyDef.min); } if(keyDef.max) { assert(val.length <= keyDef.min, key + ' length must be less than or equal to ', keyDef.max); } } else if(typeof val === 'number') { if(keyDef.min) { assert(val >= keyDef.min, key + ' must be greater than or equal to ', keyDef.min); } if(keyDef.max) { assert(val <= keyDef.max, ' must be less than or equal to ', keyDef.max); } } }); return options; }