Skip to content

Commit

Permalink
jslinted (Crockford is such a crude guy!)
Browse files Browse the repository at this point in the history
Change-Id: I118ce7ed257e68bcd4e53e15674cd89718c705ae
  • Loading branch information
mourner committed Aug 12, 2011
1 parent 9ca6ceb commit a231bd8
Showing 1 changed file with 40 additions and 35 deletions.
75 changes: 40 additions & 35 deletions crude.js
Expand Up @@ -4,24 +4,25 @@
* See https://github.com/CloudMade/Crude for more information.
*/

(function(global, undefined) {
(function (global, undefined) {
"use strict";

var Crude = {},
oldCrude = global.Crude;
oldCrude = global.Crude;

if (typeof module != 'undefined' && module.exports) {
module.exports = Crude;
} else {
global['Crude'] = Crude;
}

Crude.noConflict = function() {
Crude.noConflict = function () {
global['Crude'] = oldCrude;
return this;
};


Crude.inherit = function(Child, Parent) {
Crude.inherit = function (Child, Parent) {
function F() {}
F.prototype = Parent.prototype;

Expand All @@ -31,19 +32,19 @@
};


Crude.api = function(baseUrl, format, requestFn) {
Crude.api = function (baseUrl, format, requestFn) {
return new Crude._Api(baseUrl, format, requestFn);
};


Crude._Api = function(baseUrl, format, requestFn) {
Crude._Api = function (baseUrl, format, requestFn) {
this._baseUrl = baseUrl;
this._format = format;
this._requestFn = requestFn;
};

Crude._Api.prototype = {
request: function(path, method, data) {
request: function (path, method, data) {
if (!data && typeof method != 'string') {
data = method;
method = 'get';
Expand All @@ -52,7 +53,7 @@

var url = this._baseUrl + '/' + path + '.' + this._format;

url = url.replace(/{ *([^} ]+) *}/g, function(a, key){
url = url.replace(/\{ *([^} ]+) *\}/g, function (a, key) {
var value = data[key];
if (value === undefined) {
throw new Error('No value provided for variable: ' + key);
Expand All @@ -64,58 +65,58 @@
return this._requestFn(url, method, data);
},

resources: function(name, pluralName) {
resources: function (name, pluralName) {
pluralName = pluralName || Crude.pluralize(name);
var resources = this[pluralName] = new Crude._Resources(this, name, pluralName);
return resources;
}
};


Crude._Resources = function(api, name, pluralName, prefix) {
Crude._Resources = function (api, name, pluralName, prefix) {
this._api = api;
this._name = name;
this._pluralName = pluralName;
this._prefix = prefix;
};

Crude._Resources.prototype = {
request: function(path, method, data) {
request: function (path, method, data) {
var prefix = (this._prefix ? this._prefix + '/' : ''),
postfix = (path ? '/' + path : '');
postfix = (path ? '/' + path : '');

return this._api.request(prefix + this._pluralName + postfix, method, data);
},

get: function(id, data) {
get: function (id, data) {
if (!data && typeof id == 'object') {
data = id;
id = null;
}
return this.request(id || '', 'get', data);
},

create: function(props, data) {
var props = Crude.wrapKeys(props, this._name),
data = Crude.extend({}, data, props);
create: function (props, data) {
props = Crude.wrapKeys(props, this._name);
data = Crude.extend({}, data, props);

return this.request('', 'post', data);
},

update: function(id, props, data) {
var props = Crude.wrapKeys(props, this._name),
data = Crude.extend({}, data, props);
update: function (id, props, data) {
props = Crude.wrapKeys(props, this._name);
data = Crude.extend({}, data, props);

return this.request(id, 'put', data);
},

del: function(id, data) {
del: function (id, data) {
return this.request(id, 'delete', data);
},

belongTo: function(parent) {
belongTo: function (parent) {
var methodName = 'in' + Crude.capitalize(parent._name);
this[methodName] = function(id) {
this[methodName] = function (id) {
function NestedResources() {
Crude._NestedResources.apply(this, arguments);
}
Expand All @@ -130,21 +131,21 @@
return this;
},

memberAction: function(name, options) {
memberAction: function (name, options) {
// options: path, method, argsToDataFn
// TODO member action
return this;
},

collectionAction: function(name, options) {
collectionAction: function (name, options) {
// TODO collection action
return this;
}
};


// create a Resources-inherited class to allow extending nested resources globally
Crude._NestedResources = function() {
Crude._NestedResources = function () {
Crude._Resources.apply(this, arguments);
};
Crude.inherit(Crude._NestedResources, Crude._Resources);
Expand All @@ -157,9 +158,10 @@
[/(x|ch|s|sh)$/i, '$1es'],
['child', 'children']];

Crude.pluralize = function(name) {
Crude.pluralize = function (name) {
var rules = Crude.pluralRules,
i = rules.length, rule;
i = rules.length,
rule;

while (i--) {
rule = rules[i];
Expand All @@ -176,15 +178,18 @@
return name;
};

Crude.capitalize = function(str) {
Crude.capitalize = function (str) {
return str.charAt(0).toUpperCase() + str.slice(1);
};

Crude.extend = function(dest) {
var sources = Array.prototype.slice.call(arguments, 1);
for (var j = 0, len = sources.length, src; j < len; j++) {
Crude.extend = function (dest) {
var sources = Array.prototype.slice.call(arguments, 1),
len = sources.length,
src, i, j;

for (j = 0; j < len; j++) {
src = sources[j] || {};
for (var i in src) {
for (i in src) {
if (src.hasOwnProperty(i)) {
dest[i] = src[i];
}
Expand All @@ -193,9 +198,9 @@
return dest;
};

Crude.wrapKeys = function(props, name) {
var obj = {};
for (var i in props) {
Crude.wrapKeys = function (props, name) {
var obj = {}, i;
for (i in props) {
if (props.hasOwnProperty(i)) {
obj[name + '[' + i + ']'] = props[i];
}
Expand Down

0 comments on commit a231bd8

Please sign in to comment.