json-api-schema
Version:
JSON Api Schema is a JSON dialect that can describe any Web Based API that uses JSON to exchange data.
250 lines (223 loc) • 7.26 kB
JavaScript
var ArrayIterator, Entity, EntityIterator, IteratorsIterator, NullIterator, ObjectIterator, null_iterator,
__slice = [].slice;
EntityIterator = require("./iterators/entity_iterator");
ObjectIterator = require("./iterators/object_iterator");
ArrayIterator = require("./iterators/array_iterator");
NullIterator = require("./iterators/null_iterator");
IteratorsIterator = require("./iterators/iterators_iterator");
null_iterator = new NullIterator();
Entity = (function() {
Entity.extend = function(obj) {
var key, value, _ref;
for (key in obj) {
value = obj[key];
if (key !== 'extended' && key !== 'included') {
this[key] = value;
}
}
if ((_ref = obj.extended) != null) {
_ref.apply(this);
}
return this;
};
Entity.include = function(obj) {
var key, value, _ref;
for (key in obj) {
value = obj[key];
if (key !== 'extended' && key !== 'included') {
this.prototype[key] = value;
}
}
if ((_ref = obj.included) != null) {
_ref.apply(this);
}
return this;
};
Entity.accessors = function() {
var camelize, properties, propertyToMethod, propertyToSingularizedMethod, proto, singularize, templates, _i;
properties = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), templates = arguments[_i++];
camelize = function(string) {
return string.replace(/(?:^|[-_])(\w)/g, function(_, c) {
if (c) {
return c.toUpperCase();
} else {
return "";
}
});
};
singularize = function(string) {
return string.replace(/s$/, "");
};
propertyToMethod = function(prefix, property) {
return prefix + camelize(property.replace(/^_+/, ""));
};
propertyToSingularizedMethod = function(prefix, property) {
return singularize(propertyToMethod(prefix, property));
};
proto = this.prototype;
return templates.forEach(function(template) {
switch (template) {
case "get":
return properties.forEach(function(prop) {
var methodName;
methodName = propertyToMethod("get", prop);
return proto[methodName] = function() {
return this[prop];
};
});
case "set":
return properties.forEach(function(prop) {
var methodName;
methodName = propertyToMethod("set", prop);
return proto[methodName] = function(value) {
if (value instanceof Entity) {
value.setParent(this);
}
return this[prop] = value;
};
});
case "get-key":
return properties.forEach(function(prop) {
var methodName;
methodName = propertyToSingularizedMethod("get", prop);
return proto[methodName] = function(k) {
var m;
m = this[prop] || {};
return m[k];
};
});
case "put-key":
return properties.forEach(function(prop) {
var methodName;
methodName = propertyToSingularizedMethod("put", prop);
return proto[methodName] = function(k, v) {
var m;
if (v instanceof Entity) {
v.setParent(this);
}
m = this[prop] != null ? this[prop] : this[prop] = {};
return m[k] = v;
};
});
case "delete-key":
return properties.forEach(function(prop) {
var methodName;
methodName = propertyToSingularizedMethod("delete", prop);
return proto[methodName] = function(k) {
var m, v;
m = this[prop];
v = m[k];
if (v instanceof Entity) {
v.setParent(void 0);
}
delete m[k];
return v;
};
});
case "put-keys":
return properties.forEach(function(prop) {
var methodName;
methodName = propertyToMethod("put", prop);
return proto[methodName] = function(obj) {
var k, m, v;
for (k in obj) {
v = obj[k];
if (v instanceof Entity) {
v.setParent(this);
}
m = this[prop];
m[k] = v;
}
return prop;
};
});
}
});
};
Entity.children = function() {
var children, isArray, iteratorFor;
children = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
isArray = function(array) {
return !(!array || (!array.length || array.length === 0) || typeof array !== "object" || !array.constructor || array.nodeType || array.item);
};
iteratorFor = function(object) {
if (object instanceof Entity) {
return new EntityIterator(object);
} else if (isArray(object)) {
return new ArrayIterator(object);
} else if (typeof object === "object") {
return new ObjectIterator(object);
} else {
return null_iterator;
}
};
return this.prototype.getChildrenIterator = function() {
var child, subIterators, _i, _len;
if (children.length === 0) {
return null_iterator;
} else if (children.length === 1) {
return iteratorFor(this[children[0]]);
} else {
subIterators = [];
for (_i = 0, _len = children.length; _i < _len; _i++) {
child = children[_i];
if (this[child]) {
subIterators.push(iteratorFor(this[child]));
}
}
return new IteratorsIterator(subIterators);
}
};
};
Entity.accessors("title", "description", ["get", "set"]);
Entity.accessors("annotations", ["get-key", "put-key", "put-keys", "delete-key"]);
Entity.accessors("locals", ["get-key", "put-key", "put-keys", "delete-key"]);
Entity.entity = function(name) {
this.prototype.getEntityType = function() {
return name;
};
return this.prototype.isInstanceOfEntity = function(type) {
return type === name || this.__super__.isInstanceOfEntity.call(this, name);
};
};
function Entity(options) {
var k, v;
for (k in options) {
v = options[k];
this[k] = v;
}
}
Entity.prototype.getEntityType = function() {
return "Entity";
};
Entity.prototype.isInstanceOfEntity = function(type) {
return type === "Entity";
};
Entity.prototype.getParent = function() {
return this.parent;
};
Entity.prototype.setParent = function(parent) {
return this.parent = parent;
};
Entity.prototype.isRoot = function() {
return !!this.parent;
};
Entity.prototype.getAncestors = function() {
var ancestors, e, parent;
ancestors = [];
e = this;
while (parent = e.getParent()) {
ancestors.unshift(parent);
e = parent;
}
return ancestors;
};
Entity.prototype.getChildrenIterator = function() {
return null_iterator;
};
Entity.prototype.isLeaf = function() {
return !this.getChildrenIterator().hasNext();
};
return Entity;
})();
module.exports = Entity;