Skip to content

Commit

Permalink
Action support
Browse files Browse the repository at this point in the history
  • Loading branch information
jaz303 committed Jul 23, 2009
1 parent b3bf04b commit 5028789
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/actions.js
@@ -0,0 +1,36 @@
Function.prototype.__actionName = null;
Function.prototype.actionName = function() { return this.__actionName; };
Function.prototype.isAction = function() { return this.__actionName !== null; };

Function.prototype.actionize = function(name, options) {
if (this.isAction()) {
return this;
} else {
return action(name, options || {}, this);
}
};

function action(name, options, fun) {

if (typeof options == 'function') {
fun = options;
options = {};
}

var action = function() {
if (arguments.callee.isEnabled()) {
return fun.apply(this, arguments);
}
}

action.__actionName = name;
action.__actionEnabled = ('enabled' in options) ? options.enabled : true;

action.isEnabled = function() { return this.__actionEnabled; };
action.disable = function() { this.__actionEnabled = false; };
action.enable = function() { this.__actionEnabled = true; };
action.toggleEnabled = function() { this.__actionEnabled = !this.__actionEnabled; };

return action;

};
74 changes: 74 additions & 0 deletions test/unit/actions.js
@@ -0,0 +1,74 @@
load("../test-unit.js");
load("../../src/actions.js");

test({

"action should be function": function() {
assertEqual('function', typeof action('foo', function() {}));
},

"action should be invoked as function": function() {
assertEqual(10, action('bar', function(a, b) { return a + b; })(6, 4));
},

"vanilla function should not be action": function() {
assert(!(function() {}).isAction());
},

"action should know it is action": function() {
assert(action('bleem', function() {}).isAction());
},

"action should be enabled by default": function() {
assert(action('foo', function() {}).isEnabled());
},

"action should respect enabled setting": function() {
var a = action('a', {enabled: false}, function() {});
assert(!a.isEnabled());
var b = action('b', {enabled: true}, function() {});
assert(b.isEnabled());
},

"action's enabled setting should be toggleable": function() {
var f = action('f', function() {});
f.toggleEnabled();
assert(!f.isEnabled());
f.toggleEnabled();
assert(f.isEnabled());
},

"action should do nothing when disabled": function() {

var foo = 0;
var inc = action('inc', function() { foo++; });

inc();
assertEqual(1, foo);

inc.disable();
inc();
assertEqual(1, foo);

inc.toggleEnabled();
inc();
assertEqual(2, foo);

},

"actionize should change functions into actions": function() {

var fun = function() {};
var act = fun.actionize('foof');

assert(act.isAction());
assertEqual('foof', act.actionName());

},

"actionize should leave existing actions untouched": function() {
var act = action('boof', function() {});
assertEqual(act, act.actionize());
}

});

0 comments on commit 5028789

Please sign in to comment.