Code coverage report for coa/lib/coaobject.js

# All files / coa/lib coaobject.js

75% Statements 12/16
50% Branches 1/2
71.43% Functions 5/7
75% Lines 12/16

    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

    1x

    1x

    58x
    58x
    58x
    58x

    44x
    44x

    3x

    3x

    8x

    38x

     'use strict';

    const Q = require('q');

    /**
     * COA Object
     *
     * Base class for all COA-related objects
     *
     * --------|-----|-----|-----
     *         | Cmd | Opt | Arg
     * --------|-----|-----|-----
     *  name   | ✓   | ✓   | ✓
     *  title  | ✓   | ✓   | ✓
     *  comp   | ✓   | ✓   | ✓
     *  reject | ✓   | ✓   | ✓
     *  end    | ✓   | ✓   | ✓
     *  apply  | ✓   | ✓   | ✓
     *
     * @class CoaObject
     */
    module.exports = class CoaObject {
        constructor(cmd) {
            this._cmd = cmd;
            this._name = null;
            this._title = null;
            this._comp = null;
        }

        /**
         * Set a canonical identifier to be used anywhere in the API.
         *
         * @param {String} name - command, option or argument name
         * @returns {COA.CoaObject} - this instance (for chainability)
         */
        name(name) {
            this._name = name;
            return this;
        }

        /**
         * Set a long description to be used anywhere in text messages.
         * @param {String} title - human readable entity title
         * @returns {COA.CoaObject} - this instance (for chainability)
         */
        title(title) {
            this._title = title;
            return this;
        }

        /**
         * Set custom additional completion for current object.
         *
         * @param {Function} comp - completion generation function,
         *         invoked in the context of object instance.
         *         Accepts parameters:
         *                 - {Object} opts - completion options
         *         It can return promise or any other value threated as a result.
         * @returns {COA.CoaObject} - this instance (for chainability)
         */
        comp(comp) {
            this._comp = comp;
            return this;
        }

        /**
         * Apply function with arguments in a context of object instance.
         *
         * @param {Function} fn - body
         * @param {Array.<*>} args... - arguments
         * @returns {COA.CoaObject} - this instance (for chainability)
         */
        apply(fn) {
            arguments.length > 1?
                fn.apply(this, [].slice.call(arguments, 1))
                : fn.call(this);

            return this;
        }

        /**
         * Return reject of actions results promise with error code.
         * Use in .act() for return with error.
         * @param {Object} reason - reject reason
         *         You can customize toString() method and exitCode property
         *         of reason object.
         * @returns {Q.promise} rejected promise
         */
        reject(reason) {
            return Q.reject(reason);
        }

        /**
         * Finish chain for current subcommand and return parent command instance.
         * @returns {COA.Cmd} parent command
         */
        end() {
            return this._cmd;
        }
    };