Skip to content

Commit

Permalink
Merge pull request #106 from CleverStack/1.2.0
Browse files Browse the repository at this point in the history
Cleanup for 1.2
  • Loading branch information
pilsy committed Jan 28, 2015
2 parents 144d8d5 + ad2776e commit 4e3dc48
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 180 deletions.
100 changes: 30 additions & 70 deletions lib/classes/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,27 @@ module.exports = Controller.extend(
},
/* @Prototype */
{
processIncludes: function( options ) {
if ( !!this.req.query._include ) {
options.include = [];

this.req.query._include.split( ',' ).forEach( function( include ) {
include = include.split( '|' );
var queryInclude = { model: models[ include[ 0 ] ] };
if ( include.length > 1 ) {
queryInclude.as = include[ 1 ];
}
options.include.push( queryInclude );
});
}
},

listAction: function() {
if ( this.Class.service !== null && this.Class.service.model !== undefined ) {
var query = this.req.query
, options = { where: _.omit( query, '_include' ) }
, includes = query._include || false;

if ( !!includes ) {
options.include = [];
includes.split( ',' ).forEach( function( include ) {
include = include.split( '|' );
var queryInclude = { model: models[ include[ 0 ] ] };
if ( include.length > 1 ) {
queryInclude.as = include[ 1 ];
}
options.include.push( queryInclude );
});
}
, options = { where: _.omit( query, '_include' ) };

this.processIncludes( options );

return this.Class
.service
Expand All @@ -43,20 +47,9 @@ module.exports = Controller.extend(
getAction: function() {
if ( this.Class.service !== null && this.Class.service.model !== undefined ) {
var query = this.req.query
, options = { where: _.omit( query, '_include' ) }
, includes = query._include || false;

if ( !!includes ) {
options.include = [];
includes.split( ',' ).forEach( function( include ) {
include = include.split( '|' );
var queryInclude = { model: models[ include[ 0 ] ] };
if ( include.length > 1 ) {
queryInclude.as = include[ 1 ];
}
options.include.push( queryInclude );
});
}
, options = { where: _.omit( query, '_include' ) };

this.processIncludes( options );

options.where.id = this.req.params.id || this.req.query.id;

Expand Down Expand Up @@ -84,20 +77,9 @@ module.exports = Controller.extend(
return this.putAction();
} else if ( this.Class.service !== null && this.Class.service.model !== undefined ) {
var query = this.req.query
, options = { where: _.omit( query, '_include' ) }
, includes = query._include || false;

if ( !!includes ) {
options.include = [];
includes.split( ',' ).forEach( function( include ) {
include = include.split( '|' );
var queryInclude = { model: models[ include[ 0 ] ] };
if ( include.length > 1 ) {
queryInclude.as = include[ 1 ];
}
options.include.push( queryInclude );
});
}
, options = { where: _.omit( query, '_include' ) };

this.processIncludes( options );

return this.Class
.service
Expand All @@ -112,20 +94,9 @@ module.exports = Controller.extend(
putAction: function() {
if ( this.Class.service !== null && this.Class.service.model !== undefined ) {
var query = this.req.query
, options = { where: _.omit( query, '_include' ) }
, includes = query._include || false;

if ( !!includes ) {
options.include = [];
includes.split( ',' ).forEach( function( include ) {
include = include.split( '|' );
var queryInclude = { model: models[ include[ 0 ] ] };
if ( include.length > 1 ) {
queryInclude.as = include[ 1 ];
}
options.include.push( queryInclude );
});
}
, options = { where: _.omit( query, '_include' ) };

this.processIncludes( options );

options.where.id = this.req.params.id || this.req.query.id;

Expand All @@ -142,20 +113,9 @@ module.exports = Controller.extend(
deleteAction: function() {
if ( this.Class.service !== null && this.Class.service.model !== undefined ) {
var query = this.req.query
, options = { where: _.omit( query, '_include' ) }
, includes = query._include || false;

if ( !!includes ) {
options.include = [];
includes.split( ',' ).forEach( function( include ) {
include = include.split( '|' );
var queryInclude = { model: models[ include[ 0 ] ] };
if ( include.length > 1 ) {
queryInclude.as = include[ 1 ];
}
options.include.push( queryInclude );
});
}
, options = { where: _.omit( query, '_include' ) };

this.processIncludes( options );

options.where.id = this.req.params.id || this.req.query.id;

Expand Down
108 changes: 10 additions & 98 deletions lib/classes/Model.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
var injector = require( 'injector' )
, Exceptions = require( 'exceptions' )
, Promise = require( 'bluebird' )
, path = require( 'path' )
, async = require( 'async' )
, util = require( 'util' )
, utils = require( 'utils' )
, debuggr = require( 'debug' )( 'Models' )
, debuggr = require( 'debug' )( 'cleverstack:models' )
, _ = require( 'underscore' )
, inflect = require( 'i' )()
, Class = injector.getInstance( 'Class' )
, moduleLdr = injector.getInstance( 'moduleLoader' )
, validator = utils.modelValidator
Expand Down Expand Up @@ -134,13 +132,8 @@ var Model = Class.extend(
extendingArgs = [ Static, Proto ];

if ( !modelName ) {

var Reg = new RegExp( '.*\\(([^\\)]+)\\:.*\\:.*\\)', 'ig' )
, stack = new Error().stack.split( '\n' )
, file = stack.splice( 2, 1 );

if ( Reg.test( file ) ) {
modelName = RegExp.$1.split( path.sep ).pop().replace( '.js', '' );
if ( ( modelName = utils.helpers.getClassName( 4 ) ) !== false ) {
modelName = modelName.replace( 'Model', '' );
} else {
throw new Error( 'Unable to determine model name.' );
}
Expand Down Expand Up @@ -186,7 +179,7 @@ var Model = Class.extend(

debug( 'Defining schema...' );
Static._schema = {};
Object.keys( Proto ).forEach( this.callback( 'getSchemaFromProto', Proto, Static ) );
Object.keys( Proto ).forEach( this.callback( utils.modelUtils.getSchemaFromProto, Proto, Static ) );

debug( 'Defining models this.debug() helper...' );
Proto.debug = Static.debug = function( msg ) {
Expand Down Expand Up @@ -233,53 +226,6 @@ var Model = Class.extend(
return model;
},

// Private function used to build _schema so it can be passed to the _driver for schema creation
getSchemaFromProto: function( Proto, Static, key ) {
var prop = Proto[ key ]
, columnName = !!this.underscored ? inflect.underscore( key ) : key;

if ( !!prop.columnName && key !== prop.columnName ) {
Static._customColumnNames.push( { key: key, columnName: prop.columnName } );
} else if ( !!Static.underscored && key !== columnName ) {
Static._customColumnNames.push( { key: key, columnName: columnName } );
}

if ( typeof prop === 'function' && [ String, Number, Boolean, Date, Buffer, Model.Types.ENUM, Model.Types.TINYINT, Model.Types.BIGINT, Model.Types.FLOAT, Model.Types.DECIMAL, Model.Types.TEXT ].indexOf( Proto[ key ] ) === -1 && key !== 'defaults') {

} else if ( key !== 'defaults' ) {

if ( typeof Static._schema !== 'object' ) {
Static._schema = {};
}

if ( typeof Static._getters !== 'object' ) {
Static._getters = {};
}

if ( typeof Static._setters !== 'object' ) {
Static._setters = {};
}

Static._schema[ key ] = prop;
Static._getters[ key ] = function() {
if ( key === 'id' && Static.type.toLowerCase() === 'odm' ) {
return this._model._id;
} else {
return this._model[ !!prop.columnName ? prop.columnName : columnName ];
}
};
Static._setters[ key ] = function( val ) {
this._dirty = true;
this._model[ !!prop.columnName ? prop.columnName : columnName ] = val;
this._changed.push( key );

return this;
};

delete Proto[ key ];
}
},

// @TODO refactor this id / idOrWhere bullshit (findOptions...)
find: function( id, options ) {
var modelType = this.type.toUpperCase ? this.type.toUpperCase() : this.type
Expand All @@ -289,6 +235,8 @@ var Model = Class.extend(

options = options || {};

utils.modelUtils.renameCustomColumnsForQuery.apply( this, [ findOptions ] );

return new Promise(function( resolve, reject ) {

// Configure findOptions
Expand All @@ -310,18 +258,6 @@ var Model = Class.extend(
delete findOptions.where.id;
}

if ( findOptions.where && that._customColumnNames.length > 0 ) {
Object.keys( findOptions.where ).forEach( function( key ) {
var val = findOptions.where[ key ]
, newKey = _.findWhere( that._customColumnNames, { key: key } );

if ( newKey ) {
findOptions.where[ newKey.columnName ] = val;
delete findOptions.where[ key ];
}
});
}

// Make sure we have either an id or findOptions to find by models with
if ( !!isModel && !id && !findOptions ) {
return reject( new Exceptions.InvalidData( [ 'You must specify either an id or an object containing fields to find a', that._name ].join( ' ' ) ) );
Expand Down Expand Up @@ -416,17 +352,7 @@ var Model = Class.extend(
delete findOptions.where.id;
}

if ( findOptions.where && that._customColumnNames.length > 0 ) {
Object.keys( findOptions.where ).forEach( function( key ) {
var val = findOptions.where[ key ]
, newKey = _.findWhere( that._customColumnNames, { key: key } );

if ( newKey ) {
findOptions.where[ newKey.columnName ] = val;
delete findOptions.where[ key ];
}
});
}
utils.modelUtils.renameCustomColumnsForQuery.apply( this, [ findOptions ] );

return new Promise(function( resolve, reject ) {
async.waterfall(
Expand Down Expand Up @@ -505,17 +431,8 @@ var Model = Class.extend(
},

function handleCustomColumnNames( callback ) {
if ( that._customColumnNames.length ) {
that._customColumnNames.forEach( function( column ) {
if ( data[ column.key ] ) {
data[ column.columnName ] = data[ column.key ];
delete data[ column.key ];
}
});
callback( null );
} else {
callback( null );
}
utils.modelUtils.renameCustomColumnsForQuery.apply( that, [ data ] );
callback( null );
},

function createModel( callback ) {
Expand Down Expand Up @@ -762,12 +679,7 @@ var Model = Class.extend(
}
});

this.Class._customColumnNames.forEach( function( columnName ) {
if ( json[ columnName.columnName ] !== undefined ) {
json[ columnName.key ] = json[ columnName.columnName ];
delete json[ columnName.columnName ];
}
});
utils.modelUtils.renameCustomColumnsForOutput.apply( that, [ json ] );

return json;
},
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ exports.extend = function() {

Proto._camelName = i.camelize( moduleName.replace( /\-/ig, '_' ), false );
moduleDebug( 'Creating debugger with name ' + Proto._camelName + '...' );
Proto.debug = require( 'debug' )( Proto._camelName );
Proto.debug = require( 'debug' )( 'cleverstack:' + Proto._camelName );

moduleDebug( 'Creating module class...' );
var Klass = Module.callback( 'extend' )( Static, Proto )
Expand Down
11 changes: 3 additions & 8 deletions lib/classes/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var injector = require( 'injector' )
, Promise = injector.getInstance( 'Promise' )
, path = require( 'path' )
, util = require( 'util' )
, debug = require( 'debug' )( 'Services' )
, utils = require( 'utils' )
, debug = require( 'debug' )( 'cleverstack:services' )
, Model = injector.getInstance( 'Model' )
, services = {}
, Service;
Expand Down Expand Up @@ -250,13 +251,7 @@ exports.extend = function() {
, Proto = extendingArgs.shift();

if ( !serviceName ) {
var Reg = new RegExp( '.*\\(([^\\)]+)\\:.*\\:.*\\)', 'ig' )
, stack = new Error().stack.split( '\n' )
, file = stack.splice( 2, 1 );

if ( Reg.test( file ) ) {
serviceName = RegExp.$1.split( path.sep ).pop().replace( '.js', '' );
} else {
if ( ( serviceName = utils.helpers.getClassName( 3 ) ) === false ) {
throw new Error( 'Unable to determine services location and name.' );
}
}
Expand Down
8 changes: 8 additions & 0 deletions lib/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,13 @@ module.exports = {

}
});
},

getClassName: function( offset ) {
var Reg = new RegExp( '.*\\(([^\\)]+)\\:.*\\:.*\\)', 'ig' )
, stack = new Error().stack.split( '\n' )
, file = stack.splice( offset, 1 );

return Reg.test( file ) ? RegExp.$1.split( path.sep ).pop().replace( '.js', '' ) : false;
}
}
Loading

0 comments on commit 4e3dc48

Please sign in to comment.