Skip to content

Commit

Permalink
Apply defaults on create
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Oct 11, 2011
1 parent 13df1ef commit 9de9e59
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
19 changes: 17 additions & 2 deletions lib/abstract-class.js
Expand Up @@ -32,15 +32,17 @@ function AbstractClass(data) {

Object.keys(properties).forEach(function (attr) {
var _attr = '_' + attr,
attr_was = attr + '_was';
attr_was = attr + '_was';

// Hidden property to store currrent value
Object.defineProperty(this, _attr, {
writable: true,
enumerable: false,
configurable: true,
value: isdef(data[attr]) ? data[attr] :
(isdef(this[attr]) ? this[attr] : null)
(isdef(this[attr]) ? this[attr] : (
getDefault(attr)
))
});

// Public setters and getters
Expand All @@ -64,6 +66,19 @@ function AbstractClass(data) {
});

}.bind(this));

function getDefault(attr) {
var def = properties[attr]['default']
if (isdef(def)) {
if (typeof def === 'function') {
return def();
} else {
return def;
}
} else {
return null;
}
}
};

/**
Expand Down
20 changes: 12 additions & 8 deletions test/common_test.js
Expand Up @@ -43,7 +43,7 @@ function testOrm(schema) {
Post = schema.define('Post', {
title: { type: String, length: 255 },
content: { type: Text },
date: { type: Date, detault: Date.now },
date: { type: Date, default: Date.now },
published: { type: Boolean, default: false }
});

Expand Down Expand Up @@ -82,19 +82,24 @@ function testOrm(schema) {
});

it('should initialize object properly', function (test) {
var hw = 'Hello word', post = new Post({title: hw});
var hw = 'Hello word',
now = Date.now(),
post = new Post({title: hw});

test.equal(post.title, hw);
test.ok(!post.propertyChanged('title'));
post.title = 'Goodbye, Lenin';
test.equal(post.title_was, hw);
test.ok(post.propertyChanged('title'));
// test.ok(post.isNewRecord());
test.strictEqual(post.published, false);
test.ok(post.date >= now);
test.ok(post.isNewRecord());
test.done();
});

it('should be expoted to JSON', function (test) {
test.equal(JSON.stringify(new Post({id: 1, title: 'hello, json'})),
'{"id":1,"title":"hello, json","content":null,"date":null,"published":null,"userId":null}');
test.equal(JSON.stringify(new Post({id: 1, title: 'hello, json', date: 1})),
'{"id":1,"title":"hello, json","content":null,"date":1,"published":false,"userId":null}');
test.done();
});

Expand All @@ -103,7 +108,6 @@ function testOrm(schema) {
if (err) throw err;
test.ok(post.id);
test.ok(!post.title, 'Title is blank');
test.ok(!post.date, 'Date is blank');
Post.exists(post.id, function (err, exists) {
if (err) throw err;
test.ok(exists);
Expand Down Expand Up @@ -243,10 +247,10 @@ function testOrm(schema) {
});

// matching null
Post.all({date: null, title: null}, function (err, res) {
Post.all({title: null}, function (err, res) {
var pass = true;
res.forEach(function (r) {
if (r.date != null || r.title != null) pass = false;
if (r.title != null) pass = false;
});
test.ok(res.length > 0);
test.ok(pass, 'Matching null');
Expand Down

0 comments on commit 9de9e59

Please sign in to comment.