Skip to content

Commit

Permalink
Merged with bcoe/endtable small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
terribletriojoe committed Dec 20, 2011
2 parents efb8ba4 + 88c8989 commit 39acd63
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 111 deletions.
4 changes: 2 additions & 2 deletions README.markdown
Expand Up @@ -54,11 +54,11 @@ var Dog = endtable.Object.extend(
engine: engine,
type: 'dog',
customViews: [
function lowerName(doc) {
function lowerName(doc) {
if(doc.type=='dog')
emit(doc.name.toLowerCase(),doc);
},
function otherNamedView(doc) { ... }
function otherNamedView(doc) { ... }
]
}
);
Expand Down
89 changes: 44 additions & 45 deletions lib/couch-connector.js
Expand Up @@ -104,16 +104,17 @@ exports.connector = Class.extend({
type: '',
keys: []
}

params = arrayHelpers.extend(defaults, {}, params);

// if there's a custom view specified, load by that instead
var viewURL;
if( params.customView ) {
viewURL = this.buildCustomViewURL(params.customView,params.type);
}
else {
viewURL = this.buildViewURL(params.keys, params.type);

// if there's a custom view specified, load by that instead
var viewURL;
if( params.customView ) {
viewURL = this.buildCustomViewURL(params.customView, params.type);
} else {
viewURL = this.buildViewURL(params.keys, params.type);
}

var viewParams = this.buildViewParams({
key: params.key,
startkey: params.startkey,
Expand Down Expand Up @@ -147,24 +148,23 @@ exports.connector = Class.extend({
return '/_design/' + type + '/_view/' + this.buildViewName(keys);
}
},

buildCustomViewURL: function(name, type) {
buildCustomViewURL: function(name, type) {
if (this.legacy) {
return '/_view/' + type + '/' + this.buildCustomViewName(name);
} else {
return '/_design/' + type + '/_view/' + this.buildCustomViewName(name);
}
},
},

buildDocumentName: function(type) {
return '_design/' + type;
},

buildCustomViewName: function(name) {
return 'custom_'+name;
},
buildCustomViewName: function(name) {
return 'custom_'+name;
},


buildViewName: function(keys) {
var viewName = 'by';

Expand All @@ -176,15 +176,16 @@ exports.connector = Class.extend({
},

buildViewParams: function(getParams) {
var i = 0;
var getParamsString = '';
var i = 0,
getParamsString = '';

for (var key in getParams) {
if (getParams.hasOwnProperty(key) && getParams[key]) {

// we omit the custom view keys, if specified
if(key=='customView') {
continue;
}
// we omit the custom view keys, if specified
if(key=='customView') {
continue;
}
if (i) {
getParamsString += '&';
}
Expand Down Expand Up @@ -218,65 +219,63 @@ exports.connector = Class.extend({
if (!doc) {
doc = {views: {}};
}

doc.views[_this.buildViewName(params.keys)] = {
map: 'function(doc) { if (doc.type == \'' + params.type + '\') {' + _this.buildEmitKey(params.keys) + '} }'
};
doc.views[_this.buildViewName(params.keys)] = {
map: 'function(doc) { if (doc.type == \'' + params.type + '\') {' + _this.buildEmitKey(params.keys) + '} }'
};

_this.connection.saveDoc(
documentName,
doc,
function(error, doc) {
callback(error, doc);
_this.synchronousProcessor.next();
_this.synchronousProcessor.next();
}
);
});

}, [documentName, callback]);
},

createCustomView: function(params,callback) {
callback = this.errorHandler.wrapCallback(
createCustomView: function(params,callback) {
var _this = this;

callback = this.errorHandler.wrapCallback(
callback,
'CouchConnector',
'createCustomView',
params
);

if(!params.customView || typeof(params.customView) != 'function'
|| !params.customView.name || params.customView.name =='') {
callback({message:'params.customView must be a named function'},null);
}

var _this = this;
if(!params.customView || typeof(params.customView) != 'function' || !params.customView.name) {
callback({message:'params.customView must be a named function'},null);
}

var documentName = this.buildDocumentName(params.type);
var documentName = this.buildDocumentName(params.type);

this.synchronousProcessor.runSynchronous(_this, function(documentName, callback) {
_this.connection.getDoc(documentName, function(error, doc) {

if (!doc) {
doc = {views: {}};
}

// create custom view
doc.views[_this.buildCustomViewName(params.customView.name)] = {
map: params.customView.toString().replace(/function.*?\(/,'function(')
};
// create custom view
doc.views[_this.buildCustomViewName(params.customView.name)] = {
map: params.customView.toString().replace(/function.*?\(/,'function(')
};

_this.connection.saveDoc(
documentName,
doc,
function(error, doc) {
callback(error, doc);
_this.synchronousProcessor.next();
_this.synchronousProcessor.next();
}
);
});

}, [documentName, callback]);

},
},

buildEmitKey: function(keys) {
var emitKey = 'emit(';
Expand Down
28 changes: 15 additions & 13 deletions lib/endtable-object.js
Expand Up @@ -23,19 +23,21 @@ exports.Object = MonitoredObject.extend({
this.firstSaveCallback = firstSaveCallback;
this.synchronousProcessor = new SynchronousProcessor();
this.Constructor = params.Constructor || exports.Object;

// now create the custom views, if passed in
if(this.customViews && this.customViews.length > 0) {
for(var i=0;i<this.customViews.length;i++) {
this.engine.connector.createCustomView(
{
type: this.params.type,
customView: this.customViews[i]
},
null
);
}
}
this.createCustomViews();
},

createCustomViews: function() {
if(this.customViews) {
for(var i=0, cv; (customView = this.customViews[i]) != null; i++) {
this.engine.connector.createCustomView(
{
type: this.params.type,
customView: cv
},
null
);
}
}
},

load: function(params, callback) {
Expand Down
2 changes: 1 addition & 1 deletion lib/monitored-object.js
Expand Up @@ -15,7 +15,7 @@ var MonitoredObject = Class.extend({
saveRate: 100,
engine: null,
instanceVariables: null,
customViews: null,
customViews: null,
localUID: 0,
params: {},
synchronousProcessor: null,
Expand Down
103 changes: 53 additions & 50 deletions tests/couch-connector-test.js
Expand Up @@ -69,7 +69,7 @@ exports.tests = {
}
c.loadDocument({
keys: ['name'],
customView: 'test_view',
customView: 'test_view',
type: 'person'
})
equal('/_design/person/_view/custom_test_view', lastUrl, prefix + ' proper RESTful url not created.');
Expand Down Expand Up @@ -214,55 +214,58 @@ exports.tests = {
});
}, 50);
},

'should create the specified map function myFn when createCustomView is called with params customView:myFn': function(finished, prefix) {
var c = createMockConnection();

var customView = function custom(doc) {
if(doc.type == 'custom_view_test') {
emit( toLower(doc.id+'_CUSTOM_TEST'), doc);
}
};

var cv_name = customView.name;

c.createCustomView({
customView:customView,
type:'custom_view_test'
});
equal(true, lastDocument.views[c.buildCustomViewName(cv_name)].map.indexOf('_CUSTOM_TEST') > 0, prefix + ' custom map function not properly created.');
finished();
},

'should raise an error when createCustomView is called with param customView:myFn that is not a named function': function(finished, prefix) {
var c = createMockConnection();

var custom_view = function(doc) {
if(doc.type == 'custom_view_test') {
emit( toLower(doc.id+'_CUSTOM_TEST'), doc);
}
};

c.createCustomView({
customView:custom_view,
type:'custom_view_test'
}, function(error,doc) {
equal(true,error.message == 'params.customView must be a named function',prefix + 'setting customView to an anonymous function did not raise an error in createCustomView');
finished();
});
},

'should raise an error when createCustomView is called with param custom_view with value that is not a function': function(finished, prefix) {
var c = createMockConnection();

c.createCustomView({
customView:'not a function',
type:'custom_view_test'
}, function(error,doc) {
equal(true,error.message == 'params.customView must be a named function',prefix + 'setting custom_view to something other than a function did not raise an error in createCustomView');
finished();
});
}

'should create the specified map function myFn when createCustomView is called with params customView:myFn': function(finished, prefix) {
var c = createMockConnection();

var customView = function custom(doc) {
if(doc.type == 'custom_view_test') {
emit( toLower(doc.id+'_CUSTOM_TEST'), doc);
}
};

var cv_name = customView.name;

c.createCustomView({
customView:customView,
type:'custom_view_test'
});

equal(true, lastDocument.views[c.buildCustomViewName(cv_name)].map.indexOf('_CUSTOM_TEST') > 0, prefix + ' custom map function not properly created.');
finished();
},

'should raise an error when createCustomView is called with param customView:myFn that is not a named function': function(finished, prefix) {
var c = createMockConnection();

var custom_view = function(doc) {
if(doc.type == 'custom_view_test') {
emit( toLower(doc.id+'_CUSTOM_TEST'), doc);
}
};

c.createCustomView({
customView:custom_view,
type:'custom_view_test'
}, function(error,doc) {
equal(true,error.message == 'params.customView must be a named function',prefix + 'setting customView to an anonymous function did not raise an error in createCustomView');
finished();
});
},

'should raise an error when createCustomView is called with param custom_view with value that is not a function': function(finished, prefix) {
var c = createMockConnection();

var cv_name = 'lower_docid';

c.createCustomView({
customView:'not a function',
type:'custom_view_test'
}, function(error,doc) {
equal(true,error.message == 'params.customView must be a named function',prefix + 'setting custom_view to something other than a function did not raise an error in createCustomView');
finished();
});
}

};

Expand Down

0 comments on commit 39acd63

Please sign in to comment.