All files Kubik.js

0% Statements 0/35
0% Branches 0/23
0% Functions 0/6
0% Lines 0/29
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                                                                                                                                                                       
const HooksMixin = require('hooks-mixin');
const isFunction = require('lodash/isFunction');
/**
 * abstract App Kubik class
 * The parent of all Kubiks
 * @namespace Rubik
 * @class Kubik
 * @prop {null|Rubik.App} app          application of the kubik
 * @prop {String}         name         name of kubik
 * @prop {Array}          dependencies dependencies of kubik (another kubiks names)
 */
class Kubik {
  constructor() {
    this.app = null;
    // if child prototype will contain name of dependencies
    if (!this.name) this.name = 'kubik';
    if (!this.dependencies) this.dependencies = [];
    this.extensions = [];
  }
 
  /**
   * define property
   * @param  {String}  name           name of property
   * @param  {Mixed}   value          value, or property options
   * @param  {Boolean} [assign=false] use value as options or value
   * @return {Rubik.Kubik}            this
   */
  _define(name, value, assign = false) {
    const props = {};
    if (assign === true && typeof value === 'object') {
      Object.assign(props, value);
    } else {
      props.value = value;
    }
    Object.defineProperty(this, name, props);
    return this;
  }
 
  /**
   * apply hooks and clean it (it was here for backward compatibility)
   * @param  {String}  name hook name
   * @return {Promise}
   */
  async applyHooks(name) {
    if (!this.__hooks[name].length) return;
    await this.processHooksAsync(name);
    this.__hooks[name] = [];
  }
 
  /**
   * up kubik
   * @param {Object} dependencies of kubik
   */
  up(/* dependencies */) {
    throw new TypeError('Up is not implemented in kubik ' + this.name);
  }
 
  /**
   * use extension of kubik
   * @param extension of kubik
   */
  use(extension) {
    if (!extension) return;
    if (isFunction(extension.before)) {
      this.hook('before', extension.before);
    }
    if (isFunction(extension.after)) {
      this.hook('after', extension.after);
    }
    if (extension.hooks) {
      for (const [name, hooks] of Object.entries(extension.hooks)) {
        const addHook = hook => {
          if (isFunction(hook)) this.hook(name, hook);
        }
        if (Array.isArray(hooks)) hooks.forEach(addHook);
        else addHook(hooks);
      }
    }
    this.extensions.push(extension);
  }
}
 
module.exports = HooksMixin(Kubik);