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

Statements: 28.13% (9 / 32)      Branches: 0% (0 / 2)      Functions: 0% (0 / 16)      Lines: 28.13% (9 / 32)      Ignored: none     

All files » preceptor-reporter/lib/ » abstractLoader.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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187      1 1 1 1 1 1   1                   1                                                                                                                                                                                                                                                                                                                                         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 glob = require('glob');
var Promise = require('promise');
var EventReporter = require('./reporter/event');
 
var defaultsLoader = require('./defaults/loader');
 
/**
 * @class AbstractLoader
 * @extends Base
 *
 * @property {object} _options
 * @property {EventReporter} _reporter
 * @property {int} _id
 */
var AbstractLoader = Base.extend(
 
	/**
	 * Abstract loader constructor
	 *
	 * @param {object} options
	 * @constructor
	 */
	function (options) {
		this.__super();
 
		this._options = utils.deepExtend({}, [defaultsLoader, options || {}]);
		this._reporter = new EventReporter();
 
		this.initialize();
	},
 
	{
		/**
		 * Initializes the instance
		 *
		 * @method initialize
		 */
		initialize: function () {
 
			this._id = +(new Date()) + Math.floor(Math.random() * 100000);
 
			this.getReporter().on('message', function (areaType, messageType, params) {
				this.emit('message', areaType, messageType, params);
			}.bind(this));
		},
 
 
		/**
		 * Create a new id
		 *
		 * @method newId
		 * @return {int}
		 */
		newId: function () {
			return this._id++;
		},
 
		/**
		 * Gets the options
		 *
		 * @method getOptions
		 * @return {object}
		 */
		getOptions: function () {
			return this._options;
		},
 
		/**
		 * Gets the type of reporter
		 *
		 * @method getType
		 * @return {string}
		 */
		getType: function () {
			return this.getOptions().type;
		},
 
		/**
		 * Gets the configuration supplied
		 *
		 * @method getConfiguration
		 * @return {object}
		 */
		getConfiguration: function () {
			return this.getOptions().configuration;
		},
 
		/**
		 * Output path
		 *
		 * @method getPath
		 * @return {string}
		 */
		getPath: function () {
			return this.getOptions().path;
		},
 
		/**
		 * Gets the reporter instance
		 *
		 * @method getReporter
		 * @return {EventReporter}
		 */
		getReporter: function () {
			return this._reporter;
		},
 
 
		/**
		 * Starts the loading process
		 *
		 * @method process
		 * @param {string} parentId
		 * @return {Promise}
		 */
		process: function (parentId) {
			var files = this._gatherFiles();
			return this._processFiles(parentId, files);
		},
 
 
		/**
		 * Gathers all files that is selected by the glob
		 *
		 * @method _gatherFiles
		 * @return {string[]}
		 * @private
		 */
		_gatherFiles: function () {
			return glob.sync(this.getPath());
		},
 
		/**
		 * Processes a list of files
		 *
		 * @method _processFiles
		 * @param {string} parentId
		 * @param {string[]} files
		 * @return {Promise}
		 * @private
		 */
		_processFiles: function (parentId, files) {
 
			var promise = Promise.resolve();
 
			_.each(files, function (file) {
				promise = promise.then(function () {
					return this._processFile(parentId, file);
				}.bind(this));
			}, this);
 
			return promise.then(null, function (err) {
				console.err(err.stack);
			});
		},
 
		/**
		 * Processes a single file
		 *
		 * @method _processFile
		 * @param {string} parentId
		 * @param {string} file
		 * @return {Promise}
		 * @private
		 */
		_processFile: function (parentId, file) {
			throw new Error('Unimplemented loader method "_processFile".');
		}
	},
 
	{
		/**
		 * @property TYPE
		 * @type {string}
		 * @static
		 */
		TYPE: 'AbstractLoader'
	});
 
module.exports = AbstractLoader;