UNPKG

2.6 kBJavaScriptView Raw
1var Definition, bs, defer, deferObject, exports, _;
2
3_ = require('lodash');
4
5bs = require('bs58check');
6
7defer = require('when');
8
9deferObject = require('when/keys');
10
11Definition = (function() {
12 function Definition(custodian, definition) {
13 this.custodian = custodian;
14 if (this.custodian == null) {
15 throw Error('Custodian is required');
16 }
17 if (!(this instanceof Definition)) {
18 return new Definition(this.custodian, definition);
19 } else if (definition instanceof Definition) {
20 return definition;
21 } else if (typeof definition === 'object') {
22 this._definition = defer(definition);
23 } else if (typeof definition === 'string') {
24 this._definition = this.custodian.data.get(bs.decode(definition));
25 } else {
26 throw Error("Invalid definition " + (typeof definition) + ": " + definition);
27 }
28 }
29
30 Definition.prototype.child = function(key) {
31 return this.children().then(children)(function() {
32 return Definition(this.custodian, children[key]);
33 });
34 };
35
36 Definition.prototype.children = function() {
37 return this._children != null ? this._children : this._children = this._definition.then(function(definition) {
38 var _ref;
39 return deferObject.map((_ref = definition != null ? definition.children : void 0) != null ? _ref : {}, function(child) {
40 return Definition(this.custodian, child).save().then(function(hash) {
41 return bs.encode(hash);
42 });
43 });
44 });
45 };
46
47 Definition.prototype.get = function(key) {
48 if (key != null) {
49 return this.get().then(function(definition) {
50 return definition[key];
51 });
52 } else {
53 return this._data != null ? this._data : this._data = this._definition.then((function(_this) {
54 return function(definition) {
55 return _this.children().then(function(children) {
56 var _ref, _ref1, _ref2, _ref3;
57 return {
58 permissions: (_ref = definition != null ? definition.permissions : void 0) != null ? _ref : 'public',
59 meta: (_ref1 = definition != null ? definition.meta : void 0) != null ? _ref1 : {},
60 form: (_ref2 = definition != null ? definition.form : void 0) != null ? _ref2 : {},
61 schema: (_ref3 = definition != null ? definition.schema : void 0) != null ? _ref3 : {},
62 children: children
63 };
64 });
65 };
66 })(this));
67 }
68 };
69
70 Definition.prototype.save = function() {
71 return this.custodian.data.put(this.get());
72 };
73
74 return Definition;
75
76})();
77
78exports = module.exports = Definition;