keeto/mootools-pattern-mutators — Issue #2: refactoring
Author: revnode
Created: 2011-12-23 21:39:46+00:00
State: open

Thank you so much for writing this!

I refactored your code a bit and wanted to share:

var matchers = [];

Class.defineMutator = function(regex, fn)
{
matchers.push(regex);
Class.Mutators['$mutator:' + regex] = function(values)
{
return fn.apply(this, values);
};
return this;
};

var define = Class.prototype.implement;
Class.implement('define', define);

var implement = Class.prototype.implement = function(key, value, retain)
{
matchers.each(function(matcher)
{
var match = key.match(matcher);
if(match)
{
match[0]	= value;
key	= '$mutator:' + matcher;
value	= match;
}
});
return define.call(this, key, value, retain);
}.overloadSetter();

I can't wait until this lands in mootools core! Thanks!