change readers api - involve loaders

This commit is contained in:
oleg 2016-01-10 16:55:57 +03:00
parent 91ab584e08
commit 7f74c9b829
5 changed files with 36 additions and 29 deletions

13
app.js
View File

@ -7,7 +7,9 @@ var env = process.env.NODE_ENV || 'development',
fs = require('fs'),
Steppy = require('twostep').Steppy,
_ = require('underscore'),
reader = require('./lib/reader'),
Reader = require('./lib/reader').Reader,
BaseReaderLoader = require('./lib/reader/loader/base').Loader,
JsonReaderLoader = require('./lib/reader/loader/json').Loader,
notifier = require('./lib/notifier'),
ProjectsCollection = require('./lib/project').ProjectsCollection,
BuildsCollection = require('./lib/build').BuildsCollection,
@ -19,6 +21,9 @@ var env = process.env.NODE_ENV || 'development',
var app = new EventEmitter(),
logger = libLogger('app');
app.reader = new Reader();
app.reader.register('json', JsonReaderLoader);
var httpServerLogger = libLogger('http server');
app.httpServer = httpServer.create();
@ -54,7 +59,7 @@ app.httpServer.addRequestListener(function(req, res, next) {
});
app.lib = {};
app.lib.reader = reader;
app.lib.BaseReaderLoader = BaseReaderLoader;
app.lib.notifier = notifier;
app.lib.logger = libLogger;
@ -174,7 +179,7 @@ Steppy(
});
}
reader.load(app.config.paths.data, 'config', this.slot());
app.reader.load(app.config.paths.data, 'config', this.slot());
},
function(err, mkdirResult, config) {
this.pass(mkdirResult);
@ -201,7 +206,7 @@ Steppy(
function() {
app.projects = new ProjectsCollection({
db: db,
reader: reader,
reader: app.reader,
baseDir: app.config.paths.projects
});

View File

@ -3,20 +3,22 @@
var Steppy = require('twostep').Steppy,
_ = require('underscore'),
fs = require('fs'),
path = require('path'),
BaseReader = require('./base').Reader;
path = require('path');
var constructors = {
json: require('./json').Reader
function Reader() {
this.constructors = {};
}
exports.Reader = Reader;
Reader.prototype.register = function(ext, constructor) {
this.constructors[ext] = constructor;
};
exports.BaseReader = BaseReader;
Reader.prototype.load = function(dir, name, callback) {
var self = this;
exports.register = function(ext, constructor) {
constructors[ext] = constructor;
};
exports.load = function(dir, name, callback) {
Steppy(
function() {
fs.readdir(dir, this.slot());
@ -26,19 +28,19 @@ exports.load = function(dir, name, callback) {
var filePath = _(filePaths).find(function(filePath) {
ext = path.extname(filePath).replace(/^\./, '');
return (
ext in constructors &&
ext in self.constructors &&
path.basename(filePath, '.' + ext) === name
);
});
if (filePath) {
var Constructor = constructors[ext],
var Constructor = self.constructors[ext],
reader = new Constructor();
reader.load(dir, name, this.slot());
} else {
throw new Error(
'Can`t load "' + name + '" from "' + dir + '" using ' +
'readers: ' + _(constructors).keys().join(', ')
'readers: ' + _(self.constructors).keys().join(', ')
);
}
},

View File

@ -2,12 +2,12 @@
var Steppy = require('twostep').Steppy;
function Reader() {
function Loader() {
}
exports.Reader = Reader;
exports.Loader = Loader;
Reader.prototype.load = function(dir, name, callback) {
Loader.prototype.load = function(dir, name, callback) {
var self = this;
Steppy(
function() {
@ -23,5 +23,5 @@ Reader.prototype.load = function(dir, name, callback) {
);
};
Reader.prototype._load = function(dir, name, callback) {
Loader.prototype._load = function(dir, name, callback) {
};

View File

@ -2,22 +2,22 @@
var Steppy = require('twostep').Steppy,
inherits = require('util').inherits,
ParentReader = require('./base').Reader,
ParentLoader = require('./base').Loader,
path = require('path'),
fs = require('fs');
function Reader() {
ParentReader.call(this);
function Loader() {
ParentLoader.call(this);
}
inherits(Reader, ParentReader);
inherits(Loader, ParentLoader);
exports.Reader = Reader;
exports.Loader = Loader;
Reader.prototype.ext = 'json';
Loader.prototype.ext = 'json';
Reader.prototype._load = function(dir, name, callback) {
Loader.prototype._load = function(dir, name, callback) {
var self = this;
Steppy(
function() {

View File

@ -56,7 +56,7 @@
"nci-projects-reloader": "0.1.1",
"nci-rest-api-server": "0.1.1",
"nci-static-server": "0.1.0",
"nci-yaml-reader": "0.1.0",
"nci-yaml-reader": "0.2.0",
"nodemon": "1.3.7",
"nrun": "0.1.4",
"sinon": "1.14.1"