# Code coverage report for node-mongoskin/lib/utils.js

## Statements: 98.98% (97 / 98) Branches: 91.49% (43 / 47) Functions: 100% (18 / 18) Lines: 100% (96 / 96) Ignored: none

All files » node-mongoskin/lib/ » utils.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

    1
    1
    1
    1
    1
    1

    1

    1
    5
    5
    1

    4

    6
    1
    44
    44
    44
    1
    1
    1

    43

    44
    44
    44
    44

    6

    1
    175
    175
    175

    32
    32

    143

    175
    167
    8
    6
    6

    6
    1

    6
    167
    95
    95
    57

    38
    38
    5

    33

    95

    6
    7
    1

    6
    2

    1
    1
    1

    6
    184

    6
    78

    28
    28

    6
    6

    44
    44
    44
    44
    44
    2

    42
    42

    44

    78

    6
    10
    1
    9
    6
    6
    3
    3
    3
    3

    10
    10

    6
    6
    4
    2
    1

    6
    28

    6

     /*!
     * mongoskin - utils.js
     *
     * Copyright(c) 2011 - 2012 kissjs.org
     * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
     * MIT Licensed
     */

    "use strict";

    /**
     * Module dependencies.
     */

    var __slice = Array.prototype.slice;
    var EventEmitter = require('events').EventEmitter;
    var constant = require('./constant');
    var STATE_CLOSE = constant.STATE_CLOSE;
    var STATE_OPENNING = constant.STATE_OPENNING;
    var STATE_OPEN = constant.STATE_OPEN;

    exports.makeSkinClass = function makeSkinClass(NativeClass, useNativeConstructor) {

      function onError (err, args, name) {
        var cb = args.pop();
        if (cb && typeof cb === 'function') {
          cb(err);
        } else {
          console.error("Error occured with no callback to handle it while calling " + name,  err);
        }
      };

      var skinClassName = 'Skin' + NativeClass.name;
      function SkinClass() {
        var args = __slice.call(arguments);
        this._construct_args = args;
        if(useNativeConstructor && arguments.length > 0) {
          args.unshift(null);
          var ctor = NativeClass.bind.apply(NativeClass, args);
          this._native = new ctor();
        } else {
          this._native = null;
        }
        this._emitter = new EventEmitter();
        this._emitter.setMaxListeners(50);
        this._state = STATE_CLOSE;
        this._init && this._init();
      }
      SkinClass._class_name = skinClassName;

      function bindSkin(propName) {
        var fn;
        var desc = Object.getOwnPropertyDescriptor(NativeClass.prototype, propName);
        if(!desc) {
          // console.log('no desc', skinClassName, propName, desc);
          try{
            fn = NativeClass.prototype[propName];
          } catch(e) {}
        } else {
          fn = desc.value;
        }
        if(typeof fn == 'function') {
          SkinClass._bindMethod(propName);
        } else if(desc) {
          Eif (desc.get) {
            SkinClass._bindGetter(propName);
          }
          if (desc.set) {
            SkinClass._bindSetter(propName);
          }
        // } else {
        //   this will never be called, so comment it.
        //   console.log('no desc and no value', skinClassName, propName);
        }
      }

      SkinClass._bindMethod = function(propName) {
        SkinClass.prototype[propName] = function() {
          var args = __slice.apply(arguments);
          if (this._state == STATE_OPEN) {
            this._native[propName].apply(this._native, args);
          } else {
            this.open(function(err, p_native) {
                if (err) {
                  onError(err, args, skinClassName + '.' + propName);
                } else {
                  p_native[propName].apply(p_native, args);
                }
            });
          }
          return this;
        }
      }

      SkinClass._bindGetter = function(propName) {
          SkinClass.prototype.__defineGetter__(propName, function() {
              return this._native && this._native[propName];// || this['_prop_' + propName];
          });
      }

      SkinClass._bindSetter = function(propName) {
          SkinClass.prototype.__defineSetter__(propName, function(value) {
              // this['_prop_' + propName] = value;
              this.open(function(err, p_native) {
                  Iif(err) return onError(err, args, skinClassName + '.' + propName);
                  p_native[propName] = value;
              });
          });
      }

      for(var propName in NativeClass.prototype) {
        if(propName[0] != '_') bindSkin(propName);
      }

      SkinClass.prototype.open = function(callback) {
        switch (this._state) {
          case STATE_OPEN:
            callback(null, this._native);
            break;
          case STATE_OPENNING:
            this._emitter.once('open', callback);
            break;
          default:
            this._emitter.once('open', callback);
            this._state = STATE_OPENNING;
            var self = this;
            this._open(function(err, p_native) {
                if (err) {
                  self._state = STATE_CLOSE;
                } else {
                  self._state = STATE_OPEN;
                  self._native = p_native;
                }
                self._emitter.emit('open', err, p_native);
            });
        }
        return this;
      }

      SkinClass.prototype.close = function (callback) {
        if (this._state === STATE_CLOSE) {
          callback && callback();
        } else if (this._state === STATE_OPEN) {
          this._state = STATE_CLOSE;
          this._close(callback);
        } else Eif (this._state === STATE_OPENNING) {
          var self = this;
          this._emitter.once('open', function (err, db) {
              self.close(callback);
          });
        }
        this._native = null;
        return this;
      }

      SkinClass.prototype._close = function(callback) {
        if(this._native.close) {
          this._native.close(callback)
        } else if(callback) {
          callback();
        }
      }

      SkinClass.prototype.isOpen = function() {
        return this._state === STATE_OPEN;
      }

      return SkinClass;

    }