diff --git a/.gitignore b/.gitignore index b4604dc68..57585e9ad 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,9 @@ pids *.pid *.seed +# Notes +notes.md + # Directory for instrumented libs generated by jscoverage/JSCover lib-cov diff --git a/lib/helpers/model-helper.js b/lib/helpers/model-helper.js index bccd15f52..fd343dbe1 100644 --- a/lib/helpers/model-helper.js +++ b/lib/helpers/model-helper.js @@ -4,6 +4,7 @@ var clc = require('cli-color'); var helpers = require(__dirname); module.exports = { + notifyAboutExistingFile: function (file) { helpers.view.error( 'The file ' + clc.blueBright(file) + ' already exists. ' + @@ -14,23 +15,85 @@ module.exports = { transformAttributes: function (flag) { /* possible flag formats: - - first_name:string,last_name:string,bio:text - 'first_name:string last_name:string bio:text' - 'first_name:string, last_name:string, bio:text' + + proposed flag format: + - name:[type:string,required:true] email:[type:string,required:true] + - name:string email:[type:string,required:true], first_name:string, last_name:string + + deprecated flag format: + - first_name:string,last_name:string,bio:text */ - var set = flag.replace(/,/g, ' ').split(/\s+/); - var result = {}; + var self = this; + var copy = flag.slice(); + var arr = copy.split(' '); + + var stringObj = arr.map(function (str) { + var newStr = str.replace(/\[/g, '{'); + var result = newStr.replace(/\]/g, '}'); - set.forEach(function (pair) { - var split = pair.split(':'); + if (result[ result.length - 1 ] === ',') { + var tempArr = result.split(''); - result[split[0]] = split[1]; + tempArr.splice(result.length - 1, 1); + result = tempArr.join(''); + } + + return result; }); + var result = stringObj.reduce(function (prev, curr) { + var obj = self.objectifyString(curr); + + prev[obj[0]] = obj[1]; + return prev; + }, {}); + return result; }, + objectifyString: function (str) { + var splitPoint = str.indexOf(':'); + var field = str.slice(0, splitPoint); + + if (str.indexOf('{') === -1) { + var typeValue = str.split(':')[1]; + + return [field, {type: typeValue}]; + } + + var start = str.indexOf('{'); + var end = str.indexOf('}'); + var preObj = str.slice(start + 1, end); + var attrArr = preObj.split(','); + + var pairs = attrArr.map(function (str) { + return str.split(':'); + }); + + var attrObj = pairs.reduce(function (prev, curr) { + prev[curr[0]] = curr[1]; + return prev; + }, {}); + + this.convertValueToType(attrObj); + return [field, attrObj]; + }, + + convertValueToType: function (obj) { + return Object.keys(obj).forEach(function (attr) { + var value = obj[attr]; + + if (value === 'true' || value === 'false') { + obj[attr] = Boolean(value); + } else if (parseInt(value)) { + obj[attr] = parseInt(value); + } + }); + }, + generateFileContent: function (args) { return helpers.template.render('models/model.js', { name: args.name, diff --git a/test/support/tmp/.gitkeep b/test/support/tmp/.gitkeep deleted file mode 100644 index e69de29bb..000000000