1
0
Fork 0

Foxx: Models can have a Schema now

This commit is contained in:
Lucas Dohmen 2013-09-18 12:53:22 +02:00
parent f7c18c4ddd
commit 53746deca7
2 changed files with 117 additions and 2 deletions

View File

@ -97,6 +97,71 @@ function ModelSpec () {
};
}
function JSONSchema () {
var FoxxModel, TestModel, jsonSchema, instance;
return {
setUp: function () {
FoxxModel = require('org/arangodb/foxx/model').Model;
},
testGetEmptyJSONSchema: function () {
TestModel = FoxxModel.extend({});
jsonSchema = TestModel.toJSONSchema("myname");
assertEqual(jsonSchema.id, "myname");
assertEqual(jsonSchema.required, []);
assertEqual(jsonSchema.properties, {});
},
testAttributesOfAPlainModel: function () {
attributes = {a: 1, b: 2};
TestModel = FoxxModel.extend({});
instance = new TestModel(attributes);
assertEqual(instance.attributes, attributes);
},
testAddOptionalAttributeToJSONSchemaInLongForm: function () {
TestModel = FoxxModel.extend({}, {
attributes: {
x: { type: "string" }
}
});
jsonSchema = TestModel.toJSONSchema("myname");
assertEqual(jsonSchema.id, "myname");
assertEqual(jsonSchema.required, []);
assertEqual(jsonSchema.properties.x.type, "string");
},
testAddOptionalAttributeToJSONSchemaInShortForm: function () {
TestModel = FoxxModel.extend({}, {
attributes: {
x: "string"
}
});
jsonSchema = TestModel.toJSONSchema("myname");
assertEqual(jsonSchema.id, "myname");
assertEqual(jsonSchema.required, []);
assertEqual(jsonSchema.properties.x.type, "string");
},
testAddRequiredAttributeToJSONSchema: function () {
TestModel = FoxxModel.extend({}, {
attributes: {
x: { type: "string", required: true }
}
});
jsonSchema = TestModel.toJSONSchema("myname");
assertEqual(jsonSchema.id, "myname");
assertEqual(jsonSchema.properties.x.type, "string");
assertEqual(jsonSchema.required, ["x"]);
}
};
}
jsunity.run(ModelSpec);
jsunity.run(JSONSchema);
return jsunity.done();

View File

@ -31,7 +31,9 @@
var Model,
_ = require("underscore"),
is = require("org/arangodb/is"),
backbone_helpers = require("backbone");
backbone_helpers = require("backbone"),
parseAttributes,
parseRequiredAttributes;
// -----------------------------------------------------------------------------
// --SECTION-- Model
@ -65,8 +67,56 @@ Model = function (attributes) {
this.attributes = attributes || {};
};
_.extend(Model.prototype, {
parseAttributes = function (rawAttributes) {
'use strict';
var properties = {};
_.each(rawAttributes, function (value, key) {
if (_.isString(value)) {
properties[key] = {
type: value
};
} else {
properties[key] = {
type: value.type
};
}
});
return properties;
};
parseRequiredAttributes = function (rawAttributes) {
'use strict';
var requiredProperties = [];
_.each(rawAttributes, function (value, key) {
if (_.isObject(value) && value.required) {
requiredProperties.push(key);
}
});
return requiredProperties;
};
// "Class" Properties
_.extend(Model, {
// TODO: Docs
toJSONSchema: function (id) {
'use strict';
var attributes = this.attributes;
return {
id: id,
required: parseRequiredAttributes(attributes),
properties: parseAttributes(attributes)
};
},
});
// Instance Properties
_.extend(Model.prototype, {
////////////////////////////////////////////////////////////////////////////////
/// @fn JSF_foxx_model_get
/// @brief Get the value of an attribute