Code coverage report for src/Common/Chain/ChainContext.js

Statements: 100% (34 / 34)      Branches: 100% (14 / 14)      Functions: 100% (6 / 6)      Lines: 100% (33 / 33)      Ignored: none     

All files » src/Common/Chain/ » ChainContext.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    1                                         1   621               621                 621                 621                     1   1252 109       1252     1252   1252                           1   704   701   701                                 1   636 636   636         636       738     738     732 20       6       738 6           636 6 1       630       1  
'use strict';
 
var _    = require("lodash"),
    Safe = require("../Safe"),
    Type = require("../Type"),
 
    ChainFunction   = require("./ChainFunction");
 
 
/**
 *
 * A chain context keeps track of the current chainable state
 * @class
 *
 * @param {Object} options
 *
 * @example
 *
 * new ChainContext({
 *
 *     pipe:
 * })
 */
var ChainContext = function(options){
 
    options = Safe.object(options);
 
    /**
     * The context registered functions
     *
     * @type { [Internal.AssertionFunction] }
     *
     */
    this.fns = [];
 
    /**
     *
     * The shared fn context
     *
     * @type {Object}
     *
     */
    this.scope = this.setScope(options.scope);
 
    /**
     *
     * Pipe mode context
     *
     * @type {Boolean}
     *
     */
    this.pipe = Safe.boolean(options.pipe);
 
};
 
/**
 *
 * Sets the scope of this context.
 *
 * @param  {Object|Function} scope
 * @return {Object}
 */
ChainContext.prototype.setScope = function(scope){
 
    if(Type.isDefined(scope)){
        scope = Type.isFunction(scope) ? scope() : scope;
    }
 
    /// Maintain the current context if not defined
    scope = scope || this.scope;
 
    /// set the context
    this.scope = Safe.object(scope, {});
 
    return this.scope;
 
};
 
/**
 *
 * Chain the given function
 *
 * @param  {Function|Object}   fn
 * @param  {Array}  args
 *
 * @return {Internal.Chain}
 *
 */
ChainContext.prototype.add = function(fn, args){
 
    if(Type.isFunction(fn)) {
 
        fn = new ChainFunction({ fn: fn, args: args });
 
        this.fns.push( fn );
 
    }
 
};
 
/**
 *
 * Execute the context using the given argument as parameter
 *
 * @param  {*}          args
 * @param  {Array}      extraArgs
 * @param  {Funcion}    errCallback
 *
 * @return {*}
 **
 */
ChainContext.prototype.exec = function(args, extraArgs, errCallback){
 
    extraArgs   = Safe.value(extraArgs);
    errCallback = Safe.function(errCallback, function(err){ throw err; });
 
    var self    = this,
        result  = args,
        error   = null;
 
    /// execute the functions
    _.each(
        this.fns,
        function(fn, index){
 
            try {
 
                /// really execute the function
                var value = fn.apply(self.scope, result, extraArgs);
 
                /// if chain return values between the functions
                if(self.pipe) {
                    result = value;
                }
 
            } catch(e){
                error = e;
            }
 
            /// stop iterating
            if(error !== null){
                return false;
            }
 
        });
 
    /// if an error exists
    if(error){
        errCallback(error);
        return null;
    }
 
    /// returns
    return result;
 
};
 
module.exports = ChainContext;