-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (44 loc) · 1.25 KB
/
index.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
var debug = require('sherlock')('struct');
var inherits = require('super');
var pathval = require('pathval');
var Schema = exports.Schema = require('./lib/schema').Schema;
var Struct = exports.Struct = function Struct(attr, opts) {
this.__obj = {};
};
Object.defineProperty(Struct, 'type', {
get: function() { return this.prototype.__type; },
enumerable: true
});
Struct.extend = function(type, _schema) {
if (!(type && 'string' === typeof type)) {
_schema = type;
type = 'Struct';
}
var Klass = (function(self, schema) {
return function() {
this.schema = schema;
self.apply(this, arguments);
};
})(this, new Schema(_schema));
inherits.merge([ Klass, this ]);
inherits.inherits(Klass, this);
inherits.merge([ Klass.prototype, this.prototype ]);
Object.defineProperty(Klass, 'type', {
get: function() { return this.prototype.__type; },
enumerable: true
});
Klass.extend = this.extend;
Klass.prototype.__type = type;
Klass.prototype.constructor = Klass;
return Klass;
};
Struct.prototype = {
constructor: Struct,
__type: 'Struct',
get: function(path) {
return pathval.get(this.__obj, path);
},
set: function(path, value) {
return pathval.set(this.__obj, path, value);
}
};