lib/Manager.js   A
last analyzed

Complexity

Total Complexity 23
Complexity/F 1.44

Size

Lines of Code 92
Function Count 16

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 23
c 1
b 0
f 0
nc 1
mnd 1
bc 22
fnc 16
dl 0
loc 92
rs 10
bpm 1.375
cpm 1.4375
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B Manager.js ➔ Manager 0 76 1
1
var Stubs = require('./Stub')
2
var Objects = require('./Utility').Objects
3
4
/**
5
 * Library entrypoint, installs and withdraws global symbols
6
 *
7
 * @param {object} [context] Global value to populate
8
 *
9
 * @class
10
 *
11
 * @implements IStubManager
12
 */
13
function Manager (context) {
14
  var self = this
15
  context = context || global
16
  self._settings = {}
17
  self._stash = null
18
19
  function eachSymbol (callback) {
20
    Object.keys(Stubs).forEach(function (symbol) {
21
      var configuration = self._settings.global || {}
22
      configuration = Objects.merge(configuration, self._settings[symbol] || {})
23
      callback(symbol, Stubs[symbol], configuration)
24
    })
25
  }
26
27
  function stash () {
28
    if (self._stash) {
29
      return
30
    }
31
    self._stash = {}
32
    eachSymbol(function (symbol) {
33
      if (context.hasOwnProperty(symbol)) {
34
        self._stash[symbol] = context[symbol]
35
      }
36
    })
37
  }
38
39
  function unstash () {
40
    if (!self._stash) {
41
      return
42
    }
43
    eachSymbol(function (symbol) {
44
      if (self._stash.hasOwnProperty(symbol)) {
45
        context[symbol] = self._stash[symbol]
46
      } else {
47
        delete context[symbol]
48
      }
49
    })
50
    delete self._stash
51
  }
52
53
  this.setup = function (settings) {
54
    self._settings = settings
55
    eachSymbol(function (symbol, _, configuration) {
56
      if (!context[symbol] || !context[symbol]._setup) {
57
        return
58
      }
59
      context[symbol]._setup(configuration)
60
    })
61
  }
62
63
  this.install = function () {
64
    stash()
65
    eachSymbol(function (symbol, Stub, configuration) {
66
      var stub = typeof Stub === 'function' ? new Stub(configuration) : Stub
67
      stub._setup && stub._setup(configuration)
68
      context[symbol] = stub
69
    })
70
  }
71
72
  this.reset = function () {
73
    eachSymbol(function (symbol) {
74
      context[symbol] && context[symbol]._reset && context[symbol]._reset()
75
    })
76
  }
77
78
  this.flush = function () {
79
    eachSymbol(function (symbol) {
80
      context[symbol] && context[symbol]._flush && context[symbol]._flush()
81
    })
82
  }
83
84
  this.uninstall = function () {
85
    self.flush()
86
    unstash()
87
  }
88
}
89
90
module.exports = {
91
  Manager: Manager
92
}
93