metadata.js 1.59 KB
Newer Older
nittaya chompoosan's avatar
nittaya chompoosan committed
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
'use strict';

var f = require('util').format;

var Define = function(name, object, stream) {
  this.name = name;
  this.object = object;
  this.stream = typeof stream === 'boolean' ? stream : false;
  this.instrumentations = {};
};

Define.prototype.classMethod = function(name, options) {
  var keys = Object.keys(options).sort();
  var key = generateKey(keys, options);

  // Add a list of instrumentations
  if (this.instrumentations[key] == null) {
    this.instrumentations[key] = {
      methods: [],
      options: options
    };
  }

  // Push to list of method for this instrumentation
  this.instrumentations[key].methods.push(name);
};

var generateKey = function(keys, options) {
  var parts = [];
  for (var i = 0; i < keys.length; i++) {
    parts.push(f('%s=%s', keys[i], options[keys[i]]));
  }

  return parts.join();
};

Define.prototype.staticMethod = function(name, options) {
  options.static = true;
  var keys = Object.keys(options).sort();
  var key = generateKey(keys, options);

  // Add a list of instrumentations
  if (this.instrumentations[key] == null) {
    this.instrumentations[key] = {
      methods: [],
      options: options
    };
  }

  // Push to list of method for this instrumentation
  this.instrumentations[key].methods.push(name);
};

Define.prototype.generate = function() {
  // Generate the return object
  var object = {
    name: this.name,
    obj: this.object,
    stream: this.stream,
    instrumentations: []
  };

  for (var name in this.instrumentations) {
    object.instrumentations.push(this.instrumentations[name]);
  }

  return object;
};

module.exports = Define;