1
0
Fork 0

Added ability to override propCopy() parser functions

closure/master
Mike Gerwitz 2010-12-16 21:11:07 -05:00
parent 5126c71a2d
commit a2c894b6bb
2 changed files with 56 additions and 8 deletions

View File

@ -161,9 +161,10 @@ exports.propParse = function( data, options )
* *
* @return undefined * @return undefined
*/ */
exports.propCopy = function( props, dest, result_data ) exports.propCopy = function( props, dest, result_data, actions )
{ {
result_data = result_data || {}; result_data = result_data || {};
actions = actions || {};
// initialize result_data // initialize result_data
var abstract_methods = var abstract_methods =
@ -179,9 +180,9 @@ exports.propCopy = function( props, dest, result_data )
abstract_map[ method ] = i; abstract_map[ method ] = i;
} }
// default functionality // substitute default functionality if needed
var actions = { actions = {
each: function( name, value ) each: actions.each || function( name, value )
{ {
// methods can only be overridden with methods // methods can only be overridden with methods
if ( ( dest[ name ] instanceof Function ) if ( ( dest[ name ] instanceof Function )
@ -192,22 +193,22 @@ exports.propCopy = function( props, dest, result_data )
} }
}, },
property: function( name, value ) property: actions.property || function( name, value )
{ {
dest[ name ] = value; dest[ name ] = value;
}, },
getter: function( name, func ) getter: actions.getter || function( name, func )
{ {
dest.__defineGetter__( name, func ); dest.__defineGetter__( name, func );
}, },
setter: function( name, func ) setter: actions.setter || function( name, func )
{ {
dest.__defineSetter__( name, func ); dest.__defineSetter__( name, func );
}, },
method: function( name, func, is_abstract ) method: actions.method || function( name, func, is_abstract )
{ {
var data = { abstractModified: false }, var data = { abstractModified: false },
pre = dest[ name ]; pre = dest[ name ];

View File

@ -30,6 +30,11 @@ var assert = require( 'assert' ),
var props = { var props = {
one: 1, one: 1,
two: 2, two: 2,
method: function() {},
get val() {},
set val() {},
}; };
var dest = {}; var dest = {};
@ -40,3 +45,45 @@ assert.ok(
"All properties should be copied to the destination object" "All properties should be copied to the destination object"
); );
var each = false,
prop = false,
method = false,
getter = false,
setter = false;
propCopy( props, dest, null, {
each: function()
{
each = true;
},
property: function()
{
prop = true;
},
method: function()
{
method = true;
},
getter: function()
{
getter = true;
},
setter: function()
{
setter = true;
},
} );
[ each, prop, method, getter, setter ].forEach( function( item, i )
{
assert.equal(
item,
true,
"Can override propCopy() parser functions [" + i + "]"
);
});