1 |
|
2 |
/* |
3 |
* An Implementation of Traits |
4 |
* see http://www.iam.unibe.ch/~scg/cgi-bin/scgbib.cgi?query=nathanael+traits+composable+units+ecoop |
5 |
* |
6 |
* Current Composition rules: |
7 |
* - At compile time we override existing (at the time of rule application) methods |
8 |
* - At runtime we dont |
9 |
*/ |
10 |
Class("Joose.Role", { |
11 |
isa: Joose.Class, |
12 |
has: ["requiresMethodNames", "methodModifiers"], |
13 |
methods: { |
14 |
|
15 |
wrapMethod: function () { |
16 |
this.methodModifiers.push(arguments) |
17 |
var test = this.methodModifiers |
18 |
}, |
19 |
|
20 |
requiresMethod: function (methodName) { |
21 |
var bool = false; |
22 |
Joose.A.each(this.requiresMethodNames, function (name) { |
23 |
if(methodName == name) { |
24 |
bool = true |
25 |
} |
26 |
}) |
27 |
|
28 |
return bool |
29 |
}, |
30 |
|
31 |
addInitializer: Joose.emptyFunction, |
32 |
|
33 |
defaultClassFunctionBody: function () { |
34 |
var f = function () { |
35 |
throw new Error("Roles may not be instantiated.") |
36 |
}; |
37 |
f.toString = function () { return this.meta.className() } |
38 |
return f |
39 |
}, |
40 |
|
41 |
addSuperClass: function () { |
42 |
throw new Error("Roles may not inherit from a super class.") |
43 |
}, |
44 |
|
45 |
initialize: function () { |
46 |
this._name = "Joose.Role" |
47 |
this.requiresMethodNames = []; |
48 |
this.methodModifiers = []; |
49 |
}, |
50 |
|
51 |
addRequirement: function (methodName) { |
52 |
this.requiresMethodNames.push(methodName) |
53 |
}, |
54 |
|
55 |
apply: function (object) { |
56 |
|
57 |
if(joose.isInstance(object)) { |
58 |
// Create an anonymous subclass ob object's class |
59 |
|
60 |
object.detach(); |
61 |
object.meta.addRole(this.getClassObject()); |
62 |
} else { |
63 |
// object is actually a class |
64 |
var me = this; |
65 |
var names = this.getMethodNames(); |
66 |
|
67 |
//alert("Super"+me.name + " -> "+classObject.meta.name +"->" + names) |
68 |
|
69 |
Joose.A.each(names, function (name) { |
70 |
var m = me.dispatch(name); |
71 |
if(!object.meta.hasMethod(name) || object.meta.getMethodObject(name).isFromSuperClass()) { |
72 |
object.meta.addMethodObject(m.meta) |
73 |
} |
74 |
}) |
75 |
|
76 |
Joose.A.each(this.methodModifiers, function (paras) { |
77 |
object.meta.wrapMethod.apply(object.meta, paras) |
78 |
}) |
79 |
} |
80 |
}, |
81 |
|
82 |
hasRequiredMethods: function (classObject, throwException) { |
83 |
var me = this |
84 |
var complete = true |
85 |
Joose.A.each(this.requiresMethodNames, function (value) { |
86 |
var found = classObject.meta.can(value) |
87 |
if(!found) { |
88 |
if(throwException) { |
89 |
throw("Class "+classObject.meta.className()+" does not fully implement the role "+me.meta.className()+". The method is "+value+" missing.") |
90 |
} |
91 |
complete = false |
92 |
return |
93 |
} |
94 |
}) |
95 |
return complete |
96 |
}, |
97 |
|
98 |
isImplementedBy: function (classObject, throwException) { |
99 |
|
100 |
var complete = this.hasRequiredMethods(classObject, throwException); |
101 |
if(complete) { |
102 |
complete = this.implementsMyMethods(classObject); |
103 |
} |
104 |
return complete |
105 |
} |
106 |
} |
107 |
}) |
108 |
|
109 |
Joose.Role.anonymousClassCounter = 0; |