Skip to content

Commit

Permalink
v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-orlik committed Apr 28, 2015
2 parents 6d71387 + 0a7c894 commit 75664ee
Show file tree
Hide file tree
Showing 26 changed files with 283 additions and 412 deletions.
3 changes: 1 addition & 2 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ function SealiousApp(path_to_package_json, mode, layer_name){

this.mode = mode || "local";
this.layer_name = layer_name || null;

//ModuleManager = new ModuleManager();
//ConfigManager = new ConfigManager();
this.ChipManager = require("./chip-types/chip-manager");
Expand Down Expand Up @@ -80,11 +79,11 @@ SealiousApp.prototype = new function(){
}else{
var dispatcher_path = "./dispatchers/" + layer_name + "/dispatcher-" + layer_name + ".js";
}
console.log("dispatcher_path =", dispatcher_path);
return require(dispatcher_path);
}

this.start = function(){
Sealious.Dispatcher = this.dispatcher;
this.dispatcher.init();
this.dispatcher.start();

Expand Down
2 changes: 0 additions & 2 deletions lib/chip-types/association-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ var AssociationInterface = new function(){
var from_type = ResourceTypeManager.getByName(from_type_name);
var to_type = ResourceTypeManager.getByName(to_type_name);

//var field_type_ltr = new FieldTypeManager.getFieldTypeConstructorByName("association")(from_type_name, to_type_name);

var from_field_options = {
type: "association",
required: options.left_required || false
Expand Down
8 changes: 8 additions & 0 deletions lib/chip-types/chip-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ var ChipManager = new function(){
action_handler_function(chip_instance, dispatcher, dependencies);
}

this.chip_exists = function(type, name){
if(chips[type] && chips[type][name]){
return true;
}else{
return false;
}
}

this.get_chip = function(type, name){
try{
return chips[type][name]
Expand Down
2 changes: 1 addition & 1 deletion lib/chip-types/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Datastore.prototype = new Chip();

Datastore.prototype.return_not_implemented = function(fn_name){
return function(){
throw new Error("Function ", fn_name, "not implemented in ", this.longid, ", aborting.");
throw new Sealious.Errors.DeveloperError("Function ", fn_name, "not implemented in ", this.longid, ", aborting.");
}
}

Expand Down
12 changes: 9 additions & 3 deletions lib/chip-types/field-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var merge = require("merge");
* Stores field type metadata, as well as validation methods
* @class
*/
function FieldType(longid){
function FieldType(longid, declaration){
this.longid = longid;
}

Expand All @@ -23,19 +23,25 @@ FieldType.prototype = new function(){
});
}

this.validate_declaration = function(declaration){
return new Promise(function(resolve, reject){
resolve();
});
}

this.encode = function(value_in_code){
return new Promise(function(resolve, reject){
resolve(value_in_code);
})
}

this.decode = function(value_in_database){
this.decode = function(value_in_database, formatting_options){
return new Promise(function(resolve, reject){
resolve(value_in_database);
})
}

this.setParams = function(param_map){
this.set_params = function(param_map){
if(this.params==undefined){
this.params = param_map;
}
Expand Down
94 changes: 37 additions & 57 deletions lib/chip-types/resource-manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var Promise = require("bluebird");

var ResourceRepresentation = require("./resource-representation.js");
var ChipManager = require("./chip-manager.js");

var UUIDGenerator = require("uid");

/**
* Manages resources in the database
Expand All @@ -10,27 +10,13 @@ var ChipManager = require("./chip-manager.js");
var ResourceManager = new function(){

this.create = function(dispatcher, type_name, body, owner, access_mode, access_mode_args){
console.log("resource-manager.js:create-resource");
if(arguments.length==3){
var owner=null;
var access_mode = "private";
var access_mode_args = [];
} else if(arguments.length==4){
var access_mode = "private";
} else if(arguments.length==5){
var access_mode = "private";
var access_mode_args = [];
} else if(arguments.length==6) {

} else {
throw new Sealious.Errors.ValidationError("Wrong amount of arguments: create_resource."); //~
}

if(!ChipManager.chip_is_registred("resource_type." + type_name)){
throw new Sealious.Errors.ValidationError("Unknown resource type: " + type_name); //~
if(owner===undefined) owner=null;
if(access_mode===undefined) access_mode = "private";
if(access_mode_args==undefined) access_mode_args = [];
if(!Sealious.ChipManager.chip_exists("resource_type", type_name)){
throw new Sealious.Errors.ValidationError("Unknown resource type: " + type_name);
}

var resource_type_object = ChipManager.get_chip("resource_type", type_name);
var resource_type_object = Sealious.ChipManager.get_chip("resource_type", type_name);

var encoded_body = null;

Expand All @@ -39,14 +25,10 @@ var ChipManager = require("./chip-manager.js");
.then(
function(){
return resource_type_object.encode_field_values(body);
}
).then(
}).then(
function(response){
encoded_body = response;
return dispatcher.metadata_increment_variable("first_free_id", dispatcher)
}
).then(
function(newID) {
var newID = UUIDGenerator(10);
var resource_data = {
sealious_id: newID,
type: type_name,
Expand Down Expand Up @@ -77,7 +59,6 @@ var ChipManager = require("./chip-manager.js");
}

this.get_resource_access_mode = function(dispatcher, resource_id){
resource_id = parseInt(resource_id);
return new Promise(function(resolve,reject){
dispatcher.datastore.find("resources", { sealious_id: resource_id }, {})
.then(function(documents){
Expand All @@ -89,13 +70,13 @@ var ChipManager = require("./chip-manager.js");
});
}

this.delete = function(dispatcher, type_name, body){
if(!ChipManager.chip_is_registred("resource_type." + type_name)){
throw new Sealious.Errors.ValidationError("Unknown resource type: " + type_name); //~
this.delete = function(dispatcher, type_name, id){
if(!Sealious.ChipManager.chip_is_registred("resource_type." + type_name)){
throw new Sealious.Errors.ValidationError("Unknown resource type: " + type_name);
}

return new Promise(function(resolve, reject){
dispatcher.datastore.delete("resources", {sealious_id: body.id, type: type_name}, {})
dispatcher.datastore.delete("resources", {sealious_id: id, type: type_name}, {})
.then(function(data){
resolve();
}).catch(function(e){
Expand All @@ -112,7 +93,6 @@ var ChipManager = require("./chip-manager.js");
*/

this.get_by_id = function(dispatcher, resource_id){
resource_id = parseInt(resource_id);
return new Promise(function(resolve,reject){
dispatcher.datastore.find("resources", { sealious_id: resource_id }, {})
.then(function(documents){
Expand All @@ -132,15 +112,15 @@ var ChipManager = require("./chip-manager.js");
params = {};
}
return new Promise(function(resolve, reject){
if(!ChipManager.chip_is_registred("resource_type."+type_name)){
if(!Sealious.ChipManager.chip_exists("resource_type", type_name)){
reject(new Sealious.Errors.ValidationError("resource type "+type_name+" does not exist"));
}else{
dispatcher.datastore.find("resources", { type: type_name }, {}, params).then(function(response) {
var ret = response.map(function(database_entry){
return new ResourceRepresentation(database_entry).getData();
})
resolve(ret);
})
resolve(ret);
})
}
})
}
Expand Down Expand Up @@ -196,28 +176,28 @@ var ChipManager = require("./chip-manager.js");

this.search_by_mode = function(dispatcher, type, mode){
return new Promise(function(resolve, reject){
dispatcher.datastore.find("resources", {access_mode: mode, type: type}, {})
.then(function(documents){
var database_entry = documents;
var resource_representations = documents.map(function(document){
return new ResourceRepresentation(document).getData()
});
resolve(resource_representations);
});
dispatcher.datastore.find("resources", {access_mode: mode, type: type}, {})
.then(function(documents){
var database_entry = documents;
var resource_representations = documents.map(function(document){
return new ResourceRepresentation(document).getData()
});
resolve(resource_representations);
});
});
}
}

this.get_resource_type_signature = function(dispatcher, type_name){
return new Promise(function(resolve, reject){
var resource_type_chip = ChipManager.get_chip("resource_type", type_name);
if(resource_type_chip){
resolve(resource_type_chip.get_signature());
}else{
reject(new Sealious.Errors.Error("ResourceManager tried to access chip type `" + type_name + "`, which does not exist."));
}
})
this.get_resource_type_signature = function(dispatcher, type_name){
return new Promise(function(resolve, reject){
var resource_type_chip = ChipManager.get_chip("resource_type", type_name);
if(resource_type_chip){
resolve(resource_type_chip.get_signature());
}else{
reject(new Sealious.Errors.Error("ResourceManager tried to access chip type `" + type_name + "`, which does not exist."));
}
})
}
}
}


module.exports = ResourceManager;
module.exports = ResourceManager;
6 changes: 1 addition & 5 deletions lib/chip-types/resource-representation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
*/
function ResourceRepresentation(database_entry){
this.database_entry = database_entry;
if(!database_entry.body){
//console.log("database_entry missing:", database_entry.body);
}
//console.log("DATABASE_ENTRY: ", database_entry);
}

ResourceRepresentation.prototype = new function(){
Expand All @@ -19,10 +15,10 @@ ResourceRepresentation.prototype = new function(){
*/
this.getData = function(){
var data = this.database_entry.body || {};
//console.log(data===null);
data.owner_id = this.database_entry.owner;
data.id = this.database_entry.sealious_id;
data.access_mode = this.database_entry.access_mode;
data.type = this.database_entry.type;

return data;
}
Expand Down
13 changes: 6 additions & 7 deletions lib/chip-types/resource-type-field.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
var Promise = require("bluebird");

/**
* A field in a resource type
* @class
* @param {string} name
* @param {object} options
*/
function ResourceTypeField(declaration){
this.name = declaration.name;
this.type_name = declaration.type;
this.human_readable_name = declaration.human_readable_name || null;
var type_constructor = Sealious.ChipManager.get_chip("field_type", declaration.type);
this.type_parameters = declaration.params;
this.type = new type_constructor();
this.type.setParams(declaration.params);
this.type.set_params(declaration.params);
this.required = declaration.required || false;
this.derived = declaration.derived || false;
}
Expand All @@ -31,10 +25,15 @@ ResourceTypeField.prototype = new function(){
that.type.isProperValue(value, dispatcher).then(
resolve,
function(err){
/*
//older version, not compliant with #132
var new_error = {
field_name: that.name,
error_message: err
}
*/
var new_error={};
new_error[that.name] = err;
reject(new_error);
}
);
Expand Down
Loading

0 comments on commit 75664ee

Please sign in to comment.