From 85bd648adc4428340f7cec69d629a2e4d6e079aa Mon Sep 17 00:00:00 2001 From: Matt Ranney Date: Tue, 5 Oct 2010 11:29:22 -0700 Subject: [PATCH] Export RedisClient prototype to support extending. See examples/extend.js for a simple example. Remove command list from exports while I was in there. --- examples/extend.js | 24 ++++++++++++++++++++++++ index.js | 10 ++++++---- package.json | 2 +- 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 examples/extend.js diff --git a/examples/extend.js b/examples/extend.js new file mode 100644 index 0000000000..488b8c2dc5 --- /dev/null +++ b/examples/extend.js @@ -0,0 +1,24 @@ +var redis = require("redis"), + client = redis.createClient(); + +// Extend the RedisClient prototype to add a custom method +// This one converts the results from "INFO" into a JavaScript Object + +redis.RedisClient.prototype.parse_info = function (callback) { + this.info(function (err, res) { + var lines = res.toString().split("\r\n").sort(); + var obj = {}; + lines.forEach(function (line) { + var parts = line.split(':'); + if (parts[1]) { + obj[parts[0]] = parts[1]; + } + }); + callback(obj) + }); +}; + +client.parse_info(function (info) { + console.dir(info); + client.quit(); +}); diff --git a/index.js b/index.js index cee834db24..541035be95 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,8 @@ var net = require("net"), sys = require("sys"), events = require("events"), default_port = 6379, - default_host = "127.0.0.1"; + default_host = "127.0.0.1", + commands; exports.debug_mode = false; @@ -425,6 +426,7 @@ function RedisClient(stream) { events.EventEmitter.call(this); } sys.inherits(RedisClient, events.EventEmitter); +exports.RedisClient = RedisClient; RedisClient.prototype.connection_gone = function (why) { var self = this; @@ -663,7 +665,7 @@ RedisClient.prototype.end = function () { }; // http://code.google.com/p/redis/wiki/CommandReference -exports.commands = [ +commands = [ // Connection handling "QUIT", "AUTH", // Commands operating on all value types @@ -693,7 +695,7 @@ exports.commands = [ "PING", ]; -exports.commands.forEach(function (command) { +commands.forEach(function (command) { RedisClient.prototype[command] = function () { var args = to_array(arguments); args.unshift(command); // put command at the beginning @@ -710,7 +712,7 @@ function Multi(client, args) { } } -exports.commands.forEach(function (command) { +commands.forEach(function (command) { Multi.prototype[command.toLowerCase()] = function () { var args = to_array(arguments); args.unshift(command); diff --git a/package.json b/package.json index 798f5f9869..0ce62ca58e 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { "name" : "redis", - "version" : "0.3.3", + "version" : "0.3.4", "description" : "Redis client library", "author": "Matt Ranney ", "contributors": [