Skip to content

Commit

Permalink
[api] Add .saveSync() and .loadSync() methods to File store
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Apr 20, 2011
1 parent b9951b4 commit d65922d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/nconf.js
Expand Up @@ -69,6 +69,16 @@ nconf.clear = function (key, callback) {
// Responds with an Object representing all keys associated in this instance.
//
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) {
var error = new Error('nconf store ' + nconf.store.type + ' has no load() method');
if (callback) {
Expand All @@ -92,6 +102,15 @@ nconf.save = function (value, callback) {
if (!callback) {
callback = value;
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) {
Expand Down
57 changes: 57 additions & 0 deletions lib/nconf/stores/file.js
Expand Up @@ -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)
// #### @callback {function} Continuation to respond to when complete.
Expand All @@ -69,4 +99,31 @@ File.prototype.load = function (callback) {
self.store = data;
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;
};
2 changes: 1 addition & 1 deletion test/file-store-test.js
Expand Up @@ -38,7 +38,7 @@ vows.describe('nconf/stores/file').addBatch({
topic: function () {
var tmpPath = path.join(__dirname, 'fixtures', 'tmp.json'),
tmpStore = new nconf.stores.File({ file: tmpPath });
return tmpStore
return tmpStore;
},
"the save() method": {
topic: function (tmpStore) {
Expand Down

0 comments on commit d65922d

Please sign in to comment.