# Code coverage report for lib/accessors/pojo.js

## Statements: 93.1% (54 / 58) Branches: 77.78% (21 / 27) Functions: 87.5% (14 / 16) Lines: 96.15% (50 / 52) Ignored: none

All files » lib/accessors/ » pojo.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
    1631

    1
    178
    178

    1

    289

    24

    24
    24

    584

    584
    584
    167

    584
    584

    5

    67

    67

    67

    67

    214

    214
    214
    163
    163
    163
    163
    163
    163

    230

    230

    36
    36

    230

    230

    16
    16

    16
    16

    5

    5
    4
    13
    13

    5
    2
    2

    16

    82
    156

     var BaseAccessor = require("./base"),
    _set           = require("../utils/set");

    function POJOAccessor () {
      this._getters = {};
      this._watchers = [];
    }

    module.exports = BaseAccessor.extend(POJOAccessor, {

      /**
       */

      castObject: function(object) { return object; },

      /**
       */

      call: function(object, path, params) {

        var fnName = path.pop(),
        fnCtx      = path.length ? this.get(object, path) : object;

        Iif (!fnCtx) return;
        return fnCtx[fnName].apply(fnCtx, params);
      },

      /**
       */

      get: function(object, path) {

        if (typeof path === "string") path = path.split(".");

        var pt = path.join("."), getter;
        if (!(getter = this._getters[pt])) {
          getter = this._getters[pt] = new Function("return this." +pt);
        }

        // is undefined - fugly, but works for this test.
        try {
          return getter.call(object);
        } catch (e) {
          return void 0;
        }
      },

      /**
       */

      set: function(object, path, value) {

        if (typeof path === "string") path = path.split(".");

        var ret = _set(object, path, value);

        this.apply();

        return ret;
      },

      /**
       */

      watchProperty: function(object, path, listener) {
        var self = this;

        // assign a value to bypass the first trigger.
        var currentValue, firstCall = true;
        return this._addWatcher(function () {
          var newValue = self.get(object, path);
          Iif (!firstCall && newValue === currentValue && typeof newValue !== "function") return;
          firstCall = true;
          var oldValue = currentValue;
          currentValue = newValue;
          listener(newValue, currentValue);
        });
      },

      /**
       */

      _addWatcher: function (applyChanges) {

        var self = this;

        var watcher = {
          apply: applyChanges,
          trigger: applyChanges,
          dispose: function () {
            var i = self._watchers.indexOf(watcher);
            Eif (~i) self._watchers.splice(i, 1);
          }
        };

        this._watchers.push(watcher);

        return watcher;
      },

      /**
       */

      watchEvent: function(object, event, listener) {

        Eif (Object.prototype.toString.call(object) === "[object Array]" && event === "change") {
          return this._watchArrayChangeEvent(object, listener);
        }

        return {
          dispose: function(){ }
        };
      },

      /**
       */

      _watchArrayChangeEvent: function (object, listener) {
        var copy = object.concat();
        return this._addWatcher(function () {

          var hasChanged = object.length !== copy.length;

          if (!hasChanged) {
            for (var i = 0, n = copy.length; i < n; i++) {
              hasChanged = (copy[i] !== object[i]);
              if(hasChanged) break;
            }
          }

          if (hasChanged) {
            copy = object.concat();
            listener();
          }
        });
      },

      /**
       * TODO - deserialize is improper. Maybe use
       *
       */

      normalizeCollection: function(collection) {
        return collection;
      },

      /**
       */

      normalizeObject: function(object) {
        return object;
      },

      /**
       */

      apply: function() {
        for (var i = 0, n = this._watchers.length; i < n; i++) {
          this._watchers[i].apply();
        }
      }
    });