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

## Statements: 91.8% (56 / 61) Branches: 79.31% (23 / 29) Functions: 86.67% (13 / 15) Lines: 94.55% (52 / 55) 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
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    1821

    1

    666

    52

    52
    52

    33481

    33481
    33481
    34

    33481
    33481

    15

    71

    71
    71
    20

    71

    71
    71

    71

    71

    635

    635
    635
    17678
    17678
    17678
    17678
    17678
    17678

    1082

    1082

    469
    469

    1082

    1082

    447
    447

    447
    447

    557

    557
    556
    1921
    1921

    557
    2
    2

    447

    87
    18137

     var BaseAccessor = require("./base");

    module.exports = BaseAccessor.extend({

      /**
       */

      _getters: {},

      /**
       */

      _setters: {},

      /**
       */

      _watchers: [],

      /**
       */

      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];

        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];

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

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

        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();
        }
      }
    });