2010-12-01 19:27:40 -05:00
|
|
|
/**
|
|
|
|
* Contains interface module
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Mike Gerwitz
|
|
|
|
*
|
|
|
|
* This file is part of ease.js.
|
|
|
|
*
|
|
|
|
* ease.js is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @author Mike Gerwitz
|
|
|
|
* @package core
|
|
|
|
*/
|
|
|
|
|
2010-12-01 20:41:54 -05:00
|
|
|
var util = require( './util' );
|
2010-12-01 19:27:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an interface
|
|
|
|
*
|
|
|
|
* @return {Interface} extended interface
|
|
|
|
*/
|
|
|
|
exports.extend = function()
|
|
|
|
{
|
|
|
|
return extend.apply( this, arguments );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-01 21:39:41 -05:00
|
|
|
exports.abstractMethod = util.createAbstractMethod;
|
|
|
|
|
|
|
|
|
2010-12-01 19:27:40 -05:00
|
|
|
/**
|
|
|
|
* Default interface implementation
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function Interface() {}
|
|
|
|
|
|
|
|
|
|
|
|
function extend()
|
|
|
|
{
|
|
|
|
var args = Array.prototype.slice.call( arguments ),
|
2010-12-01 19:38:30 -05:00
|
|
|
props = prop_check( args.pop() ) || {},
|
2010-12-01 19:27:40 -05:00
|
|
|
base = args.pop() || Interface,
|
|
|
|
prototype = new base();
|
|
|
|
|
2010-12-01 23:01:20 -05:00
|
|
|
var new_interface = function() {};
|
|
|
|
|
|
|
|
util.propCopy( props, prototype );
|
|
|
|
|
|
|
|
new_interface.prototype = prototype;
|
|
|
|
new_interface.constructor = new_interface;
|
2010-12-01 19:27:40 -05:00
|
|
|
|
2010-12-01 20:41:54 -05:00
|
|
|
// freeze the interface (preventing additions), if supported
|
|
|
|
util.freeze( new_interface );
|
2010-12-01 19:27:40 -05:00
|
|
|
|
|
|
|
return new_interface;
|
|
|
|
}
|
|
|
|
|
2010-12-01 19:38:30 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks to ensure that only methods are being declared
|
|
|
|
*
|
|
|
|
* @param {Object} props interface definition
|
|
|
|
*
|
|
|
|
* @return {Object} the same properties that were passed to the function
|
|
|
|
*/
|
|
|
|
function prop_check( props )
|
|
|
|
{
|
|
|
|
for ( prop in props )
|
|
|
|
{
|
2010-12-01 21:39:41 -05:00
|
|
|
if ( util.isAbstractMethod( props[ prop ] ) === false )
|
2010-12-01 19:38:30 -05:00
|
|
|
{
|
|
|
|
throw new Error(
|
2010-12-01 21:39:41 -05:00
|
|
|
"Only abstract methods are permitted within Interface definitons"
|
2010-12-01 19:38:30 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|