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-28 21:56:55 -05:00
|
|
|
var util = require( './util' ),
|
|
|
|
Class = require( './class' );
|
2010-12-01 19:27:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an interface
|
|
|
|
*
|
|
|
|
* @return {Interface} extended interface
|
|
|
|
*/
|
|
|
|
exports.extend = function()
|
|
|
|
{
|
|
|
|
return extend.apply( this, arguments );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default interface implementation
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function Interface() {}
|
|
|
|
|
|
|
|
|
|
|
|
function extend()
|
|
|
|
{
|
|
|
|
var args = Array.prototype.slice.call( arguments ),
|
2010-12-28 20:13:50 -05:00
|
|
|
props = args.pop() || {},
|
2010-12-01 19:27:40 -05:00
|
|
|
base = args.pop() || Interface,
|
|
|
|
prototype = new base();
|
|
|
|
|
2010-12-28 21:56:55 -05:00
|
|
|
// sanity check
|
|
|
|
inheritCheck( prototype );
|
|
|
|
|
2010-12-01 23:01:20 -05:00
|
|
|
var new_interface = function() {};
|
|
|
|
|
2010-12-28 20:13:50 -05:00
|
|
|
util.propCopy( props, prototype, {
|
|
|
|
each: function( name, value )
|
|
|
|
{
|
|
|
|
if ( util.isAbstractMethod( value ) === false )
|
|
|
|
{
|
|
|
|
throw TypeError(
|
|
|
|
"Only abstract methods are permitted within Interface " +
|
|
|
|
"definitons"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
} );
|
2010-12-01 23:01:20 -05:00
|
|
|
|
2010-12-28 22:08:30 -05:00
|
|
|
attachExtend( new_interface );
|
2010-12-01 23:01:20 -05:00
|
|
|
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
|
|
|
|
2010-12-28 21:56:55 -05:00
|
|
|
/**
|
|
|
|
* Assures that the parent object is a valid object to inherit from
|
|
|
|
*
|
|
|
|
* This method allows inheriting from any object (note that it will likely cause
|
|
|
|
* errors if not an interface), but will place restrictions on objects like
|
|
|
|
* Classes that do not make sense to inherit from. This will provide a more
|
|
|
|
* friendly error, with suggestions on how to resolve the issue, rather than a
|
|
|
|
* cryptic error resulting from inheritance problems.
|
|
|
|
*
|
|
|
|
* This method will throw an exception if there is a violation.
|
|
|
|
*
|
|
|
|
* @param {Object} prototype prototype to check for inheritance flaws
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function inheritCheck( prototype )
|
|
|
|
{
|
|
|
|
// if we're inheriting from another interface, then we're good
|
|
|
|
if ( prototype instanceof Interface )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Class.isClassInstance( prototype ) )
|
|
|
|
{
|
|
|
|
throw new TypeError(
|
|
|
|
"Interfaces cannot extend from classes. Try creating an " +
|
|
|
|
"abstract class instead."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-01 23:27:31 -05:00
|
|
|
/**
|
|
|
|
* Attaches extend method to the given function (interface)
|
|
|
|
*
|
|
|
|
* @param {Function} func function (interface) to attach method to
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2010-12-28 22:08:30 -05:00
|
|
|
function attachExtend( func )
|
2010-12-01 23:27:31 -05:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Shorthand for extending interfaces
|
|
|
|
*
|
|
|
|
* This method can be invoked on the object, rather than having to call
|
|
|
|
* Interface.extend( this ).
|
|
|
|
*
|
|
|
|
* @param {Object} props properties to add to extended interface
|
|
|
|
*
|
|
|
|
* @return {Object} extended interface
|
|
|
|
*/
|
|
|
|
util.defineSecureProp( func, 'extend', function( props )
|
|
|
|
{
|
|
|
|
return extend( this, props );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|