Permalink
Browse files

initial work towards functions as blueprints

  • Loading branch information...
1 parent 28bd694 commit 8d0071e1e2cb690ede323ec15ba24c6c3acad503 @timoxley timoxley committed Jan 10, 2012
Showing with 105 additions and 31 deletions.
  1. +46 −31 lib/kin.js
  2. +59 −0 tests/kin_test.js
View
@@ -32,41 +32,56 @@ function generate(type, overrideProperties, callback) {
}
var key
var self = this
- this.linkProperties(currentBlueprint, overrideProperties, function(err, props) {
- var properties = _.clone(currentBlueprint)
- _.each(props, function(value, key) {
- properties[key] = value
- })
- var keys = _.keys(properties)
- async.forEachSeries(
- keys,
- function(key, callback) {
- var property = properties[key]
- if (typeof property == 'function') {
- Kin.get(properties, key, callback)
+ if (typeof currentBlueprint == 'function') {
+ currentBlueprint(function(err, data, meta) {
+ _.each(overrideProperties, function(value, key) {
+ if (/^_/.test(key) && key != '_id') {
+ var keyWithNoUnderscore = key.substr(1)
+ meta[keyWithNoUnderscore] = value
} else {
- callback()
+ data[key] = value
}
- },
- function(err) {
- if (err) throw err
- var meta = {}
- _.each(properties, function(value, key) {
- if (/^_/.test(key) && key != '_id') {
- var keyWithNoUnderscore = key.substr(1)
- meta[keyWithNoUnderscore] = properties[key]
- delete properties[key]
+ })
+ return callback(err, data, meta)
+ })
+
+ } else {
+ this.linkProperties(currentBlueprint, overrideProperties, function(err, props) {
+ var properties = _.clone(currentBlueprint)
+ _.each(props, function(value, key) {
+ properties[key] = value
+ })
+ var keys = _.keys(properties)
+ async.forEachSeries(
+ keys,
+ function(key, callback) {
+ var property = properties[key]
+ if (typeof property == 'function') {
+ Kin.get(properties, key, callback)
+ } else {
+ callback()
+ }
+ },
+ function(err) {
+ if (err) throw err
+ var meta = {}
+ _.each(properties, function(value, key) {
+ if (/^_/.test(key) && key != '_id') {
+ var keyWithNoUnderscore = key.substr(1)
+ meta[keyWithNoUnderscore] = properties[key]
+ delete properties[key]
+ }
+ })
+ if (Model) {
+ var model = new Model(properties)
+ return callback.call(model, null, model, meta)
+ } else {
+ return callback.call(properties, null, properties, meta)
}
- })
- if (Model) {
- var model = new Model(properties)
- return callback.call(model, null, model, meta)
- } else {
- return callback.call(properties, null, properties, meta)
}
- }
- )
- })
+ )
+ })
+ }
}
Kin.prototype._getGeneratorFunction = function(type, fixedOverrideProperties) {
var self = this
View
@@ -938,6 +938,65 @@ var tests = testCase({
test.done()
})
}
+ },
+ 'blueprint can take a function instead of an object': {
+ 'runs function': function(test) {
+ test.expect(1)
+ kin.blueprint('User', function(callback) {
+ test.ok(true)
+ callback()
+ })
+ kin.generate('User', function() {
+ test.done()
+ })
+ },
+ 'can generate an object': function(test) {
+ kin.blueprint('User', function(callback) {
+ callback(null, {name: 'Bill'})
+ })
+ kin.generate('User', function(err, user) {
+ test.ok(!err)
+ test.ok(user)
+ test.equal(user.name, 'Bill')
+ test.done()
+ })
+ },
+ 'can generate meta datas': function(test) {
+ kin.blueprint('User', function(callback) {
+ callback(null, {name: 'Bill'}, {count: 1})
+ })
+ kin.generate('User', function(err, user, meta) {
+ test.ok(!err)
+ test.ok(user)
+ test.equal(user.name, 'Bill')
+ test.equal(meta.count, 1)
+ test.done()
+ })
+ },
+
+ 'can take override properties': function(test) {
+ kin.blueprint('User', function(callback) {
+ callback(null, {name: 'Bill'})
+ })
+ kin.generate('User', {name: 'Joe'}, function(err, user) {
+ test.ok(!err)
+ test.ok(user)
+ test.equal(user.name, 'Joe')
+ test.done()
+ })
+ },
+ 'will override meta properties': function(test) {
+ kin.blueprint('User', function(callback) {
+ callback(null, {name: 'Bill'}, {count: 0})
+ })
+ kin.generate('User', {name: 'Joe', _count: 2}, function(err, user, meta) {
+ test.ok(!err)
+ test.ok(user)
+ test.equal(user.name, 'Joe')
+ test.equal(meta.count, 2)
+ test.done()
+ })
+ }
}
})

0 comments on commit 8d0071e

Please sign in to comment.