1
0
Fork 0

Began Interface development

closure/master
Mike Gerwitz 2010-12-01 19:27:40 -05:00
parent 1e2e73c9f0
commit b294f84481
4 changed files with 126 additions and 11 deletions

View File

@ -22,17 +22,8 @@
* @package core
*/
var propCopy = require( './util' ).propCopy;
/**
* Whether the Object.freeze() method is available
* @var {boolean}
*/
var can_freeze = ( Object.seal === undefined )
? false
: true
;
var propCopy = require( './util' ).propCopy,
can_freeze = require( './util' ).canFreeze();
/**

64
lib/interface.js 100644
View File

@ -0,0 +1,64 @@
/**
* 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
*/
var can_freeze = require( './util' ).canFreeze();
/**
* 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 ),
props = args.pop() || {},
base = args.pop() || Interface,
prototype = new base();
var new_interface = {};
// freeze the interface (preventing additions) if supported
if ( can_freeze )
{
Object.freeze( new_interface );
}
return new_interface;
}

View File

@ -33,6 +33,20 @@ var getset = ( Object.prototype.__defineGetter__ === undefined )
;
/**
* Whether the Object.freeze() method is available
*
* @return {boolean}
*/
exports.canFreeze = function()
{
return ( Object.seal === undefined )
? false
: true
;
}
/**
* Copies properties to the destination object
*

View File

@ -0,0 +1,46 @@
/**
* Tests interfaces
*
* 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 test
*/
require( './common' );
var assert = require( 'assert' ),
Class = require( 'class' ),
Interface = require( 'interface' );
var FooType = Interface.extend(
{
foo: function( foo, bar ) {},
});
assert.ok(
( FooType instanceof Object ),
"Interface extend method creates a new object"
);
assert.equal(
Object.isFrozen( FooType ),
true,
"Generated interface object should be frozen"
);