7dae56248ff0598feb4ea4055fcf30651c50af55
[aai/esr-gui.git] /
1 var f = require('util').format;
2
3 var Define = function(name, object, stream) {
4   this.name = name;
5   this.object = object;
6   this.stream = typeof stream == 'boolean' ? stream : false;
7   this.instrumentations = {};
8 }
9
10 Define.prototype.classMethod = function(name, options) {
11   var keys = Object.keys(options).sort();
12   var key = generateKey(keys, options);
13
14   // Add a list of instrumentations
15   if(this.instrumentations[key] == null) {
16     this.instrumentations[key] = {
17       methods: [], options: options
18     }
19   }
20
21   // Push to list of method for this instrumentation
22   this.instrumentations[key].methods.push(name);
23 }
24
25 var generateKey = function(keys, options) {
26   var parts = [];
27   for(var i = 0; i < keys.length; i++) {
28     parts.push(f('%s=%s', keys[i], options[keys[i]]));
29   }
30
31   return parts.join();
32 }
33
34 Define.prototype.staticMethod = function(name, options) {
35   options.static = true;
36   var keys = Object.keys(options).sort();
37   var key = generateKey(keys, options);
38
39   // Add a list of instrumentations
40   if(this.instrumentations[key] == null) {
41     this.instrumentations[key] = {
42       methods: [], options: options
43     }
44   }
45
46   // Push to list of method for this instrumentation
47   this.instrumentations[key].methods.push(name);
48 }
49
50 Define.prototype.generate = function(keys, options) {
51   // Generate the return object
52   var object = {
53     name: this.name, obj: this.object, stream: this.stream,
54     instrumentations: []
55   }
56
57   for(var name in this.instrumentations) {
58     object.instrumentations.push(this.instrumentations[name]);
59   }
60
61   return object;
62 }
63
64 module.exports = Define;