Skip to content

Commit

Permalink
Merge pull request chriso#8 from chase/master
Browse files Browse the repository at this point in the history
Allows Redback to use a previously created Redis client.
  • Loading branch information
chriso committed Jul 10, 2011
2 parents e12c4bc + d28bebb commit fd822da
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
6 changes: 6 additions & 0 deletions README.md 100755 → 100644
Expand Up @@ -28,7 +28,13 @@ It also comes with the following advanced data structures:

```javascript
var redback = require('redback').createClient();
```

or

```javascript
var redis = require('redis').createClient();
var redback = new request('redback').use(redis);
var user3 = redback.createSocialGraph(3);
user3.follow(1, callback);

Expand Down
27 changes: 21 additions & 6 deletions lib/Redback.js
Expand Up @@ -30,18 +30,21 @@ var advanced = ['KeyPair','DensitySet','CappedList','SocialGraph',
* The Redback object wraps the Redis client and acts as a factory
* for structures.
*
* @param {RedisClient} client
* @param {Object} options (optional)
* or
* @param {string} host (optional)
* @param {int} port (optional)
* @param {Object} options (optional)
* @api public
*/

var Redback = exports.Redback = function (host, port, options) {
this.client = redis.createClient.apply(this, arguments);
this.namespace = '';
if (typeof options === 'object') {
this.namespace = options.namespace || '';
}
var Redback = exports.Redback = function () {
this.client = arguments[0] instanceof redis.RedisClient ?
arguments[0] : redis.createClient.apply(this, arguments);
var options = arguments[1] instanceof Object ?
arguments[1] : arguments[2];
this.namespace = options.namespace || '';
}

/**
Expand Down Expand Up @@ -141,6 +144,18 @@ exports.createClient = function (host, port, options) {
return new Redback(host, port, options);
}

/**
* Wrap a Redis client with Redback.
*
* @param {RedisClient} client
* @param {Object} options (optional)
* @api public
*/

exports.use = function (client, options) {
return new Redback(client, options);
}

/**
* Add the Redis structures from ./base_structures
*/
Expand Down
30 changes: 24 additions & 6 deletions site/api.html
Expand Up @@ -514,15 +514,19 @@ <h2></h2>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>RedisClient</em> client</p></li><li><p><strong>param</strong>: <em>Object</em> options (optional)</p></li></ul>

<p>or</p>

<ul><li><p><strong>param</strong>: <em>string</em> host (optional)</p></li><li><p><strong>param</strong>: <em>int</em> port (optional)</p></li><li><p><strong>param</strong>: <em>Object</em> options (optional)</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Redback</span> = <span class="variable">exports</span>.<span class="class">Redback</span> = <span class="keyword">function</span> (<span class="variable">host</span>, <span class="variable">port</span>, <span class="variable">options</span>) {
<span class="this">this</span>.<span class="variable">client</span> = <span class="variable">redis</span>.<span class="variable">createClient</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);
<span class="this">this</span>.<span class="variable">namespace</span> = <span class="string">''</span>;
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">options</span> === <span class="string">'object'</span>) {
<span class="this">this</span>.<span class="variable">namespace</span> = <span class="variable">options</span>.<span class="variable">namespace</span> || <span class="string">''</span>;
}
<pre><code><span class="keyword">var</span> <span class="class">Redback</span> = <span class="variable">exports</span>.<span class="class">Redback</span> = <span class="keyword">function</span> () {
<span class="this">this</span>.<span class="variable">client</span> = <span class="variable">arguments</span>[<span class="number integer">0</span>] <span class="variable">instanceof</span> <span class="variable">redis</span>.<span class="class">RedisClient</span> ?
<span class="variable">arguments</span>[<span class="number integer">0</span>] : <span class="variable">redis</span>.<span class="variable">createClient</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);
<span class="keyword">var</span> <span class="variable">options</span> = <span class="variable">arguments</span>[<span class="number integer">1</span>] <span class="variable">instanceof</span> <span class="class">Object</span> ?
<span class="variable">arguments</span>[<span class="number integer">1</span>] : <span class="variable">arguments</span>[<span class="number integer">2</span>];
<span class="this">this</span>.<span class="variable">namespace</span> = <span class="variable">options</span>.<span class="variable">namespace</span> || <span class="string">''</span>;
}</code></pre>
</td>
</tr>
Expand Down Expand Up @@ -613,6 +617,20 @@ <h2></h2>
</tr>
<tr class="code">
<td class="docs">
<p>Wrap a Redis client with Redback.</p>

<h2></h2>

<ul><li><p><strong>param</strong>: <em>RedisClient</em> client</p></li><li><p><strong>param</strong>: <em>Object</em> options (optional)</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="variable">use</span> = <span class="keyword">function</span> (<span class="variable">client</span>, <span class="variable">options</span>) {
<span class="keyword">return</span> <span class="keyword">new</span> <span class="class">Redback</span>(<span class="variable">client</span>, <span class="variable">options</span>);
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Add the Redis structures from ./base_structures
</p>
</td>
Expand Down

0 comments on commit fd822da

Please sign in to comment.