├── .gitignore ├── Gemfile ├── Gemfile.lock ├── README.md ├── Rakefile ├── generators └── license.js ├── lib ├── main.js ├── responds_to.js └── try_to_perform.js ├── package.json └── tests ├── responds_to_test.js └── try_to_perform_test.js /.gitignore: -------------------------------------------------------------------------------- 1 | *.bpkg 2 | *.gem 3 | *.rbc 4 | .DS_Store 5 | .bpm 6 | .bundle 7 | .config 8 | .yardoc 9 | InstalledFiles 10 | _site 11 | _yardoc 12 | assets 13 | assets/bpm_libs.js 14 | assets/bpm_styles.css 15 | coverage 16 | dist 17 | doc/ 18 | docs/jsdoc 19 | docs/output 20 | lib/*/tests/all.js 21 | lib/*/tests/qunit* 22 | lib/bundler/man 23 | pkg 24 | rdoc 25 | spade-boot.js 26 | spec/reports 27 | test/tmp 28 | test/version_tmp 29 | test_*.html 30 | tmp 31 | tmp*.gem 32 | tmp.bpm 33 | tmp.spade 34 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | gem "sproutcore", :git => "https://github.com/wycats/abbot-from-scratch.git" 4 | gem "uglifier" 5 | gem "execjs" 6 | gem "multi_json" 7 | gem "rake" -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GIT 2 | remote: https://github.com/wycats/abbot-from-scratch.git 3 | revision: 29dfaa6c3c120847e61f742f74c414e4872cc142 4 | specs: 5 | sproutcore (0.0.1) 6 | 7 | GEM 8 | remote: http://rubygems.org/ 9 | specs: 10 | execjs (1.2.6) 11 | multi_json (~> 1.0) 12 | multi_json (1.0.3) 13 | rake (0.9.2) 14 | uglifier (1.0.3) 15 | execjs (>= 0.3.0) 16 | multi_json (>= 1.0.2) 17 | 18 | PLATFORMS 19 | ruby 20 | 21 | DEPENDENCIES 22 | execjs 23 | multi_json 24 | rake 25 | sproutcore! 26 | uglifier 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SproutCore Utils 2 | 3 | ### Description: 4 | 5 | Some utility functions that are used by other packages in SproutCore 2.0. 6 | 7 | ### Problems: 8 | 9 | If you run into a problem, please file an issue on this repository. 10 | 11 | ### Synopsis: 12 | 13 | TODO: Upload documentation somewhere 14 | 15 | For the time being, there are only a few functions and it should be easy to 16 | grok their use from the source code. 17 | 18 | ### Requirements: 19 | 20 | In order to use the Utils package, you'll need: 21 | 22 | * [SproutCore 2.0's runtime library](https://github.com/sproutcore/sproutcore20) 23 | 24 | ### Install: 25 | 26 | You can use the Utils package by either downloading the JavaScript files 27 | provided in the [Downloads section](https://github.com/sproutcore/sproutcore-utils/archives/master) 28 | of this repository or using [BPM](http://getbpm.org/). To install it via BPM, simply run 29 | 30 | bpm add sproutcore-utils 31 | 32 | And this will handle adding all of its dependencies as well. You, of course, need to 33 | be using BPM for your application or package for this to work ;) 34 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/setup" 2 | require "erb" 3 | require "uglifier" 4 | require "sproutcore" 5 | 6 | LICENSE = File.read("generators/license.js") 7 | 8 | module SproutCore 9 | module Compiler 10 | class Entry 11 | def body 12 | "\n(function(exports) {\n#{@raw_body}\n})({});\n" 13 | end 14 | end 15 | end 16 | end 17 | 18 | def strip_require(file) 19 | result = File.read(file) 20 | result.gsub!(%r{^\s*require\(['"]([^'"])*['"]\);?\s*$}, "") 21 | result 22 | end 23 | 24 | def strip_sc_assert(file) 25 | result = File.read(file) 26 | result.gsub!(%r{^(\s)+sc_assert\((.*)\).*$}, "") 27 | result 28 | end 29 | 30 | def uglify(file) 31 | uglified = Uglifier.compile(File.read(file)) 32 | "#{LICENSE}\n#{uglified}" 33 | end 34 | 35 | SproutCore::Compiler.output = "tmp/static" 36 | SproutCore::Compiler.intermediate = "tmp/static" 37 | 38 | def compile_utils_task 39 | SproutCore::Compiler.intermediate = "tmp/sproutcore-utils" 40 | js_tasks = SproutCore::Compiler::Preprocessors::JavaScriptTask.with_input "lib/**/*.js", "." 41 | SproutCore::Compiler::CombineTask.with_tasks js_tasks, "#{SproutCore::Compiler.intermediate}/sproutcore-utils" 42 | end 43 | 44 | task :compile_utils_task => compile_utils_task 45 | 46 | task :build => [:compile_utils_task] 47 | 48 | file "dist/sproutcore-utils.js" => :build do 49 | puts "Generating sproutcore-utils.js" 50 | 51 | mkdir_p "dist" 52 | 53 | File.open("dist/sproutcore-utils.js", "w") do |file| 54 | file.puts strip_require("tmp/static/sproutcore-utils.js") 55 | end 56 | end 57 | 58 | # Minify dist/sproutcore-utils.js to dist/sproutcore-utils.min.js 59 | file "dist/sproutcore-utils.min.js" => "dist/sproutcore-utils.js" do 60 | puts "Generating sproutcore-utils.min.js" 61 | 62 | File.open("dist/sproutcore-utils.prod.js", "w") do |file| 63 | file.puts strip_sc_assert("dist/sproutcore-utils.js") 64 | end 65 | 66 | File.open("dist/sproutcore-utils.min.js", "w") do |file| 67 | file.puts uglify("dist/sproutcore-utils.prod.js") 68 | end 69 | rm "dist/sproutcore-utils.prod.js" 70 | end 71 | 72 | desc "Build SproutCore Utilities" 73 | task :dist => ["dist/sproutcore-utils.min.js"] 74 | 75 | task :clean do 76 | sh "rm -rf tmp && rm -rf dist" 77 | end 78 | 79 | task :default => :dist -------------------------------------------------------------------------------- /generators/license.js: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // Project: SproutCore - JavaScript Application Framework 3 | // Copyright: ©2006-2011 Strobe Inc. and contributors. 4 | // Portions ©2008-2011 Apple Inc. All rights reserved. 5 | // License: Licensed under MIT license (see license.js) 6 | // ========================================================================== 7 | -------------------------------------------------------------------------------- /lib/main.js: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // Project: SproutCore 3 | // Copyright: ©2006-2011 Strobe Inc. and contributors. 4 | // Portions ©2008-2011 Apple Inc. All rights reserved. 5 | // License: Licensed under MIT license (see license.js) 6 | // ========================================================================== 7 | 8 | require('ember-runtime'); 9 | require('sproutcore-utils/responds_to'); 10 | require('sproutcore-utils/try_to_perform'); 11 | -------------------------------------------------------------------------------- /lib/responds_to.js: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // Project: SproutCore 3 | // Copyright: ©2006-2011 Strobe Inc. and contributors. 4 | // Portions ©2008-2011 Apple Inc. All rights reserved. 5 | // License: Licensed under MIT license (see license.js) 6 | // ========================================================================== 7 | 8 | var slice = Array.prototype.slice; 9 | 10 | var respondsTo = function(obj, methodName) { 11 | return !!(obj[methodName] instanceof Function); 12 | }; 13 | 14 | /** 15 | @param {Object} obj The object to check for the method 16 | @param {String} methodName The method name to check for 17 | */ 18 | SC.respondsTo = respondsTo; 19 | 20 | SC.Object.reopen( 21 | /** @scope SC.Object.prototype */{ 22 | 23 | respondsTo: function() { 24 | var args = slice.call(arguments); 25 | args.unshift(this); 26 | return SC.respondsTo.apply(SC, args); 27 | } 28 | 29 | }); -------------------------------------------------------------------------------- /lib/try_to_perform.js: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // Project: SproutCore 3 | // Copyright: ©2006-2011 Strobe Inc. and contributors. 4 | // Portions ©2008-2011 Apple Inc. All rights reserved. 5 | // License: Licensed under MIT license (see license.js) 6 | // ========================================================================== 7 | 8 | require('sproutcore-utils/responds_to'); 9 | 10 | var slice = Array.prototype.slice; 11 | 12 | var tryToPerform = function(obj, methodName) { 13 | var args = slice.call(arguments); 14 | args.shift(); args.shift(); 15 | return SC.respondsTo(obj, methodName) && (obj[methodName].apply(obj, args) !== false); 16 | }; 17 | 18 | /** 19 | Checks to see if the `methodName` exists on the `obj`, 20 | and if it does, invokes it with the arguments passed. 21 | 22 | @function 23 | 24 | @param {Object} obj The object to check for the method 25 | @param {String} methodName The method name to check for 26 | @param {Object...} args The arguments to pass to the method 27 | 28 | @returns {Boolean} true if the method does not return false 29 | @returns {Boolean} false otherwise 30 | */ 31 | SC.tryToPerform = tryToPerform; 32 | 33 | SC.Object.reopen( 34 | /** @scope SC.Object.prototype */{ 35 | 36 | tryToPerform: function() { 37 | var args = slice.call(arguments); 38 | args.unshift(this); 39 | return SC.tryToPerform.apply(SC, args); 40 | } 41 | 42 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sproutcore-utils", 3 | "summary": "SproutCore Utilities", 4 | "description": "A collection of utility functions for SproutCore", 5 | "homepage": "http://sproutcore.com", 6 | "author": "Strobe Inc., Apple, Inc., and contributors", 7 | "version": "2.0.beta.2", 8 | "bpm": "1.0.0", 9 | "directories": { 10 | "lib": "lib" 11 | }, 12 | "dependencies": { 13 | "spade": "~> 1.0", 14 | "ember-runtime": ">= 0" 15 | }, 16 | "dependencies:development": { 17 | "spade-qunit": "~> 1.0.0" 18 | }, 19 | "bpm:build": { 20 | "bpm_libs.js": { 21 | "directories": [ 22 | "lib" 23 | ], 24 | "modes": "*" 25 | }, 26 | "sproutcore-utils/bpm_tests.js": { 27 | "directories": [ 28 | "tests" 29 | ], 30 | "modes": [ 31 | "debug" 32 | ] 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/responds_to_test.js: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // Project: SproutCore 3 | // Copyright: ©2006-2011 Strobe Inc. and contributors. 4 | // Portions ©2008-2011 Apple Inc. All rights reserved. 5 | // License: Licensed under MIT license (see license.js) 6 | // ========================================================================== 7 | /*globals module test ok isObj equals expects same plan */ 8 | 9 | var obj; 10 | 11 | module("SC.respondsTo", { 12 | setup: function() { 13 | obj = SC.Object.create({ 14 | foo: "bar", 15 | total: 12345, 16 | aMethodThatExists: function() {}, 17 | aMethodThatReturnsTrue: function() { return true; }, 18 | aMethodThatReturnsFoobar: function() { return "Foobar"; }, 19 | aMethodThatReturnsFalse: function() { return NO; } 20 | }); 21 | }, 22 | 23 | teardown: function() { 24 | obj = undefined ; 25 | } 26 | 27 | }); 28 | 29 | test("Checks if methods exist using the 'SC.respondsTo' method", function() { 30 | equals(SC.respondsTo(obj, 'aMethodThatExists'), true); 31 | equals(SC.respondsTo(obj, 'aMethodThatDoesNotExist'), false); 32 | }); 33 | 34 | 35 | test("Checks if methods exist using the 'obj.respondsTo' method", function() { 36 | equals(obj.respondsTo('aMethodThatExists'), true); 37 | equals(obj.respondsTo('aMethodThatDoesNotExist'), false); 38 | }); 39 | -------------------------------------------------------------------------------- /tests/try_to_perform_test.js: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // Project: SproutCore 3 | // Copyright: ©2006-2011 Strobe Inc. and contributors. 4 | // Portions ©2008-2011 Apple Inc. All rights reserved. 5 | // License: Licensed under MIT license (see license.js) 6 | // ========================================================================== 7 | /*globals module test ok isObj equals expects same plan */ 8 | 9 | var obj; 10 | 11 | module("SC.tryToPerform", { 12 | setup: function() { 13 | obj = SC.Object.create({ 14 | foo: "bar", 15 | total: 12345, 16 | aMethodThatExists: function() {}, 17 | aMethodThatReturnsTrue: function() { return true; }, 18 | aMethodThatReturnsFoobar: function() { return "Foobar"; }, 19 | aMethodThatReturnsFalse: function() { return NO; } 20 | }); 21 | }, 22 | 23 | teardown: function() { 24 | obj = undefined ; 25 | } 26 | 27 | }); 28 | 29 | test("Should return false when asked to perform a method it does not have", function() { 30 | equals(SC.tryToPerform(obj, 'aMethodThatDoesNotExist'), false); 31 | equals(obj.tryToPerform('aMethodThatDoesNotExist'), false); 32 | }); 33 | 34 | test("SC.tryToPerform -- Should pass back the return true if method returned true, false if method not implemented or returned false", function() { 35 | equals(SC.tryToPerform(obj, 'aMethodThatReturnsTrue'), true, 'method that returns true'); 36 | equals(SC.tryToPerform(obj, 'aMethodThatReturnsFoobar'), true, 'method that returns non-false'); 37 | equals(SC.tryToPerform(obj, 'aMethodThatReturnsFalse'), false, 'method that returns false'); 38 | equals(SC.tryToPerform(obj, 'imaginaryMethod'), false, 'method that is not implemented'); 39 | }); 40 | 41 | test("obj.tryToPerform -- Should pass back the return true if method returned true, false if method not implemented or returned false", function() { 42 | equals(obj.tryToPerform('aMethodThatReturnsTrue'), true, 'method that returns true'); 43 | equals(obj.tryToPerform('aMethodThatReturnsFoobar'), true, 'method that returns non-false'); 44 | equals(obj.tryToPerform('aMethodThatReturnsFalse'), false, 'method that returns false'); 45 | equals(obj.tryToPerform('imaginaryMethod'), false, 'method that is not implemented'); 46 | }); 47 | --------------------------------------------------------------------------------