Skip to content

Provide an AMD with amdefine. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"protocol"
],
"dependencies": {
"amdefine": "~1",
"underscore": "1.5.2"
},
"devDependencies": {
Expand Down
194 changes: 99 additions & 95 deletions src/i.js
Original file line number Diff line number Diff line change
@@ -1,123 +1,127 @@
'use strict';

var _ = require('underscore');
if (typeof define !== 'function') {
var define = require('amdefine')(module);
}

function noOp() {}
define(['underscore'], function (_) {
function noOp() {}

function addFunctionNameToObject(functionName, object) {
if (functionName && _.isString(functionName)) {
object[functionName] = 'function';
function addFunctionNameToObject(functionName, object) {
if (functionName && _.isString(functionName)) {
object[functionName] = 'function';
}
}
}

function processBlock(block, blockName, outObject) {
if (_.isArray(block[blockName])) {
outObject[blockName] = outObject[blockName] || {};
_.each(block[blockName], function (value) {
addFunctionNameToObject(value, outObject[blockName]);
});
} else if (_.isObject(block[blockName])) {
outObject[blockName] = outObject[blockName] || {};
_.each(block[blockName], function (value, key) {
addFunctionNameToObject(key, outObject[blockName]);
});
function processBlock(block, blockName, outObject) {
if (_.isArray(block[blockName])) {
outObject[blockName] = outObject[blockName] || {};
_.each(block[blockName], function (value) {
addFunctionNameToObject(value, outObject[blockName]);
});
} else if (_.isObject(block[blockName])) {
outObject[blockName] = outObject[blockName] || {};
_.each(block[blockName], function (value, key) {
addFunctionNameToObject(key, outObject[blockName]);
});
}
}
}

function buildInterface(interfaceDescription) {
var result = {
required: {}
};
function buildInterface(interfaceDescription) {
var result = {
required: {}
};

if (_.isArray(interfaceDescription)) {
_.each(interfaceDescription, function (methodName) {
addFunctionNameToObject(methodName, result.required);
});
} else if (_.isObject(interfaceDescription)) {
processBlock(interfaceDescription, 'required', result);
processBlock(interfaceDescription, 'optional', result);
}

if (_.isArray(interfaceDescription)) {
_.each(interfaceDescription, function (methodName) {
addFunctionNameToObject(methodName, result.required);
});
} else if (_.isObject(interfaceDescription)) {
processBlock(interfaceDescription, 'required', result);
processBlock(interfaceDescription, 'optional', result);
return result;
}

return result;
}
var Methodical = function (interfaceDescription) {
this._interface = buildInterface(interfaceDescription);
};

var Methodical = function (interfaceDescription) {
this._interface = buildInterface(interfaceDescription);
};
Methodical.function = 'function';

Methodical.function = 'function';
Methodical.fromConstructor = function (Constructor) {
var requiredMethods;

Methodical.fromConstructor = function (Constructor) {
var requiredMethods;
if (_.isFunction(Constructor)) {
requiredMethods = [];
_.each(Constructor.prototype, function (value, key) {
if (_.isFunction(value)) {
requiredMethods.push(key);
}
});
} else {
throw new TypeError('A function must be passed');
}

if (_.isFunction(Constructor)) {
requiredMethods = [];
_.each(Constructor.prototype, function (value, key) {
if (_.isFunction(value)) {
requiredMethods.push(key);
}
});
} else {
throw new TypeError('A function must be passed');
}
return new Methodical(requiredMethods);
};

return new Methodical(requiredMethods);
};
Methodical.prototype.getInterface = function () {
return this._interface;
};

Methodical.prototype.getInterface = function () {
return this._interface;
};
Methodical.prototype.check = function (object) {
var errors = [];

Methodical.prototype.check = function (object) {
var errors = [];
// ensures that if we pass garbage in, we get sensible error messages
if (!_.isObject(object)) {
object = {};
}

// ensures that if we pass garbage in, we get sensible error messages
if (!_.isObject(object)) {
object = {};
}
_.each(this.getInterface().required, _.bind(function (value, key) {
if (typeof object[key] !== value) {
errors.push('The required method "' + key + '" is not implemented');
}
}, this));

_.each(this.getInterface().required, _.bind(function (value, key) {
if (typeof object[key] !== value) {
errors.push('The required method "' + key + '" is not implemented');
if (errors.length) {
var throwable = new TypeError();
var messageIntro = 'The object does not conform to the interface: ';
if (this.name) {
messageIntro = 'The object does not conform to the "' + this.name + '" interface: ';
}
throwable.message = messageIntro + errors.join('; ');
throw throwable;
}
}, this));

if (errors.length) {
var throwable = new TypeError();
var messageIntro = 'The object does not conform to the interface: ';
if (this.name) {
messageIntro = 'The object does not conform to the "' + this.name + '" interface: ';
}
throwable.message = messageIntro + errors.join('; ');
throw throwable;
}
};

Methodical.prototype.complete = function (object) {

};
if (!_.isFunction(object) && !_.isObject(object)) {
throw new TypeError('Cannot complete a non-object');
}

Methodical.prototype.complete = function (object) {
_.each(this.getInterface().required, _.bind(function (value, key) {
if (typeof object[key] !== value) {
object[key] = noOp;
}
}, this));
};

if (!_.isFunction(object) && !_.isObject(object)) {
throw new TypeError('Cannot complete a non-object');
}
Methodical.prototype.tryCall = function (object, method) {
var methodArguments = Array.prototype.slice.call(arguments, 2);
this.tryApply(object, method, methodArguments);
};

_.each(this.getInterface().required, _.bind(function (value, key) {
if (typeof object[key] !== value) {
object[key] = noOp;
Methodical.prototype.tryApply = function (object, method, args) {
if (_.isFunction(object[method])) {
object[method].apply(object, args);
} else if (this.getInterface().required[method] === 'function') {
object[method].apply(object, args);
}
}, this));
};

Methodical.prototype.tryCall = function (object, method) {
var methodArguments = Array.prototype.slice.call(arguments, 2);
this.tryApply(object, method, methodArguments);
};

Methodical.prototype.tryApply = function (object, method, args) {
if (_.isFunction(object[method])) {
object[method].apply(object, args);
} else if (this.getInterface().required[method] === 'function') {
object[method].apply(object, args);
}
};
};

module.exports = Methodical;
return Methodical;
});