Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Export RedisClient prototype to support extending.

See examples/extend.js for a simple example.
Remove command list from exports while I was in there.
  • Loading branch information...
commit 85bd648adc4428340f7cec69d629a2e4d6e079aa 1 parent ddcecc5
@mranney authored
Showing with 31 additions and 5 deletions.
  1. +24 −0 examples/extend.js
  2. +6 −4 index.js
  3. +1 −1  package.json
View
24 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();
+});
View
10 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);
View
2  package.json
@@ -1,5 +1,5 @@
{ "name" : "redis",
- "version" : "0.3.3",
+ "version" : "0.3.4",
"description" : "Redis client library",
"author": "Matt Ranney <mjr@ranney.com>",
"contributors": [
Please sign in to comment.
Something went wrong with that request. Please try again.