1
0
Fork 0

Added support for getters/setters in propParse()

closure/master
Mike Gerwitz 2010-12-05 20:50:13 -05:00
parent 20b78ba73a
commit fd2b3ccc6d
2 changed files with 52 additions and 8 deletions

View File

@ -94,20 +94,35 @@ exports.defineSecureProp = function( obj, prop, value )
* *
* @return undefined * @return undefined
*/ */
exports.propSort = function( data, options ) exports.propParse = function( data, options )
{ {
var callback_prop = options.property || function() {}, var fvoid = function() {},
callback_method = options.method || function() {}, callback_prop = options.property || fvoid,
callback_abstract_method = options.abstractMethod || function() {}, callback_method = options.method || fvoid,
callback_abstract_method = options.abstractMethod || fvoid,
callback_getter = options.getter || fvoid,
callback_setter = options.setter || fvoid,
callback = null; callback = null;
// for each of the given properties, determine what type of property we're // for each of the given properties, determine what type of property we're
// dealing with (in the classic OO sense) // dealing with (in the classic OO sense)
for ( prop in data ) for ( prop in data )
{ {
var value = data[ prop ]; var value = data[ prop ],
getter = ( ( getset ) ? data.__lookupGetter__( prop ) : null ),
setter = ( ( getset ) ? data.__lookupSetter__( prop ) : null );
if ( value instanceof Function ) // getter/setter
if ( getter || setter )
{
callback_getter( prop, getter );
callback_setter( prop, setter );
// we're done
continue;
}
// method
else if ( value instanceof Function )
{ {
// concrete or abstract? // concrete or abstract?
callback = ( exports.isAbstractMethod( value ) ) callback = ( exports.isAbstractMethod( value ) )
@ -115,6 +130,7 @@ exports.propSort = function( data, options )
: callback_method : callback_method
; ;
} }
// simple property
else else
{ {
callback = callback_prop; callback = callback_prop;

View File

@ -40,6 +40,10 @@ var data = {
// object (property) // object (property)
propObj: {}, propObj: {},
// getter/setter
get someFoo() {},
set someFoo() {},
// concrete method // concrete method
method: function() {}, method: function() {},
@ -50,7 +54,9 @@ var data = {
var props = {}, var props = {},
methods = {}, methods = {},
amethods = {}; amethods = {},
getters = {},
setters = {};
util.propParse( data, { util.propParse( data, {
property: function( name, value ) property: function( name, value )
@ -67,7 +73,17 @@ util.propParse( data, {
abstractMethod: function( name, def ) abstractMethod: function( name, def )
{ {
amethods[ name ] = def; amethods[ name ] = def;
} },
getter: function( name, func )
{
getters[ name ] = func;
},
setter: function( name, func )
{
setters[ name ] = func;
},
} ); } );
@ -94,3 +110,15 @@ assert.equal(
"Property parser properly detects abstract methods" "Property parser properly detects abstract methods"
); );
assert.equal(
getters.someFoo,
data.__lookupGetter__( 'someFoo' ),
"Property parser properly detects getters"
);
assert.equal(
setters.someFoo,
data.__lookupSetter__( 'someFoo' ),
"Property parser properly detects setters"
);