/
ny.interface.js
109 lines (92 loc) · 2.63 KB
/
ny.interface.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
( function( $n ){
/**
* the interface class
* @constructor
* @private
* @param {String} name
* @param {Object} methods
*/
var Interface = function( name, methods ){
if(arguments.length != 2) {
throw new Error("Interface constructor called with " + arguments.length
+ "arguments, but expected exactly 2.");
}
var i, j;
this.name = name;
this.methods = { fn: [], attr: [] };
for( j in methods ){
if( j == 'fn' || j == 'attr' ){
if( methods[ j ].constructor == Array ){
for( i = 0, len = methods[ j ].length; i < len; i++) {
if(typeof methods[ j ][ i ] !== 'string') {
throw new Error("Interface constructor expects method names to be "
+ "passed in as a string.");
}
this.methods[ j ].push(methods[ j ][ i ]);
}
}
else {
throw new Error("Interface constructor expects method member to be "
+ "Array of String");
}
}
else {
throw new Error("Interface constructor expects method member to be "
+ "'fn' or 'attr'.");
}
}
};
/**
* interface namespace and create an instance of interface add to interfaceList
* @param {String} name
* @param {Array[String]} methods
*/
$n.interface = function(name, methods) {
interfaceList[ name ] = new Interface( name, methods );
};
// Static class method.
$n.interface.ensure = function( object ) {
if(arguments.length < 2) {
throw new Error("Function Interface.ensureImplements called with " +
arguments.length + "arguments, but expected at least 2.");
}
var interface, i, j, k;
for(var i = 1, len = arguments.length; i < len; i++) {
// get interface
if( typeof arguments[i] == 'string' ){
interface = interfaceList[ arguments[ i ] ];
}
else {
interface = arguments[ i ];
}
// check interface
if(interface.constructor !== Interface) {
throw new Error("Function Interface.ensureImplements expects arguments "
+ "two and above to be instances of Interface.");
}
for( k in interface.methods ){
for( j = 0, methodsLen = interface.methods[ k ].length; j < methodsLen; j++) {
var method = interface.methods[ k ][ j ];
if(!object[ method ] || ( k == 'fn' && typeof object[ method ] !== 'function') ) {
throw new Error("Function Interface.ensureImplements: object "
+ "does not implement the " + interface.name
+ " interface. " + ( k == 'fn' ? 'Method' : 'attribute' ) + " " + method + " was not found.");
}
}
}
}
return true;
};
/**
* get interface by name
* @param {String}
*/
$n.interface.get = function( name ){
return interfaceList[ name ]
};
/**
* @private to store all the interface instance
*/
var interfaceList = {};
})( $n );