API Docs for: 0.9.1
Show:

File: lib/abstractListener.js

  1. // Copyright 2014, Yahoo! Inc.
  2. // Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.
  3.  
  4. var Base = require('preceptor-core').Base;
  5. var utils = require('preceptor-core').utils;
  6. var _ = require('underscore');
  7.  
  8. var defaultsListener = require('./defaults/listener');
  9.  
  10. /**
  11. * @class AbstractListener
  12. * @extends Base
  13. *
  14. * @property {object} _options
  15. * @property {ReportManager} _manager
  16. *
  17. * @event message
  18. */
  19. var AbstractListener = Base.extend(
  20.  
  21. /**
  22. * Abstract listener constructor
  23. *
  24. * @param {object} [options]
  25. * @param {object} [options.configuration]
  26. * @param {object} [options.placeholder]
  27. * @constructor
  28. */
  29. function (options) {
  30. this.__super();
  31.  
  32. this._options = utils.deepExtend({}, [defaultsListener, options || {}]);
  33.  
  34. this.initialize();
  35. },
  36.  
  37. {
  38. /**
  39. * Initializes the instance
  40. */
  41. initialize: function () {
  42. // Nothing yet
  43. },
  44.  
  45.  
  46. /**
  47. * Gets the options
  48. *
  49. * @method getOptions
  50. * @return {object}
  51. */
  52. getOptions: function () {
  53. return this._options;
  54. },
  55.  
  56.  
  57. /**
  58. * Gets the configuration supplied
  59. *
  60. * @method getConfiguration
  61. * @return {object}
  62. */
  63. getConfiguration: function () {
  64. return this.getOptions().configuration;
  65. },
  66.  
  67. /**
  68. * Gets the placeholder and their replacements
  69. *
  70. * @method getPlaceholder
  71. * @return {object}
  72. */
  73. getPlaceholder: function () {
  74. return this.getOptions().placeholder;
  75. },
  76.  
  77.  
  78. /**
  79. * Triggers a message
  80. *
  81. * @method triggerMessage
  82. * @param {string} messageType
  83. * @param {*[]} data
  84. */
  85. triggerMessage: function (messageType, data) {
  86. this.emit("message", messageType, data);
  87. },
  88.  
  89.  
  90. /**
  91. * Parses a string and extracts message information
  92. *
  93. * @method parse
  94. * @param {string} text
  95. * @param {object} [placeholder]
  96. * @return {string}
  97. */
  98. parse: function (text, placeholder) {
  99. throw new Error('Unimplemented listener method "process".');
  100. },
  101.  
  102.  
  103. /**
  104. * Processes a text with the placeholder
  105. *
  106. * @method processPlaceholder
  107. * @param {string} text
  108. * @param {object} [placeholder]
  109. * @return {string}
  110. */
  111. processPlaceholder: function (text, placeholder) {
  112. var localPlaceholder,
  113. keys,
  114. localText = text;
  115.  
  116. // Directly supplied placeholder have priority
  117. if (placeholder) {
  118. keys = _.keys(placeholder);
  119. _.each(keys, function (key) {
  120. localText = localText.replace(key, placeholder[key]);
  121. });
  122. }
  123.  
  124. // Try to find any placeholder from the global list
  125. localPlaceholder = this.getPlaceholder();
  126. keys = _.keys(localPlaceholder);
  127. _.each(keys, function (key) {
  128. localText = localText.replace(key, localPlaceholder[key]);
  129. });
  130.  
  131. return localText;
  132. }
  133. },
  134.  
  135. {
  136. /**
  137. * @property TYPE
  138. * @type {string}
  139. * @static
  140. */
  141. TYPE: 'AbstractListener'
  142. });
  143.  
  144. module.exports = AbstractListener;
  145.