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

Commit 82719d59 by Sascha Depold

first steps for better private method scoping

1 parent 12fd2280
Showing with 151 additions and 137 deletions
...@@ -115,12 +115,13 @@ ModelFactory.prototype.find = function(options) { ...@@ -115,12 +115,13 @@ ModelFactory.prototype.find = function(options) {
} }
ModelFactory.prototype.build = function(values, options) { ModelFactory.prototype.build = function(values, options) {
var instance = new Model(values, Utils._.extend(this.options, {hasPrimaryKeys: this.hasPrimaryKeys})) var instance = new Model(values, Utils._.extend(this.options, this.attributes, { hasPrimaryKeys: this.hasPrimaryKeys }))
, self = this , self = this
options = options || {} options = options || {}
instance.__definition = this instance.__definition = this
//console.log(this.attributes)
Utils._.map(this.attributes, function(definition, name) { Utils._.map(this.attributes, function(definition, name) {
if(typeof instance[name] == 'undefined') { if(typeof instance[name] == 'undefined') {
var value = null var value = null
...@@ -129,7 +130,7 @@ ModelFactory.prototype.build = function(values, options) { ...@@ -129,7 +130,7 @@ ModelFactory.prototype.build = function(values, options) {
value = self.rawAttributes[name].defaultValue value = self.rawAttributes[name].defaultValue
instance[name] = value instance[name] = value
instance.addAttribute(name, value) instance.__addAttribute(name, value)
} }
}) })
Utils._.each(this.options.instanceMethods || {}, function(fct, name) { instance[name] = fct }) Utils._.each(this.options.instanceMethods || {}, function(fct, name) { instance[name] = fct })
......
var Utils = require("./utils") var Utils = require("./utils")
, Mixin = require("./associations/mixin") , Mixin = require("./associations/mixin")
var Model = module.exports = function(values, options) { module.exports = (function() {
var self = this var Model = function(values, options) {
var self = this
this.__definition = null // will be set in Model.build this.__definition = null // will be set in Model.build
this.attributes = [] this.attributes = []
this.__options = options || {} this.__options = options || {}
// add all passed values to the model and store the attribute names in this.attributes initAttributes.call(this, values)
Utils._.map(values, function(value, key) { self.addAttribute(key, value) }) }
Utils.addEventEmitter(Model)
Utils._.extend(Model.prototype, Mixin.prototype)
// set id to null if not passed as value Model.Events = {
// a newly created model has no id insert: 'InsertQuery',
var defaults = this.__options.hasPrimaryKeys ? {} : { id: null } update: 'UpdateQuery',
destroy: 'DestroyQuery'
}
if(this.__options.timestamps) { Model.prototype.__defineGetter__('QueryGenerator', function() {
defaults[this.__options.underscored ? 'created_at' : 'createdAt'] = new Date() return this.__definition.QueryGenerator
defaults[this.__options.underscored ? 'updated_at' : 'updatedAt'] = new Date() })
if(this.__options.paranoid) Model.prototype.save = function() {
defaults[this.__options.underscored ? 'deleted_at' : 'deletedAt'] = null var attr = this.__options.underscored ? 'updated_at' : 'updatedAt'
if(this.hasOwnProperty(attr))
this[attr] = new Date()
if(this.isNewRecord) {
var self = this
var eventEmitter = new Utils.CustomEventEmitter(function() {
query.call(self, self.QueryGenerator.insertQuery(self.__definition.tableName, self.values))
.on('success', function(obj) {
obj.isNewRecord = false
eventEmitter.emit('success', obj)
})
.on('failure', function(err) { eventEmitter.emit('failure', err) })
})
return eventEmitter.run()
} else {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id
return query.call(this, this.QueryGenerator.updateQuery(this.__definition.tableName, this.values, identifier))
}
} }
Utils._.map(defaults, function(value, attr) { Model.prototype.updateAttributes = function(updates) {
if(!self.hasOwnProperty(attr)) var self = this
self.addAttribute(attr, value)
})
}
Utils.addEventEmitter(Model)
Utils._.extend(Model.prototype, Mixin.prototype)
Model.Events = { var readOnlyAttributes = Utils._.keys(this.__definition.primaryKeys)
insert: 'InsertQuery', readOnlyAttributes.push('id')
update: 'UpdateQuery', readOnlyAttributes.push('createdAt')
destroy: 'DestroyQuery' readOnlyAttributes.push('updatedAt')
} readOnlyAttributes.push('deletedAt')
Utils._.each(updates, function(value, attr) {
var updateAllowed = (
(readOnlyAttributes.indexOf(attr) == -1) &&
(readOnlyAttributes.indexOf(Utils._.underscored(attr)) == -1) &&
(self.attributes.indexOf(attr) > -1)
)
updateAllowed && (self[attr] = value)
})
return this.save()
}
Model.prototype.__defineGetter__('QueryGenerator', function() { Model.prototype.destroy = function() {
return this.__definition.QueryGenerator if(this.__options.timestamps && this.__options.paranoid) {
}) this[this.__options.underscored ? 'deleted_at' : 'deletedAt'] = new Date()
return this.save()
} else {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id
return query.call(this, this.QueryGenerator.deleteQuery(this.__definition.tableName, identifier))
}
}
Model.prototype.addAttribute = function(attribute, value) { Model.prototype.__defineGetter__("identifiers", function() {
this[attribute] = value var primaryKeys = Utils._.keys(this.__definition.primaryKeys)
this.attributes.push(attribute) , result = {}
} , self = this
Model.prototype.query = function() { if(!this.__definition.hasPrimaryKeys)
var args = Utils._.map(arguments, function(arg, _) { return arg }) primaryKeys = ['id']
, s = this.__definition.modelManager.sequelize
args.push(this) primaryKeys.forEach(function(identifier) {
return s.query.apply(s, args) result[identifier] = self[identifier]
} })
Model.prototype.save = function() { return result
var attr = this.__options.underscored ? 'updated_at' : 'updatedAt' })
if(this.hasOwnProperty(attr)) Model.prototype.__defineGetter__('isDeleted', function() {
this[attr] = new Date() var result = this.__options.timestamps && this.__options.paranoid
result = result && this[this.__options.underscored ? 'deleted_at' : 'deletedAt'] != null
if(this.isNewRecord) { return result
var self = this
var eventEmitter = new Utils.CustomEventEmitter(function() {
self.query(self.QueryGenerator.insertQuery(self.__definition.tableName, self.values))
.on('success', function(obj) {
obj.isNewRecord = false
eventEmitter.emit('success', obj)
})
.on('failure', function(err) { eventEmitter.emit('failure', err) })
})
return eventEmitter.run()
} else {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id
return this.query(this.QueryGenerator.updateQuery(this.__definition.tableName, this.values, identifier))
}
}
Model.prototype.updateAttributes = function(updates) {
var self = this
var readOnlyAttributes = Utils._.keys(this.__definition.primaryKeys)
readOnlyAttributes.push('id')
readOnlyAttributes.push('createdAt')
readOnlyAttributes.push('updatedAt')
readOnlyAttributes.push('deletedAt')
Utils._.each(updates, function(value, attr) {
var updateAllowed = (
(readOnlyAttributes.indexOf(attr) == -1) &&
(readOnlyAttributes.indexOf(Utils._.underscored(attr)) == -1) &&
(self.attributes.indexOf(attr) > -1)
)
updateAllowed && (self[attr] = value)
}) })
return this.save()
}
Model.prototype.destroy = function() { Model.prototype.__defineGetter__('values', function() {
if(this.__options.timestamps && this.__options.paranoid) { var result = {}
this[this.__options.underscored ? 'deleted_at' : 'deletedAt'] = new Date() , self = this
return this.save()
} else {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id
return this.query(this.QueryGenerator.deleteQuery(this.__definition.tableName, identifier))
}
}
Model.prototype.__defineGetter__("identifiers", function() { this.attributes.forEach(function(attr) {
var primaryKeys = Utils._.keys(this.__definition.primaryKeys) result[attr] = self[attr]
, result = {} })
, self = this
return result
})
if(!this.__definition.hasPrimaryKeys) Model.prototype.__defineGetter__('primaryKeyValues', function() {
primaryKeys = ['id'] var result = {}
, self = this
Utils._.each(this.__definition.primaryKeys, function(_, attr) {
result[attr] = self[attr]
})
primaryKeys.forEach(function(identifier) { return result
result[identifier] = self[identifier]
}) })
return result Model.prototype.equals = function(other) {
}) var result = true
, self = this
Model.prototype.__defineGetter__('isDeleted', function() { Utils._.each(this.values, function(value, key) {
var result = this.__options.timestamps && this.__options.paranoid result = result && (value == other[key])
result = result && this[this.__options.underscored ? 'deleted_at' : 'deletedAt'] != null })
return result return result
}) }
Model.prototype.__defineGetter__('values', function() { Model.prototype.equalsOneOf = function(others) {
var result = {} var result = false
, self = this , self = this
this.attributes.forEach(function(attr) { others.forEach(function(other) { result = result || self.equals(other) })
result[attr] = self[attr]
})
return result return result
}) }
Model.prototype.__defineGetter__('primaryKeyValues', function() { // private
var result = {}
, self = this
Utils._.each(this.__definition.primaryKeys, function(_, attr) { var initAttributes = function(values) {
result[attr] = self[attr] var self = this
})
return result // add all passed values to the model and store the attribute names in this.attributes
}) Utils._.map(values, function(value, key) { self.__addAttribute(key, value) })
Model.prototype.equals = function(other) { // set id to null if not passed as value
var result = true // a newly created model has no id
, self = this var defaults = this.__options.hasPrimaryKeys ? {} : { id: null }
Utils._.each(this.values, function(value, key) { if(this.__options.timestamps) {
result = result && (value == other[key]) defaults[this.__options.underscored ? 'created_at' : 'createdAt'] = new Date()
}) defaults[this.__options.underscored ? 'updated_at' : 'updatedAt'] = new Date()
if(this.__options.paranoid)
defaults[this.__options.underscored ? 'deleted_at' : 'deletedAt'] = null
}
return result Utils._.map(defaults, function(value, attr) {
} if(!self.hasOwnProperty(attr))
self.__addAttribute(attr, value)
})
}
Model.prototype.equalsOneOf = function(others) { var query = function() {
var result = false var args = Utils._.map(arguments, function(arg, _) { return arg })
, self = this , s = this.__definition.modelManager.sequelize
args.push(this)
return s.query.apply(s, args)
}
Model.prototype.__addAttribute = function(attribute, value) {
this[attribute] = value
this.attributes.push(attribute)
}
others.forEach(function(other) { result = result || self.equals(other) })
return result /* Add the instance methods to Model */
} Utils._.extend(Model.prototype, Mixin.prototype)
/* Add the instance methods to Model */ return Model
Utils._.extend(Model.prototype, Mixin.prototype) })()
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!