Code coverage report for src/Common/Chain.js

Statements: 100% (48 / 48)      Branches: 80% (8 / 10)      Functions: 100% (11 / 11)      Lines: 100% (48 / 48)      Ignored: none     

All files » src/Common/ » Chain.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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246    1                           1                   1   1320   1320 1206     1320                                 1   7089   7089   689       689 613       689     689   689                                       1   1894   1894   631       631 3     631         631     631   631   631     5       631 5       631               631                                   1     729     729       7089 7089           729       1894 1894         729                                                                                       1   729     729     729 8983         1     1    
'use strict';
 
var _    = require("lodash"),
    Type = require("./Type"),
    Safe = require("./Safe"),
    ChainContext    = require("./Chain/ChainContext");
 
 
 
/**
 *
 * Module internal code
 *
 * @type {Object}
 *
 */
var Internal = {};
 
/**
 *
 * Gets the argument list
 *
 * @param  {Object} argsObj
 * @return {Array}
 *
 */
Internal.getArgumentList = function(argsObj){
 
    var args = [];
 
    _.each(argsObj, function(obj, index){
        args.push(obj);
    });
 
    return args;
 
};
 
/**
 *
 * Wraps the chainable function
 *
 * @param {Internal.ChainContext} context
 * @param {Function}    fn
 * @param {Object}      chainableFns
 * @param {Object}      evaluationFns
 * @param {Object}      options
 *
 * @return {Function}
 *
 */
Internal.wrapChainableFunction = function(context, fn, chainableFns, evaluationFns, options){
 
    options = Safe.object(options);
 
    return function() {
 
        var curContext  = context,
            args        = Internal.getArgumentList(arguments);
 
        /// initialize context if needed
        if(!curContext){
            curContext = new ChainContext(options);
        }
 
        /// add the function to the curContext
        curContext.add(fn, args);
 
        /// this is usefull to create "typed" instances of the Chain
        var ChainClass = Safe.function(options.type, Chain);
 
        return new ChainClass(chainableFns, evaluationFns, options, curContext);
 
    };
 
};
 
/**
 *
 * Wraps a returnable function. The last argument of the function is always the error, or null
 * if doesn't exists.
 *
 * @param {Internal.ChainContext} context
 * @param {Function}    fn
 * @param {Object}      chainableFns
 * @param {Object}      evaluationFns
 * @param {Object}      options
 *
 * @return {Function}
 *
 */
Internal.wrapReturnableFunction = function(context, fn, chainableFns, evaluationFns, options){
 
    options = Safe.object(options);
 
    return function() {
 
        var curContext  = context,
            args        = Internal.getArgumentList(arguments);
 
        /// initialize context if needed
        if(!curContext){
            curContext = new ChainContext(options);
        }
 
        var result  = null,
            err     = null;
 
        /// wrap the arguments to .apply call. The last argument should be the one defined
        /// on the options.
        var _args = [ args.shift() ];
 
        /// sets the scope of the context
        var scope = curContext.setScope(options.scope);
 
        try {
            /// execute all the chained functions
            result = curContext.exec(_args, options.argument);
        }
        catch(e){
            err = e;
        }
 
        /// if its on pipe mode, wrap the result so it can be correctly .apply()
        if(options.pipe){
            result = [ result ];
        }
 
        /// create the evaluation function arguments
        var fnArgs = Safe.array(result, [ null ])
            /// concat the arguments if they are not undefined
            .concat(Safe.array(args))
            /// concat the argument if is not undefined
            .concat(Safe.array(options.argument))
            /// concat the err as last argument
            .concat([err]);
 
        return fn.apply(scope, fnArgs);
 
    };
 
};
 
/**
 *
 * Get chain functions
 *
 * @param  {Internal.CurrentAssertionContext} assertionContext
 * @param  {Object} chainableFns
 * @param  {Object} evaluationFns
 * @param  {Object} options
 *
 * @return {Object}
 *
 */
Internal.getChainFunctions = function(context, chainableFns, evaluationFns, options){
 
    /// merge the default fns and the custom functions
    var fns = _.assign({}, chainableFns);
 
    /// wrap the chainable functions
    _.each(
        fns,
        function(fn, name){
 
            Eif(Type.isFunction(fn)){
                fns[name] = Internal.wrapChainableFunction(context, fn, chainableFns, evaluationFns, options);
            }
 
        });
 
    /// wrap the returning functions
    _.each(
        evaluationFns,
        function(fn, name){
 
            Eif(Type.isFunction(fn)){
                fns[name] = Internal.wrapReturnableFunction(context, fn, chainableFns, evaluationFns, options);
            }
 
        });
 
    return fns;
 
};
 
/**
 *
 * Chain constructor
 * @class
 *
 * @param {Object}          chainableFns
 * @param {Object}          evaluationFns
 * @param {Object}          options
 * @param {ChainContext}    context
 *
 * @return {Object}
 *
 *
 * @example
 *
 * new Chain(
 *
 *     /// chainable fns
 *     {
 *         "fn": function(){}
 *     },
 *
 *     /// evaluation fns
 *     {},
 *
 *     /// options fns
 *     {
 *         type         : {},
 *         pipe         : [true|false],
 *         scope        : Function,
 *         argument     : undefined
 *     },
 *
 *     /// context
 *     {}
 *
 * );
 *
 *
 */
var Chain = function(chainableFns, evaluationFns, options, context){
 
    var scope   = this;
 
    /// chain the functions
    var fns = Internal.getChainFunctions(context, chainableFns, evaluationFns, options);
 
    /// initialize this context
    _.each(fns, function(val, key){
        scope[key] = val;
    });
 
};
 
Chain.prototype = {};
 
 
module.exports = Chain;