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

Commit 8fda52ba by Jochem Maas

make it possible to define property getter and setter functions in a data attrib…

…ute definition (by defining a "get" and/or "set" property) ... these take preference to getters/setters defined (for an attribute of the given name) in the options object - some buster tests included
1 parent e8a518e3
......@@ -83,19 +83,23 @@ module.exports = (function() {
}
Utils._.each(['Get', 'Set'], function(type) {
var opt = type.toLowerCase() + 'terMethods',
meth = '__define' + type + 'ter__';
if (self.options[opt]) {
Utils._.each(self.options[opt], function(fct, name) {
//var def = {};
var prop = type.toLowerCase(),
opt = prop + 'terMethods',
meth = '__define' + type + 'ter__',
funcs = Utils._.isObject(self.options[opt]) ? self.options[opt] : {}
;
Utils._.each(self.rawAttributes, function(attr, name) {
if (attr.hasOwnProperty(prop))
funcs[name] = attr[prop]
});
if (!Utils._.isFunction(fct))
throw new Error(type + 'ter for "' + name + '" is not a function.')
Utils._.each(funcs, function(fct, name) {
if (!Utils._.isFunction(fct))
throw new Error(type + 'ter for "' + name + '" is not a function.')
self.DAO.prototype[meth](name, fct);
})
}
self.DAO.prototype[meth](name, fct);
})
})
this.DAO.prototype.attributes = Object.keys(this.DAO.prototype.rawAttributes);
......
......@@ -381,7 +381,7 @@ module.exports = (function() {
})(this);
// node-v0.8.19:
// calling either __defineGetter__ any previously defined setters for the attribute in
// calling __defineGetter__ destroys any previously defined setters for the attribute in
// question *if* that property setter was defined on the object's prototype (which is what
// we do in dao-factory) ... therefore we need to [re]define both the setter and getter
// here with either the function that already existed OR the default/automatic definition
......
......@@ -101,17 +101,37 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
expect(user.selectedValues).toEqual({ username: 'John Wayne' })
})
it("attaches getter and setter methods", function() {
var Product = this.sequelize.define('ProductWithSettersAndGetters', {
it("attaches getter and setter methods from attribute definition", function() {
var Product = this.sequelize.define('ProductWithSettersAndGetters1', {
price: {
type: Sequelize.INTEGER,
get : function() {
return 'answer = ' + this.getDataValue('price');
},
set : function(v) {
return this.setDataValue('price', v + 42);
}
}
},{
});
expect(Product.build({price: 42}).price).toEqual('answer = 84');
var p = Product.build({price: 1});
expect(p.price).toEqual('answer = 43');
p.price = 0;
expect(p.price).toEqual('answer = 42'); // ah finally the right answer :-)
})
it("attaches getter and setter methods from options", function() {
var Product = this.sequelize.define('ProductWithSettersAndGetters2', {
priceInCents: {
type: Sequelize.INTEGER
}
},{
instanceMethods: {
foo: function() {
console.log('woot')
}
},
setterMethods: {
price: function(value) {
this.dataValues.priceInCents = value * 100;
......@@ -131,6 +151,31 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
expect(Product.build({price: 20}).priceInCents).toEqual(20 * 100);
expect(Product.build({priceInCents: 30 * 100}).price).toEqual('$' + 30);
})
it("attaches getter and setter methods from options only if not defined in attribute", function() {
var Product = this.sequelize.define('ProductWithSettersAndGetters3', {
price1: {
type: Sequelize.INTEGER,
set : function(v) { this.setDataValue('price1', v * 10); }
},
price2: {
type: Sequelize.INTEGER,
get : function(v) { return this.getDataValue('price2') * 10; }
}
},{
setterMethods: {
price1: function(v) { this.setDataValue('price1', v * 100); }
},
getterMethods: {
price2: function() { return '$' + this.getDataValue('price2'); }
}
});
var p = Product.build({ price1: 1, price2: 2 });
expect(p.price1).toEqual(10);
expect(p.price2).toEqual(20);
})
})
describe('findOrCreate', function () {
......@@ -1147,7 +1192,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
done();
}.bind(this))
})
})
it("should return raw data when raw is true", function (done) {
this.User.find({ where: { username: 'barfooz'}}, { raw: true }).done(function (err, user) {
......@@ -1404,9 +1449,9 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
it("should return a DAO when queryOptions are not set", function (done) {
this.User.findAll({ where: { username: 'barfooz'}}).done(function (err, users) {
users.forEach(function (user) {
expect(user).toHavePrototype(this.User.DAO.prototype)
expect(user).toHavePrototype(this.User.DAO.prototype)
}, this)
done();
}.bind(this))
......@@ -1415,17 +1460,17 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
it("should return a DAO when raw is false", function (done) {
this.User.findAll({ where: { username: 'barfooz'}}, { raw: false }).done(function (err, users) {
users.forEach(function (user) {
expect(user).toHavePrototype(this.User.DAO.prototype)
expect(user).toHavePrototype(this.User.DAO.prototype)
}, this)
done();
}.bind(this))
})
})
it("should return raw data when raw is true", function (done) {
this.User.findAll({ where: { username: 'barfooz'}}, { raw: true }).done(function (err, users) {
users.forEach(function (user) {
expect(user).not.toHavePrototype(this.User.DAO.prototype)
expect(user).not.toHavePrototype(this.User.DAO.prototype)
expect(users[0]).toBeObject()
}, this)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!