Created
April 30, 2012 23:38
-
-
Save dodo/2563663 to your computer and use it in GitHub Desktop.
feature driven development
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inherits = require 'inherits' # https://github.com/isaacs/inherits/blob/master/inherits.js | |
module.exports = feature = (list...) -> | |
if list.length is 0 | |
return -> | |
else if list.length is 1 | |
classes = list[0] | |
else | |
classes = {} | |
for clazz,i in list | |
if not clazz.name? or clazz.name is "" | |
throw new Error "class[#{i}].name cannot be empty." | |
classes[clazz.name] = clazz | |
ctor = -> | |
@constructor = mixin | |
undefined # this is fucking !important | |
mixin = -> | |
for _, constructor of classes | |
constructor(arguments...) | |
this | |
mixin.mixin = {} | |
for key, clazz of classes | |
mixin.mixin[key] = clazz.prototype | |
for k, v of clazz.prototype | |
ctor::[k] = v | |
mixin.prototype = new ctor | |
return mixin | |
class F | |
foo: -> "foo"[email protected] | |
class B | |
bar: -> "bar"[email protected] | |
class M extends feature(fooz:F, barz:B) | |
toString: -> "#{@foo?()}#{@bar?()}" | |
class N extends feature(F,B) | |
toString: -> "#{@foo?()}nyan#{@bar?()}" | |
` // javascript start | |
var I = feature({fooi:F, booi:B}); | |
I.prototype.toString = function () { | |
return this.foo()+this.bar()+".js"; | |
}; | |
var _J = feature({fooj:F, booj:B}); | |
// var _J = feature(F, B, I); // throws up | |
var J = function J() { | |
// own stuff | |
_J.call(this); | |
} | |
J.prototype = _J.prototype; | |
J.prototype.toString = function () { | |
return ["own",this.foo(),this.bar()].join(""); | |
}; | |
var K = function K() { | |
// own stuff | |
K.super.call(this); | |
} | |
inherits(K, feature({fook:F, book:B, mk:M})); | |
K.prototype.toString = function () { | |
return [,this.foo(),"k",this.bar(),K.super.mixin.mk.toString.call(this)].join(" "); | |
}; | |
` # javascript end | |
console.log "#{new M}" | |
console.log "#{new N}" | |
console.log "#{new I}" | |
console.log "#{new J}" | |
console.log "#{new K}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solang du nicht mit Tester-Driven-Development anfängst ist alles gut.... ;-)