summaryrefslogtreecommitdiff
path: root/node_modules/mongoose/lib/types/number.js
blob: b2a69b612ea88f72281c2390cbbc0e17ce2ae518 (plain)
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
/**
 * Module dependencies.
 */

/**
 * MongooseNumber constructor.
 *
 * @param {Object} value to pass to Number
 * @param {Document} parent document
 * @deprecated (remove in 3.x)
 * @api private
 */

function MongooseNumber (value, path, doc) {
  var number = new Number(value);
  number.__proto__ = MongooseNumber.prototype;
  number._atomics = {};
  number._path = path;
  number._parent = doc;
  return number;
};

/**
 * Inherits from Number.
 */

MongooseNumber.prototype = new Number();

/**
 * Atomic increment
 *
 * @api public
 */

MongooseNumber.prototype.$inc =
MongooseNumber.prototype.increment = function increment (value) {
  var schema = this._parent.schema.path(this._path)
    , value = Number(value) || 1;
  if (isNaN(value)) value = 1;
  this._parent.setValue(this._path, schema.cast(this + value));
  this._parent.getValue(this._path)._atomics['$inc'] = value || 1;
  this._parent._activePaths.modify(this._path);
  return this;
};

/**
 * Returns true if we have to perform atomics for this, and no normal
 * operations
 *
 * @api public
 */

MongooseNumber.prototype.__defineGetter__('doAtomics', function () {
  return Object.keys(this._atomics).length;
});

/**
 * Atomic decrement
 *
 * @api public
 */

MongooseNumber.prototype.decrement = function(){
  this.increment(-1);
};

/**
 * Re-declare toString (for `console.log`)
 *
 * @api public
 */

MongooseNumber.prototype.inspect =
MongooseNumber.prototype.toString = function () {
  return String(this.valueOf());
};


/**
 * Module exports
 */

module.exports = MongooseNumber;