Skip to content

Commit

Permalink
generate *ByIds method for services
Browse files Browse the repository at this point in the history
  • Loading branch information
DracoBlue committed Aug 17, 2011
1 parent 2b79433 commit e70a81a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@ Spludo is copyright 2009-2011 by DracoBlue <http://dracoblue.net>

## 1.1.0-dev

* generate *ByIds method for services
* added ApiServiceController to register a service as REST service
* context.request now holds the current req instance
* added inflection.js
Expand Down
11 changes: 9 additions & 2 deletions build/code-templates/core-code-templates.js
Expand Up @@ -10,6 +10,7 @@ var sys = require("sys");
var fs = require("fs");
var path = require("path");
var child_process = require("child_process");
var inflection = require("inflection");

SpludoGenerator.addCodeTemplate("new-project", {
description: "Create a new plain spludo project.",
Expand Down Expand Up @@ -414,10 +415,16 @@ SpludoGenerator.addCodeTemplate("service", {
});
return result_parts.join('');
};

var service_name = values_object['service_name'];
var service_name_lower_case = service_name.toLowerCase();

values_object['service_name_lower_case'] = values_object['service_name'].toLowerCase();

values_object['service_name_lower_case'] = service_name_lower_case;
values.push(['service_name_lower_case', values_object['service_name_lower_case']]);
values_object['service_name_plural_lower_case'] = inflection.pluralize(service_name_lower_case) || service_name_lower_case;
values.push(['service_name_plural_lower_case', values_object['service_name_plural_lower_case']]);
values_object['service_name_plural'] = inflection.pluralize(service_name) || service_name;
values.push(['service_name_plural', values_object['service_name_plural']]);

var database = database_manager.getDatabase(values_object["database_connection_name"]);

Expand Down
Expand Up @@ -32,6 +32,24 @@ var %%%service_name%%% = function(initial_values) {
});
};

%%%service_name%%%Service.prototype.get%%%service_name_plural%%%ByIds = function(cb, ids) {
assert.equal(typeof cb, 'function', 'First parameter must be a callback!');

var criteria = database_manager.createCriteria();
criteria.andWhereIn('%%%id_key%%%', ids);

this.database_connection.selectTableRows('%%%database_table_name%%%', criteria)(function(error, results) {
var %%%service_name_plural_lower_case%%% = [];

var results_length = results.length;
for (var i = 0; i < results_length; i++) {
%%%service_name_plural_lower_case%%%.push(new %%%service_name%%%(results[i]));
}

cb(%%%service_name_plural_lower_case%%%);
});
};

%%%service_name%%%Service.prototype.update%%%service_name%%% = function(cb, %%%service_name_lower_case%%%, values) {
assert.equal(typeof cb, 'function', 'First parameter must be a callback!');

Expand Down Expand Up @@ -59,4 +77,4 @@ var %%%service_name%%% = function(initial_values) {
/*
* Initialize the Module
*/
module.exports = new %%%service_name%%%Service();
module.exports = new %%%service_name%%%Service();

0 comments on commit e70a81a

Please sign in to comment.