Skip to content

Instantly share code, notes, and snippets.

@ColinCampbell
Created June 23, 2011 01:14
Show Gist options
  • Save ColinCampbell/1041690 to your computer and use it in GitHub Desktop.
Save ColinCampbell/1041690 to your computer and use it in GitHub Desktop.
diff --git a/packages/sproutcore-utils/lib/main.js b/packages/sproutcore-utils/lib/main.js
new file mode 100644
index 0000000..db3d9a9
--- /dev/null
+++ b/packages/sproutcore-utils/lib/main.js
@@ -0,0 +1,10 @@
+// ==========================================================================
+// Project: SproutCore
+// Copyright: ©2006-2011 Strobe Inc. and contributors.
+// Portions ©2008-2011 Apple Inc. All rights reserved.
+// License: Licensed under MIT license (see license.js)
+// ==========================================================================
+
+require('sproutcore-runtime');
+require('sproutcore-utils/responds_to');
+require('sproutcore-utils/try_to_perform');
\ No newline at end of file
diff --git a/packages/sproutcore-utils/lib/responds_to.js b/packages/sproutcore-utils/lib/responds_to.js
new file mode 100644
index 0000000..1024826
--- /dev/null
+++ b/packages/sproutcore-utils/lib/responds_to.js
@@ -0,0 +1,29 @@
+// ==========================================================================
+// Project: SproutCore
+// Copyright: ©2006-2011 Strobe Inc. and contributors.
+// Portions ©2008-2011 Apple Inc. All rights reserved.
+// License: Licensed under MIT license (see license.js)
+// ==========================================================================
+
+var slice = Array.prototype.slice;
+
+var respondsTo = function(obj, methodName) {
+ return !!(obj[methodName] instanceof Function);
+};
+
+/**
+ @param {Object} obj The object to check for the method
+ @param {String} methodName The method name to check for
+*/
+SC.respondsTo = respondsTo;
+
+SC.Object.reopen(
+/** @scope SC.Object.prototype */{
+
+ respondsTo: function() {
+ var args = slice(arguments);
+ args.push(this);
+ return SC.respondsTo.apply(SC, args);
+ }
+
+});
\ No newline at end of file
diff --git a/packages/sproutcore-utils/lib/try_to_perform.js b/packages/sproutcore-utils/lib/try_to_perform.js
new file mode 100644
index 0000000..0ed2ac2
--- /dev/null
+++ b/packages/sproutcore-utils/lib/try_to_perform.js
@@ -0,0 +1,43 @@
+// ==========================================================================
+// Project: SproutCore
+// Copyright: ©2006-2011 Strobe Inc. and contributors.
+// Portions ©2008-2011 Apple Inc. All rights reserved.
+// License: Licensed under MIT license (see license.js)
+// ==========================================================================
+
+require('sproutcore-utils/responds_to');
+
+var slice = Array.prototype.slice;
+
+var tryToPerform = function(obj, methodName) {
+ var args = slice(arguments);
+ args.shift(); args.shift();
+
+ return SC.respondsTo(obj, methodName) && (obj[methodName].apply(obj, args) !== false);
+};
+
+/**
+ Checks to see if the `methodName` exists on the `obj`,
+ and if it does, invokes it with the arguments passed.
+
+ @function
+
+ @param {Object} obj The object to check for the method
+ @param {String} methodName The method name to check for
+ @param {Object...} args The arguments to pass to the method
+
+ @returns {Boolean} true if the method does not return false
+ @returns {Boolean} false otherwise
+*/
+SC.tryToPerform = tryToPerform;
+
+SC.Object.reopen(
+/** @scope SC.Object.prototype */{
+
+ tryToPerform: function() {
+ var args = slice(arguments);
+ args.shift(this);
+ return SC.tryToPerform.call(SC, args);
+ }
+
+});
\ No newline at end of file
diff --git a/packages/sproutcore-utils/package.json b/packages/sproutcore-utils/package.json
new file mode 100644
index 0000000..97323f1
--- /dev/null
+++ b/packages/sproutcore-utils/package.json
@@ -0,0 +1,20 @@
+{
+ "name": "sproutcore-utils",
+ "summary": "SproutCore Utils",
+ "description": "A collection of utility functions for SproutCore",
+ "homepage": "http://sproutcore.com",
+ "author": "Strobe Inc., Apple, Inc., and contributors",
+ "version": "2.0.beta.1",
+ "dependencies": {
+ "sproutcore-runtime": "2.0",
+ "sproutcore-preprocessor": "~> 0.0.2"
+ },
+ "plugin:preprocessors": [
+ "sproutcore-preprocessor/preprocessor"
+ ],
+ "directories": {
+ "lib" : "./lib",
+ "tests" : "./tests"
+ }
+}
+
diff --git a/packages/sproutcore-utils/tests/responds_to_test.js b/packages/sproutcore-utils/tests/responds_to_test.js
new file mode 100644
index 0000000..af4070d
--- /dev/null
+++ b/packages/sproutcore-utils/tests/responds_to_test.js
@@ -0,0 +1,32 @@
+// ==========================================================================
+// Project: SproutCore
+// Copyright: ©2006-2011 Strobe Inc. and contributors.
+// Portions ©2008-2011 Apple Inc. All rights reserved.
+// License: Licensed under MIT license (see license.js)
+// ==========================================================================
+/*globals module test ok isObj equals expects same plan */
+
+var obj;
+
+module("SC.respondsTo", {
+ setup: function() {
+ obj = SC.Object.create({
+ foo: "bar",
+ total: 12345,
+ aMethodThatExists: function() {},
+ aMethodThatReturnsTrue: function() { return true; },
+ aMethodThatReturnsFoobar: function() { return "Foobar"; },
+ aMethodThatReturnsFalse: function() { return NO; }
+ });
+ },
+
+ teardown: function() {
+ obj = undefined ;
+ }
+
+});
+
+test("Checks if methods exist using the 'SC.respondsTo' method", function() {
+ equals(SC.respondsTo(obj, 'aMethodThatExists'), true);
+ equals(SC.respondsTo(obj, 'aMethodThatDoesNotExist'), false);
+});
\ No newline at end of file
diff --git a/packages/sproutcore-utils/tests/try_to_perform_test.js b/packages/sproutcore-utils/tests/try_to_perform_test.js
new file mode 100644
index 0000000..e5f6a8c
--- /dev/null
+++ b/packages/sproutcore-utils/tests/try_to_perform_test.js
@@ -0,0 +1,38 @@
+// ==========================================================================
+// Project: SproutCore
+// Copyright: ©2006-2011 Strobe Inc. and contributors.
+// Portions ©2008-2011 Apple Inc. All rights reserved.
+// License: Licensed under MIT license (see license.js)
+// ==========================================================================
+/*globals module test ok isObj equals expects same plan */
+
+var obj;
+
+module("SC.tryToPerform", {
+ setup: function() {
+ obj = SC.Object.create({
+ foo: "bar",
+ total: 12345,
+ aMethodThatExists: function() {},
+ aMethodThatReturnsTrue: function() { return true; },
+ aMethodThatReturnsFoobar: function() { return "Foobar"; },
+ aMethodThatReturnsFalse: function() { return NO; }
+ });
+ },
+
+ teardown: function() {
+ obj = undefined ;
+ }
+
+});
+
+test("Should return false when asked to perform a method it does not have", function() {
+ equals(SC.tryToPerform(obj, 'aMethodThatDoesNotExist'), false);
+});
+
+test("Should pass back the return true if method returned true, false if method not implemented or returned false", function() {
+ equals(SC.tryToPerform(obj, 'aMethodThatReturnsTrue'), true, 'method that returns true');
+ equals(SC.tryToPerform(obj, 'aMethodThatReturnsFoobar'), true, 'method that returns non-false');
+ equals(SC.tryToPerform(obj, 'aMethodThatReturnsFalse'), false, 'method that returns false');
+ equals(SC.tryToPerform(obj, 'imaginaryMethod'), false, 'method that is not implemented');
+});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment