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

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() { ...@@ -83,19 +83,23 @@ module.exports = (function() {
} }
Utils._.each(['Get', 'Set'], function(type) { Utils._.each(['Get', 'Set'], function(type) {
var opt = type.toLowerCase() + 'terMethods', var prop = type.toLowerCase(),
meth = '__define' + type + 'ter__'; opt = prop + 'terMethods',
meth = '__define' + type + 'ter__',
if (self.options[opt]) { funcs = Utils._.isObject(self.options[opt]) ? self.options[opt] : {}
Utils._.each(self.options[opt], function(fct, name) { ;
//var def = {};
Utils._.each(self.rawAttributes, function(attr, name) {
if (attr.hasOwnProperty(prop))
funcs[name] = attr[prop]
});
Utils._.each(funcs, function(fct, name) {
if (!Utils._.isFunction(fct)) if (!Utils._.isFunction(fct))
throw new Error(type + 'ter for "' + name + '" is not a function.') 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); this.DAO.prototype.attributes = Object.keys(this.DAO.prototype.rawAttributes);
......
...@@ -381,7 +381,7 @@ module.exports = (function() { ...@@ -381,7 +381,7 @@ module.exports = (function() {
})(this); })(this);
// node-v0.8.19: // 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 // 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 // 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 // here with either the function that already existed OR the default/automatic definition
......
...@@ -101,17 +101,37 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -101,17 +101,37 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
expect(user.selectedValues).toEqual({ username: 'John Wayne' }) expect(user.selectedValues).toEqual({ username: 'John Wayne' })
}) })
it("attaches getter and setter methods", function() { it("attaches getter and setter methods from attribute definition", function() {
var Product = this.sequelize.define('ProductWithSettersAndGetters', { 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: { priceInCents: {
type: Sequelize.INTEGER type: Sequelize.INTEGER
} }
},{ },{
instanceMethods: {
foo: function() {
console.log('woot')
}
},
setterMethods: { setterMethods: {
price: function(value) { price: function(value) {
this.dataValues.priceInCents = value * 100; this.dataValues.priceInCents = value * 100;
...@@ -131,6 +151,31 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -131,6 +151,31 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
expect(Product.build({price: 20}).priceInCents).toEqual(20 * 100); expect(Product.build({price: 20}).priceInCents).toEqual(20 * 100);
expect(Product.build({priceInCents: 30 * 100}).price).toEqual('$' + 30); 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 () { describe('findOrCreate', function () {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!