不要怂,就是干,撸起袖子干!

Commit 20bc8b0b by Sascha Depold

helper is now a class

1 parent dc5e2609
exports.Helper = function(Sequelize) {
var Helper = {
log: function(obj) {
var sys = require("sys")
sys.log(sys.inspect(obj))
},
evaluateTemplate: function(template, replacements) {
var result = template
Helper.Hash.keys(replacements).forEach(function(key) {
result = result.replace("%{" + key + "}", replacements[key])
})
return result
},
SQL: {
var injectSQL = function(instance) {
instance.SQL = {
isManyToManyAssociation: function(association) {
return (['hasMany', 'hasAndBelongsToMany'].indexOf(association.type) > -1)
},
......@@ -27,16 +13,16 @@ exports.Helper = function(Sequelize) {
asTableIdentifier: function(name) {
var _name = name[0].toLowerCase() + name.replace(/^./, "")
return Helper.Inflection.singularize(_name) + "Id"
return instance.Inflection.singularize(_name) + "Id"
},
addPrefix: function(prefix, string, singularize) {
var _string = singularize ? Helper.Inflection.singularize(string) : Helper.Inflection.pluralize(string)
var _string = singularize ? instance.Inflection.singularize(string) : instance.Inflection.pluralize(string)
return prefix + _string[0].toUpperCase() + _string.replace(/^./, "")
},
asTableName: function(name) {
return Helper.Inflection.pluralize(name)
return instance.Inflection.pluralize(name)
},
asSqlDate: function(date) {
......@@ -54,9 +40,9 @@ exports.Helper = function(Sequelize) {
var actualValues = object.values,
result = []
Helper.Hash.forEach(actualValues, function(value, key) {
instance.Hash.forEach(actualValues, function(value, key) {
var dataType = object.table.attributes[key]
result.push(Helper.SQL.transformValueByDataType(value, dataType))
result.push(instance.SQL.transformValueByDataType(value, dataType))
})
return result
......@@ -64,39 +50,40 @@ exports.Helper = function(Sequelize) {
valuesForUpdate: function(object, options) {
var actualValues = object.values,
result = []
result = [],
self = instance
options = options || {}
Helper.Hash.forEach(actualValues, function(value, key) {
instance.Hash.forEach(actualValues, function(value, key) {
var dataType = object.table.attributes[key]
result.push([key, Helper.SQL.transformValueByDataType(value, dataType)].join(" = "))
result.push([key, self.SQL.transformValueByDataType(value, dataType)].join(" = "))
})
return result.join(options.seperator || ", ")
},
fieldsForInsertQuery: function(object) {
return Helper.Hash.keys(object.values).join(", ")
return instance.Hash.keys(object.values).join(", ")
},
transformValueByDataType: function(value, attributeOptions) {
var dataType = attributeOptions.type
if((value == null)||(typeof value == 'undefined')||((dataType.indexOf(Sequelize.INTEGER) > -1) && isNaN(value)))
if((value == null)||(typeof value == 'undefined')||((dataType.indexOf(instance.Sequelize.INTEGER) > -1) && isNaN(value)))
return "NULL"
if(dataType.indexOf(Sequelize.FLOAT) > -1)
if(dataType.indexOf(instance.Sequelize.FLOAT) > -1)
return (typeof value == 'number') ? value : parseFloat(value.replace(",", "."))
if(dataType.indexOf(Sequelize.BOOLEAN) > -1)
if(dataType.indexOf(instance.Sequelize.BOOLEAN) > -1)
return (value === true ? 1 : 0)
if(dataType.indexOf(Sequelize.INTEGER) > -1)
if(dataType.indexOf(instance.Sequelize.INTEGER) > -1)
return value
if(dataType.indexOf(Sequelize.DATE) > -1)
return ("'" + Helper.SQL.asSqlDate(value) + "'")
if(dataType.indexOf(instance.Sequelize.DATE) > -1)
return ("'" + instance.SQL.asSqlDate(value) + "'")
return ("'" + value + "'")
},
......@@ -106,26 +93,27 @@ exports.Helper = function(Sequelize) {
return ('id = ' + conditions)
else {
var result = []
Helper.Hash.forEach(conditions, function(value, key) {
var _value = Helper.SQL.transformValueByDataType(value, attributes[key])
instance.Hash.forEach(conditions, function(value, key) {
var _value = instance.SQL.transformValueByDataType(value, attributes[key])
if(_value == 'NULL') result.push(key + " IS NULL")
else result.push(key + "=" + _value)
})
return result.join(" AND ")
}
}
},
Hash: {
}
}
var injectHash = function(instance) {
instance.Hash = {
forEach: function(object, func) {
Helper.Hash.keys(object).forEach(function(key) {
instance.Hash.keys(object).forEach(function(key) {
func(object[key], key, object)
})
},
map: function(object, func) {
var result = []
Helper.Hash.forEach(object, function(value, key, object) {
instance.Hash.forEach(object, function(value, key, object) {
result.push(func(value, key, object))
})
return result
......@@ -140,14 +128,14 @@ exports.Helper = function(Sequelize) {
values: function(object) {
var result = []
Helper.Hash.keys(object).forEach(function(key) {
instance.Hash.keys(object).forEach(function(key) {
result.push(object[key])
})
return result
},
merge: function(source, target, force) {
Helper.Hash.forEach(source, function(value, key) {
instance.Hash.forEach(source, function(value, key) {
if(!target[key] || force)
target[key] = value
})
......@@ -155,14 +143,16 @@ exports.Helper = function(Sequelize) {
},
without: function(object, withoutKeys) {
var result = {}
Helper.Hash.forEach(object, function(value, key) {
instance.Hash.forEach(object, function(value, key) {
if(withoutKeys.indexOf(key) == -1)
result[key] = value
})
return result
}
},
Array: {
}
}
var injectArray = function(instance) {
instance.Array = {
map: function(array, func) {
var result = []
array.forEach(function(element) {
......@@ -197,10 +187,34 @@ exports.Helper = function(Sequelize) {
arr2.forEach(function(e) { result.push(e) })
return result
}
},
}
}
var injectBasics = function(instance) {
instance.configure = function(options) {
this.options = options
}
Inflection: require(__dirname + "/../inflection/inflection")
instance.log = function(obj) {
var sys = require("sys")
sys.log(sys.inspect(obj))
}
return Helper
instance.evaluateTemplate = function(template, replacements) {
var result = template
this.Hash.keys(replacements).forEach(function(key) {
result = result.replace("%{" + key + "}", replacements[key])
})
return result
}
}
var Helper = function(Sequelize) {
this.Sequelize = Sequelize
this.Inflection = require(__dirname + "/../inflection/inflection")
injectBasics(this)
injectSQL(this)
injectHash(this)
injectArray(this)
}
exports.Helper = Helper
\ No newline at end of file
......@@ -12,7 +12,7 @@ var Sequelize = function(database, username, password, options) {
}
var classMethods = {
Helper: new require(__dirname + "/Helper").Helper(Sequelize),
Helper: new (require(__dirname + "/Helper").Helper)(Sequelize),
STRING: 'VARCHAR(255)',
TEXT: 'TEXT',
......
......@@ -3,7 +3,6 @@ var Sequelize = require(__dirname + "/../../lib/sequelize/Sequelize").Sequelize,
Foo = s.define('Foo', { name: Sequelize.TEXT }),
Bar = s.define('Bar', { nr: Sequelize.INTEGER })
module.exports = {
'should have no fetchedAssociations first': function(assert, beforeExit) {
var allowExit = false
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!