Skip to content

Instantly share code, notes, and snippets.

@dodo
Created April 30, 2012 23:38
Show Gist options
  • Save dodo/2563663 to your computer and use it in GitHub Desktop.
Save dodo/2563663 to your computer and use it in GitHub Desktop.
feature driven development
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"+@constructor.name
class B
bar: -> "bar"+@constructor.name
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}"
@rtreffer
Copy link

rtreffer commented May 1, 2012

Solang du nicht mit Tester-Driven-Development anfängst ist alles gut.... ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment