Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5f72b2
commit e483e6b
Showing
1 changed file
with
67 additions
and
52 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,75 @@ | ||
(function() { | ||
|
||
var multiMap = {}; | ||
var multiMap = {}; | ||
function Type() {}; | ||
function Protocol() {}; | ||
|
||
_.mixin({ | ||
extenderOf: function(x, protocol) { | ||
return _(x.protocols).indexOf(protocol) != -1; | ||
}, | ||
|
||
Multi: function(name, type, fn) { | ||
if(_.isUndefined(multiMap[name])) { | ||
var dispatchTable = {}, | ||
dispatcher = {}; | ||
multiMap[name] = dispatchTable; | ||
dispatcher[name] = function(instance) { | ||
var rest = _.rest(arguments); | ||
return multiMap[name][instance.name].apply(instance, rest); | ||
}; | ||
_.mixin(dispatcher); | ||
} | ||
multiMap[name][type.name] = fn; | ||
}, | ||
|
||
_.mixin({ | ||
extenderOf: function(x, protocol) { | ||
return _(x.protocols).indexOf(protocol) != -1; | ||
}, | ||
isProtocol: function() { | ||
return x instanceof Protocol; | ||
}, | ||
|
||
multi: function(name, type, fn) { | ||
if(_.isUndefined(multiMap[name])) { | ||
var dispatchTable = {}; | ||
multiMap[name] = dispatchTable; | ||
var dispatcher = {}; | ||
dispatcher[name] = function(instance) { | ||
var rest = _.rest(arguments); | ||
return multiMap[name][instance.name].apply(instance, rest); | ||
Protocol: function(obj) { | ||
obj.prototype = new Protocol; | ||
obj.toString = function() { | ||
return ["<Protocol: ", (obj.name || _uniqueId("UnnamedProtocol")), ">"].join(""); | ||
}; | ||
_.mixin(dispatcher); | ||
} | ||
multiMap[name][type.name] = fn; | ||
}, | ||
return obj; | ||
}, | ||
|
||
isType: function(x) { | ||
return x instanceof Type; | ||
}, | ||
|
||
protocol: function(obj) { | ||
obj.toString = function() { | ||
return ["<Protocol: ", (obj.name || _uniqueId("UnnamedProtocol")), ">"].join(""); | ||
}; | ||
return obj; | ||
}, | ||
|
||
datatype: function(obj) { | ||
obj = obj || {}; | ||
var klass = function() { | ||
if(_.isFunction(this.initialize)) { | ||
return this.initialize.apply(this, arguments); | ||
} else { | ||
return this; | ||
} | ||
}; | ||
var protocols = obj.protocols || []; | ||
var name = obj.name = (obj.name || _.uniqueId("UnnamedType")); | ||
var methodMap = protocols | ||
.map(function(x) { x = _(x).clone(); delete x.name; delete x.toString; return x; }); | ||
obj =_.reduce(methodMap.concat(obj), {}, function(memo, m) { | ||
return _.extend(memo, m); | ||
}); | ||
obj = _(obj).extend({ | ||
toString: function() { | ||
return ["<Type: ", name, ">"].join(""); | ||
} | ||
}); | ||
klass.name = name; | ||
klass.prototype = obj; | ||
klass.prototype.type = klass; | ||
klass.prototype.protocols = klass.protocols = protocols; | ||
return klass; | ||
} | ||
}); | ||
Type: function(obj) { | ||
obj = obj || {}; | ||
var klass = function() { | ||
if(_.isFunction(this.initialize)) { | ||
return this.initialize.apply(this, arguments); | ||
} else { | ||
return this; | ||
} | ||
}; | ||
var protocols = obj.protocols || [], | ||
name = obj.name = (obj.name || _.uniqueId("UnnamedType")), | ||
methodMap = protocols.map(function(x) { | ||
x = _(x).clone(); delete x.name; delete x.toString; return x; | ||
}); | ||
obj =_.reduce(methodMap.concat(obj), {}, function(memo, m) { | ||
return _.extend(memo, m); | ||
}); | ||
obj = _(obj).extend({ | ||
toString: function() { | ||
return ["<Type: ", name, ">"].join(""); | ||
} | ||
}); | ||
klass.name = name; | ||
klass.prototype = new Type(); | ||
klass.prototype.type = klass; | ||
klass.prototype.protocols = klass.protocols = protocols; | ||
_(obj).each(function(v, k) { | ||
klass.prototype[k] = v; | ||
}); | ||
return klass; | ||
} | ||
}); | ||
|
||
})(); |