1
0
Fork 0

Implemented staging-style named class definition when implementing interfaces

closure/master
Mike Gerwitz 2011-03-05 03:22:45 -05:00
parent 8749228764
commit 2c2701f4ab
2 changed files with 42 additions and 6 deletions

View File

@ -308,6 +308,17 @@ function createStaging( cname )
return extend.apply( null, args ); return extend.apply( null, args );
}, },
implement: function()
{
// implement on empty base, providing the class name to be used once
// extended
return createImplement(
extend( {} ),
Array.prototype.slice.call( arguments ),
cname
);
},
}; };
} }
@ -320,12 +331,13 @@ function createStaging( cname )
* called, as it does not make sense to create a class without any * called, as it does not make sense to create a class without any
* body/definition. * body/definition.
* *
* @param {Object} base base class to implement atop of * @param {Object} base base class to implement atop of
* @param {Array} ifaces interfaces to implement * @param {Array} ifaces interfaces to implement
* @param {string=} cname optional class name once extended
* *
* @return {Object} intermediate implementation object * @return {Object} intermediate implementation object
*/ */
function createImplement( base, ifaces ) function createImplement( base, ifaces, cname )
{ {
ifaces.push( base ); ifaces.push( base );
@ -334,6 +346,12 @@ function createImplement( base, ifaces )
return { return {
extend: function( def ) extend: function( def )
{ {
// if a name was provided, use it
if ( cname )
{
def.__name = cname;
}
return extend.apply( null, [ return extend.apply( null, [
implement.apply( this, ifaces ), implement.apply( this, ifaces ),
def def

View File

@ -24,7 +24,9 @@
var common = require( './common' ), var common = require( './common' ),
assert = require( 'assert' ), assert = require( 'assert' ),
Class = common.require( 'class' )
Class = common.require( 'class' ),
Interface = common.require( 'interface' )
; ;
@ -158,8 +160,9 @@ var common = require( './common' ),
*/ */
( function testCanCreateNamedClassUsingStagingMethod() ( function testCanCreateNamedClassUsingStagingMethod()
{ {
var name = 'Foo', var name = 'Foo',
named = Class( name ).extend( {} ) named = Class( name ).extend( {} )
namedi = Class( name ).implement( Interface( {} ) ).extend( {} )
; ;
// ensure what was returned is a valid class // ensure what was returned is a valid class
@ -176,5 +179,20 @@ var common = require( './common' ),
'[object Class <' + name + '>]', '[object Class <' + name + '>]',
"Name is set on named clas via staging method" "Name is set on named clas via staging method"
); );
// we should also be able to implement interfaces
assert.equal(
Class.isClass( namedi ),
true,
"Named class generated via staging method, implementing an " +
"interface, is considered to be a valid class"
);
assert.equal(
namedi.toString(),
'[object Class <' + name + '>]',
"Name is set on named class via staging method when implementing"
);
} )(); } )();