Browse files

Convert to defaultable

  • Loading branch information...
1 parent c30d49f commit e445a1035ef31528fe295a4bc923e9d73d1d1e9f @jhs committed Oct 29, 2011
Showing with 74 additions and 35 deletions.
  1. +8 −27 api.js
  2. +11 −3 couch.js
  3. +6 −0 ddoc.js
  4. +13 −0 lib.js
  5. +7 −1 message.js
  6. +29 −4 queue.js
View
35 api.js
@@ -12,14 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+require('defaultable')(module,
+ { 'time_C': 1.0
+ }, function(module, exports, DEFS, require) {
+
var lib = require('./lib')
, util = require('util')
, couch = require('./couch')
, queue = require('./queue')
, message = require('./message')
;
-var API = { 'CreateQueue': queue.create
+module.exports =
+ { 'CreateQueue': queue.create
, 'ListQueues' : queue.list
, 'SetQueueAttributes': queue.set
, 'GetQueueAttributes': queue.get
@@ -32,30 +37,6 @@ var API = { 'CreateQueue': queue.create
, 'Db' : couch.Database
, 'Queue' : queue.Queue
, 'Message': message.Message
- }
-
-
-function with_defaults(opts) {
- var new_api = {};
- Object.keys(API).forEach(function(name) {
- var api_val = API[name];
- new_api[name] = api_val;
- if(typeof api_val !== 'function')
- return;
-
- function default_wrapper(given_opts) {
- given_opts = lib.opt_def(given_opts);
- lib.copy(opts, given_opts, function(k) { return !(k in given_opts) });
- arguments[0] = given_opts;
- return api_val.apply(this, arguments);
- }
-
- default_wrapper.prototype = api_val.prototype;
- new_api[name] = default_wrapper;
- })
-
- return new_api;
-}
+ };
-module.exports = with_defaults({});
-module.exports.defaults = with_defaults;
+}) // defaultable
View
14 couch.js
@@ -12,6 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+var defaultable = require('defaultable');
+
+defaultable(module,
+ { 'uuid_batch_size': 100
+ }, function(module, exports, DEFS, require) {
+
//
// I know maintenance will eventually have to be performed, like purging old documents.
// This is a place where it can go. Maybe in the future you can have dedicated maintenance
@@ -34,7 +40,6 @@ var lib = require('./lib')
var KNOWN_COUCHES = {};
var UUIDS = {}; // Map couch URLs to UUID pools.
-var UUID_BATCH_SIZE = 100;
//
// API
@@ -134,6 +139,7 @@ Couch.prototype.confirmed = function confirm_couch(callback) {
function Database (opts) {
var self = this;
+ opts = defaultable.merge(opts, DEFS);
if(typeof opts.couch !== 'string')
throw new Error('Required "couch" option with URL of CouchDB');
@@ -260,12 +266,12 @@ function uuids_for(couch) {
return;
self.fetching = true;
- self.couch.request('_uuids?count='+UUID_BATCH_SIZE, function(er, resp, result) {
+ self.couch.request('_uuids?count=' + DEFS.uuid_batch_size, function(er, resp, result) {
self.fetching = false;
if(er)
return self.emit('error', er);
- if(!result.uuids || result.uuids.length !== UUID_BATCH_SIZE)
+ if(!result.uuids || result.uuids.length !== DEFS.uuid_batch_size)
return self.emit('error', new Error('Unknown _uuids result: ' + lib.JS(result)));
self.pool = self.pool.concat(result.uuids);
@@ -275,3 +281,5 @@ function uuids_for(couch) {
return getter;
}
+
+}) // defaultable
View
6 ddoc.js
@@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+require('defaultable')(module,
+ {
+ }, function(module, exports, DEFS, require) {
+
var fs = require('fs')
, lib = require('./lib')
, path = require('path')
@@ -381,3 +385,5 @@ function is_list_equal(a, b) {
}
return true;
}
+
+}) // defaultable
View
13 lib.js
@@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+require('defaultable')(module,
+ {
+ }, function(module, exports, DEFS, require) {
+
var request = require('request')
, events = require('events')
, log4js = require('log4js')
@@ -20,6 +24,13 @@ var request = require('request')
exports.log4js = require('log4js');
exports.LOG_LEVEL = process.env.cqs_log_level || "info";
+// A workaround since defaultable seems to be breaking `instanceof` since it re-evaluates modules a lot.
+exports.instanceof = function instance0f(obj, type) {
+ if(typeof type != 'function' || typeof obj != 'object')
+ return false;
+ return !!(obj && obj.constructor && obj.constructor.name === type.name);
+}
+
exports.scrub_creds = function scrub_creds(url) {
return url.replace(/^(https?:\/\/)[^:]+:[^@]+@(.*)$/, '$1$2'); // Scrub username and password
}
@@ -140,3 +151,5 @@ Once.prototype.job = function(task) {
self.pending.emit.apply(self.pending, ['done'].concat(self.result));
}
}
+
+}) // defaultable
View
8 message.js
@@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+require('defaultable')(module,
+ {
+ }, function(module, exports, DEFS, require) {
+
var lib = require('./lib')
, util = require('util')
, couch = require('./couch')
@@ -301,7 +305,7 @@ Message.prototype.import_doc = function(doc) {
function change_visibility(opts, VisibilityTimeout, callback) {
var message;
- if(opts instanceof Message)
+ if(lib.instanceof(opts, Message))
message = opts;
else if(opts.ReceiptHandle)
return callback(new Error('ReceiptHandle is unsupported, use "Message" instead with message object'));
@@ -326,3 +330,5 @@ module.exports = { "Message" : Message
//
// Utilities
//
+
+}) // defaultable
View
33 queue.js
@@ -12,6 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+var defaultable = require('defaultable');
+
+defaultable(module,
+ { 'visibility_timeout' : 30
+ , 'browser_attachments': false
+ }, function(module, exports, DEFS, require) {
+
+
var lib = require('./lib')
, util = require('util')
, couch = require('./couch')
@@ -25,7 +33,6 @@ var lib = require('./lib')
// Constants
//
-var DEFAULT_VISIBILITY_TIMEOUT = 30;
var QUEUE_DDOC_ID_RE = /^_design\/CQS\/([a-zA-Z0-9_-]{1,80})$/;
//
@@ -35,11 +42,16 @@ var QUEUE_DDOC_ID_RE = /^_design\/CQS\/([a-zA-Z0-9_-]{1,80})$/;
function Queue (opts) {
var self = this;
+ if(typeof opts == 'string')
+ opts = {'name':opts};
+
+ opts = defaultable.merge(opts, DEFS);
+
self.name = opts.name || opts.QueueName || opts._str || null;
self.time_C = opts.time_C || null;
self.db = new couch.Database({'couch':opts.couch, 'db':opts.db, time_C:self.time_C});
- self.VisibilityTimeout = opts.DefaultVisibilityTimeout || opts.VisibilityTimeout || DEFAULT_VISIBILITY_TIMEOUT;
+ self.VisibilityTimeout = opts.DefaultVisibilityTimeout || opts.VisibilityTimeout || DEFS.visibility_timeout;
self.cache_confirmation = true;
self.browser_attachments = !!(opts.browser_attachments);
@@ -304,6 +316,14 @@ function create_queue(opts, cb) {
function list_queues(opts, cb) {
+ if(!cb && typeof opts == 'function') {
+ cb = opts;
+ opts = {};
+ }
+
+ if(typeof opts == 'string')
+ opts = {'prefix':opts};
+ opts = defaultable.merge(opts, DEFS);
var prefix = opts.prefix || opts._str;
if(!cb && opts._func)
@@ -359,7 +379,7 @@ function send_message(opts, message, extra, callback) {
if(opts.queue instanceof Queue)
queue = opts.queue
else
- queue = new Queue({'QueueName':opts.queue, 'couch':opts.couch, 'db':opts.db});
+ queue = new Queue({'QueueName':opts.queue, 'couch':opts.couch || DEFS.couch, 'db':opts.db || DEFS.db});
}
else
queue = new Queue(opts);
@@ -373,14 +393,17 @@ function send_message(opts, message, extra, callback) {
}
function receive_message(opts, callback, extra) {
+ if(typeof opts == 'string')
+ opts = {'queue':opts};
+
var queue;
if(opts instanceof Queue)
queue = opts;
else if(opts._str || opts.queue) {
if(opts.queue instanceof Queue)
queue = opts.queue;
else
- queue = new Queue({'QueueName':opts._str || opts.queue, 'couch':opts.couch, 'db':opts.db});
+ queue = new Queue({'QueueName':opts._str || opts.queue, 'couch':opts.couch || DEFS.couch, 'db':opts.db || DEFS.db});
delete opts.queue;
delete opts._str;
} else
@@ -415,3 +438,5 @@ function id_to_name(id) {
throw new Error("Unknown queue ddoc id: " + id);
return match[1];
}
+
+}) // defaultable

0 comments on commit e445a10

Please sign in to comment.