From b294f844815fda0272abd4dd0b2a3b2778253db9 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 1 Dec 2010 19:27:40 -0500 Subject: [PATCH] Began Interface development --- lib/class.js | 13 ++------- lib/interface.js | 64 ++++++++++++++++++++++++++++++++++++++++++ lib/util.js | 14 +++++++++ test/test-interface.js | 46 ++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+), 11 deletions(-) create mode 100644 lib/interface.js create mode 100644 test/test-interface.js diff --git a/lib/class.js b/lib/class.js index c252659..dccdd84 100644 --- a/lib/class.js +++ b/lib/class.js @@ -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(); /** diff --git a/lib/interface.js b/lib/interface.js new file mode 100644 index 0000000..ad90bc0 --- /dev/null +++ b/lib/interface.js @@ -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 . + * + * @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; +} + diff --git a/lib/util.js b/lib/util.js index 11fc5e9..fac7496 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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 * diff --git a/test/test-interface.js b/test/test-interface.js new file mode 100644 index 0000000..a5d959f --- /dev/null +++ b/test/test-interface.js @@ -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 . + * + * @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" +); +