Code coverage report for preceptor-reporter/lib/abstractListener.js

Statements: 79.17% (19 / 24)      Branches: 50% (2 / 4)      Functions: 70% (7 / 10)      Lines: 79.17% (19 / 24)      Ignored: none     

All files » preceptor-reporter/lib/ » abstractListener.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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145      1 1 1   1                     1                     2   2   2                                     93                     1                   92                       81                                                   92         92               92 92 92       92                         1  
// Copyright 2014, Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.
 
var Base = require('preceptor-core').Base;
var utils = require('preceptor-core').utils;
var _ = require('underscore');
 
var defaultsListener = require('./defaults/listener');
 
/**
 * @class AbstractListener
 * @extends Base
 *
 * @property {object} _options
 * @property {ReportManager} _manager
 *
 * @event message
 */
var AbstractListener = Base.extend(
 
	/**
	 * Abstract listener constructor
	 *
	 * @param {object} [options]
	 * @param {object} [options.configuration]
	 * @param {object} [options.placeholder]
	 * @constructor
	 */
	function (options) {
		this.__super();
 
		this._options = utils.deepExtend({}, [defaultsListener, options || {}]);
 
		this.initialize();
	},
 
	{
		/**
		 * Initializes the instance
		 */
		initialize: function () {
			// Nothing yet
		},
 
 
		/**
		 * Gets the options
		 *
		 * @method getOptions
		 * @return {object}
		 */
		getOptions: function () {
			return this._options;
		},
 
 
		/**
		 * Gets the configuration supplied
		 *
		 * @method getConfiguration
		 * @return {object}
		 */
		getConfiguration: function () {
			return this.getOptions().configuration;
		},
 
		/**
		 * Gets the placeholder and their replacements
		 *
		 * @method getPlaceholder
		 * @return {object}
		 */
		getPlaceholder: function () {
			return this.getOptions().placeholder;
		},
 
 
		/**
		 * Triggers a message
		 *
		 * @method triggerMessage
		 * @param {string} messageType
		 * @param {*[]} data
		 */
		triggerMessage: function (messageType, data) {
			this.emit("message", messageType, data);
		},
 
 
		/**
		 * Parses a string and extracts message information
		 *
		 * @method parse
		 * @param {string} text
		 * @param {object} [placeholder]
		 * @return {string}
		 */
		parse: function (text, placeholder) {
			throw new Error('Unimplemented listener method "process".');
		},
 
 
		/**
		 * Processes a text with the placeholder
		 *
		 * @method processPlaceholder
		 * @param {string} text
		 * @param {object} [placeholder]
		 * @return {string}
		 */
		processPlaceholder: function (text, placeholder) {
			var localPlaceholder,
				keys,
				localText = text;
 
			// Directly supplied placeholder have priority
			Iif (placeholder) {
				keys = _.keys(placeholder);
				_.each(keys, function (key) {
					localText = localText.replace(key, placeholder[key]);
				});
			}
 
			// Try to find any placeholder from the global list
			localPlaceholder = this.getPlaceholder();
			keys = _.keys(localPlaceholder);
			_.each(keys, function (key) {
				localText = localText.replace(key, localPlaceholder[key]);
			});
 
			return localText;
		}
	},
 
	{
		/**
		 * @property TYPE
		 * @type {string}
		 * @static
		 */
		TYPE: 'AbstractListener'
	});
 
module.exports = AbstractListener;