squire-0.2.0.js revision 89092dc10fe08b037266c0b4efb94b221f6fffb3
325N/Adefine(function() {
325N/A
325N/A /**
325N/A * Utility Functions
325N/A */
325N/A
325N/A var toString = Object.prototype.toString;
325N/A
325N/A var isArray = function(arr) {
325N/A return toString.call(arr) === '[object Array]';
325N/A };
325N/A
325N/A var isFunction = function(fn) {
325N/A return toString.call(fn) === '[object Function]';
325N/A };
325N/A
325N/A var indexOf = function(arr, search) {
325N/A for (var i = 0, length = arr.length; i < length; i++) {
325N/A if (arr[i] === search) {
325N/A return i;
325N/A }
325N/A }
325N/A
325N/A return -1;
325N/A };
325N/A
325N/A var each = function(obj, iterator, context) {
325N/A var breaker = {};
325N/A
325N/A if (obj === null) {
325N/A return;
325N/A }
325N/A
325N/A if (Array.prototype.forEach && obj.forEach === Array.prototype.forEach) {
325N/A obj.forEach(iterator, context);
325N/A } else if (obj.length === +obj.length) {
325N/A for (var i = 0, l = obj.length; i < l; i++) {
325N/A if (iterator.call(context, obj[i], i, obj) === breaker){
325N/A return;
325N/A }
325N/A }
325N/A } else {
325N/A for (var key in obj) {
325N/A if (obj.hasOwnProperty(key)) {
325N/A if (iterator.call(context, obj[key], key, obj) === breaker) {
325N/A return;
325N/A }
325N/A }
325N/A }
325N/A }
325N/A };
325N/A
325N/A /**
* Require.js Abstractions
*/
var getContext = function(id) {
return requirejs.s.contexts[id];
};
var undef = function(context, module) {
if (context.undef) {
return context.undef(module);
}
return context.require.undef(module);
};
/**
* Create a context name incrementor.
*/
var idCounter = 0;
var uniqueId = function(prefix) {
var id = idCounter++;
return 'context' + id;
};
var Squire = function() {
this.mocks = {};
this._store = [];
this.requiredCallbacks = [];
this.configure.apply(this, arguments);
};
/**
* Hook to call when the require function is called.
*/
Squire.prototype.onRequired = function(cb) {
this.requiredCallbacks.push(cb);
};
/**
* Configuration of Squire.js, called from constructor or manually takes the
* name of a require.js context to configure it.
*/
Squire.prototype.configure = function(context) {
var configuration = {};
var property;
this.id = uniqueId();
// Default the context
if (typeof context === 'undefined') {
context = '_'; // Default require.js context
}
context = getContext(context);
if ( ! context) {
throw new Error('This context has not been created!');
}
each(context.config, function(property, key) {
if (key !== 'deps') {
configuration[key] = property;
}
});
configuration.context = this.id;
this.load = requirejs.config(configuration);
};
Squire.prototype.mock = function(path, mock) {
var alias;
if (typeof path === 'object') {
each(path, function(alias, key) {
this.mock(key, alias);
}, this);
} else {
if (isFunction(mock)) {
this.mocks[path] = Squire.Helpers.returns(mock);
} else {
this.mocks[path] = mock;
}
}
return this;
};
Squire.prototype.store = function(path) {
if (path && typeof path === 'string') {
this._store.push(path);
} else if(path && isArray(path)) {
each(path, function(pathToStore) {
this.store(pathToStore);
}, this);
}
return this;
};
Squire.prototype.require = function(dependencies, callback, errback) {
var magicModuleName = 'mocks';
var self = this;
var path, magicModuleLocation;
magicModuleLocation = indexOf(dependencies, magicModuleName);
if (magicModuleLocation !== -1) {
dependencies.splice(magicModuleLocation, 1);
}
each(this.mocks, function(mock, path) {
define(path, mock);
});
this.load(dependencies, function() {
var store = {};
var args = Array.prototype.slice.call(arguments);
var dependency;
if (magicModuleLocation !== -1) {
each(self._store, function(dependency) {
store[dependency] = getContext(self.id).defined[dependency];
});
args.splice(magicModuleLocation, 0, {
mocks: self.mocks,
store: store
});
}
callback.apply(null, args);
each(self.requiredCallbacks, function(cb) {
cb.call(null, dependencies, args);
});
}, errback);
};
Squire.prototype.clean = function(mock) {
var path;
if (mock && typeof mock === 'string') {
undef(getContext(this.id), mock);
delete this.mocks[mock];
} else if(mock && isArray(mock)) {
each(mock, function(mockToClean) {
this.clean(mockToClean);
}, this);
} else {
each(this.mocks, function(mock, path){
this.clean(path);
}, this);
}
return this;
};
Squire.prototype.remove = function() {
var path;
each(getContext(this.id).defined, function(dependency, path) {
undef(getContext(this.id), path);
}, this);
delete requirejs.s.contexts[this.id];
};
Squire.prototype.run = function(deps, callback) {
var self = this;
var run = function(done) {
self.require(deps, function() {
callback.apply(null, arguments);
done();
});
};
run.toString = function() {
return callback.toString();
};
return run;
};
/**
* Utilities
*/
Squire.Helpers = {};
Squire.Helpers.returns = function(what) {
return function() {
return what;
};
};
Squire.Helpers.constructs = function(what) {
return function() {
return function() {
return what;
};
};
};
return Squire;
});