metadata.js 1.5 KB

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