Browse files

Adding storage.

  • Loading branch information...
1 parent 138d7a5 commit 6b373ea53826292e805150620d35cf344a98345f @vesln committed Jan 28, 2012
Showing with 197 additions and 0 deletions.
  1. +91 −0 lib/storage.js
  2. +106 −0 test/storage.test.js
View
91 lib/storage.js
@@ -0,0 +1,91 @@
+/*!
+ * Box - Powerful key -> value storage for the CLI.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+
+/**
+ * Module dependencies.
+ */
+var fs = require('fs');
+var path = require('path');
+
+/**
+ * Storage.
+ *
+ * @type {Object}
+ */
+var storage = module.exports;
+
+/**
+ * Sets a value.
+ *
+ * @param {String} Key.
+ * @param {String} Value.
+ * @param {Function} Callback.
+ * @api public
+ */
+storage.set = function(key, value, cb) {
+ (this.data[key] = value) && cb();
+};
+
+/**
+ * Returns a value.
+ *
+ * @param {String} Key.
+ * @param {Function} Callback.
+ */
+storage.get = function(key, cb) {
+ cb(this.data[key] || null);
+};
+
+/**
+ * Delets a key.
+ *
+ * @param {String} Key.
+ * @param {Function} Callback.
+ * @api public
+ */
+storage.del = function(key, cb) {
+ delete this.data[key] && cb();
+};
+
+/**
+ * Saves file content.
+ *
+ * @param {Function} Callback.
+ */
+storage.save = function(cb) {
+ fs.writeFile(this.source, JSON.stringify(this.data), function(err) {
+ cb(err || null)
+ });
+};
+
+/**
+ * Loads file content.
+ *
+ * @param {String} Path to file.
+ * @param {Function} Callback.
+ * @api public
+ */
+storage.load = function(file, cb) {
+ var self = this;
+ this.source = file;
+ fs.readFile(file, 'utf8', function(err, data) {
+ if (err && err.code !== 'ENOENT') return cb(err);
+ self.data = data || {};
+ cb(null);
+ });
+};
+
+/**
+ * Destroies a storage.
+ *
+ * @param {Function} Callback.
+ */
+storage.destroy = function(cb) {
+ fs.unlink(this.source, function(err) {
+ cb(err || null);
+ });
+};
View
106 test/storage.test.js
@@ -0,0 +1,106 @@
+/*!
+ * Box - Powerful key -> value storage for the CLI.
+ *
+ * Veselin Todorov <hi@vesln.com>
+ * MIT License.
+ */
+
+/**
+ * Test dependencies.
+ */
+var fs = require('fs');
+var path = require('path');
+
+/**
+ * The tested object.
+ *
+ * @type {Object}
+ */
+var storage = require('../lib/storage');
+
+/**
+ * Temporary data file.
+ *
+ * @type {String}
+ */
+var file = path.join(__dirname, 'tmp', 'data.json');
+
+/**
+ * Removes the tmp file.
+ */
+function rm() {
+ try {
+ fs.unlinkSync(file);
+ } catch(err) {
+ if (err.code !== 'ENOENT') throw err;
+ }
+};
+
+describe('storage', function() {
+ beforeEach(rm);
+ afterEach(rm);
+
+ describe('.load()', function() {
+ it('should load file content', function(done) {
+ storage.load(file, function(err) {
+ (err === null).should.be.ok;
+ done();
+ });
+ });
+ });
+
+ describe('.set() \n .get()', function() {
+ it('should access a key value', function(done) {
+ storage.set('foo', 'bar', function() {
+ storage.get('foo', function(val) {
+ val.should.eql('bar');
+ done();
+ })
+ });
+ });
+ });
+
+ describe('.del()', function() {
+ it('should delete a key', function(done) {
+ storage.set('foo', 'bar', function() {
+ storage.del('foo', function() {
+ storage.get('foo', function(val) {
+ (val === null).should.be.true;
+ done();
+ });
+ });
+ });
+ });
+ });
+
+ describe('.save()', function() {
+ it('should save content to file.', function(done) {
+ storage.load(file, function(err) {
+ (err === null).should.be.ok;
+ storage.set('foo', 'bar', function() {
+ storage.save(function(err) {
+ (err === null).should.be.true;
+ fs.readFileSync(file, 'utf8').should.eql('{"foo":"bar"}');
+ done();
+ })
+ });
+ });
+ });
+ });
+
+ describe('.destroy()', function() {
+ it('should destroy the loaded file.', function(done) {
+ storage.load(file, function(err) {
+ storage.set('foo', 'bar', function() {
+ storage.save(function(err) {
+ storage.destroy(function(err) {
+ (err === null).should.be.true;
+ path.existsSync(file).should.be.false;
+ done();
+ });
+ })
+ });
+ });
+ });
+ });
+});

0 comments on commit 6b373ea

Please sign in to comment.