[api] Add .saveSync() and .loadSync() methods to File store

This commit is contained in:
indexzero 2011-04-20 01:57:56 -04:00
parent b9951b44c1
commit d65922dc7a
3 changed files with 77 additions and 1 deletions

View file

@ -69,6 +69,16 @@ nconf.clear = function (key, callback) {
// Responds with an Object representing all keys associated in this instance. // Responds with an Object representing all keys associated in this instance.
// //
nconf.load = function (callback) { nconf.load = function (callback) {
//
// If we don't have a callback and the current
// store is capable of loading synchronously
// then do so.
//
if (!callback && nconf.store.loadSync) {
return nconf.store.loadSync();
}
if (!nconf.store.load) { if (!nconf.store.load) {
var error = new Error('nconf store ' + nconf.store.type + ' has no load() method'); var error = new Error('nconf store ' + nconf.store.type + ' has no load() method');
if (callback) { if (callback) {
@ -92,6 +102,15 @@ nconf.save = function (value, callback) {
if (!callback) { if (!callback) {
callback = value; callback = value;
value = null; value = null;
//
// If we still don't have a callback and the
// current store is capable of saving synchronously
// then do so.
//
if (!callback && nconf.store.saveSync) {
return nconf.store.saveSync();
}
} }
if (!nconf.store.save) { if (!nconf.store.save) {

View file

@ -53,6 +53,36 @@ File.prototype.save = function (value, callback) {
}); });
}; };
//
// ### function saveSync (value, callback)
// #### @value {Object} _Ignored_ Left here for consistency
// #### @callback {function} **Optional** Continuation to respond to when complete.
// Saves the current configuration object to disk at `this.file`
// using the format specified by `this.format` synchronously.
//
File.prototype.saveSync = function (value, callback) {
if (!callback) {
callback = value;
value = null;
}
var err;
try {
fs.writeFileSync(this.file, this.format.stringify(this.store));
}
catch (ex) {
err = ex;
}
if (callback) {
return callback(err);
}
if (err) {
throw err;
}
};
// //
// ### function load (callback) // ### function load (callback)
// #### @callback {function} Continuation to respond to when complete. // #### @callback {function} Continuation to respond to when complete.
@ -69,4 +99,31 @@ File.prototype.load = function (callback) {
self.store = data; self.store = data;
callback(null, self.store); callback(null, self.store);
}); });
};
//
// ### function load (callback)
// #### @callback {function} **Optional** Continuation to respond to when complete.
// Attempts to load the data stored in `this.file` synchronously and responds appropriately.
//
File.prototype.loadSync = function (callback) {
var err, data;
try {
data = fs.readFileSync(this.file, 'utf8');
this.store = this.format.parse(data);
}
catch (ex) {
err = ex;
}
if (callback) {
return callback(err, data);
}
if (err) {
throw err;
}
return data;
}; };

View file

@ -38,7 +38,7 @@ vows.describe('nconf/stores/file').addBatch({
topic: function () { topic: function () {
var tmpPath = path.join(__dirname, 'fixtures', 'tmp.json'), var tmpPath = path.join(__dirname, 'fixtures', 'tmp.json'),
tmpStore = new nconf.stores.File({ file: tmpPath }); tmpStore = new nconf.stores.File({ file: tmpPath });
return tmpStore return tmpStore;
}, },
"the save() method": { "the save() method": {
topic: function (tmpStore) { topic: function (tmpStore) {