electra2020.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. var EventEmitter = require("events").EventEmitter
  2. var _ = require("lodash");
  3. var async = require("async");
  4. function uberfactory(uuid, slugger) {
  5. var ignores = [
  6. "_isdirty",
  7. "uninherits",
  8. "inherits",
  9. "unextends",
  10. "extends",
  11. "unisa",
  12. "isa",
  13. "relatesAsToWith",
  14. "test",
  15. "emita",
  16. "emitas",
  17. "data2",
  18. "getStuff",
  19. "getStuff2",
  20. "getStuff3",
  21. "pingup",
  22. "data",
  23. "metas2",
  24. "metas",
  25. "metas3",
  26. "exporta",
  27. "pickdeep",
  28. "withAllSync",
  29. "withAll",
  30. "innerExport",
  31. "exportFlatSync",
  32. "exportSync",
  33. "export2",
  34. "export",
  35. "domain",
  36. "_events",
  37. "rawListeners",
  38. "_maxListeners",
  39. "setMaxListeners",
  40. "getMaxListeners",
  41. "emit",
  42. "addListener",
  43. "on",
  44. "prependListener",
  45. "once",
  46. "prependOnceListener",
  47. "removeListener",
  48. "removeAllListeners",
  49. "listeners",
  50. "listenerCount",
  51. "eventNames",
  52. "_data", "path", "name", "children", "parents", "_inherits", "_extends", "_isa",
  53. "_hasa", "_relations", "_references", "_created", "_metas",
  54. "_rootname", "exportSerialised"
  55. ]
  56. return function factory(therootname) {
  57. var seed = therootname;
  58. var ROOT = new electra(therootname, true, false)
  59. function inner(name) {
  60. if (typeof(name) == "undefined") {
  61. return ROOT;
  62. }
  63. name = slugger(name);
  64. if (!ROOT.children[name]) {
  65. ROOT.children[name] = new electra(name);
  66. }
  67. return ROOT.children[name]
  68. }
  69. function electra(name, isroot, parent) {
  70. //name = slugger(name);
  71. this._data = {}
  72. this._metas = {};
  73. this.path = (parent ? parent.path : "") + name;
  74. this.name = name;
  75. //this._id = uuid();
  76. this.children = {};
  77. this.parents = {};
  78. this._inherits = {};
  79. this._extends = {};
  80. this._isa = {};
  81. this._hasa = {};
  82. this._relations = {};
  83. this._references = {};
  84. this._created = new Date();
  85. this._rootname = therootname;
  86. var self = this;
  87. if (parent) {
  88. this.parents[parent.name] = parent;
  89. this.path = parent.path + "/" + name
  90. if(parent._metas.copyonchild){
  91. _.map(parent._metas.copyonchild,function(k){
  92. self._metas[k] = parent._metas[k];
  93. })
  94. }
  95. }
  96. if (isroot) {
  97. this.isroot = true;
  98. return this;
  99. }
  100. function xinner(name) {
  101. var args = Array.prototype.slice.apply(arguments, [0]);
  102. if (args.length > 1) {
  103. return args.map(function(n) {
  104. return xinner(n)
  105. })
  106. }
  107. if (typeof(name) == "undefined") {
  108. return self;
  109. }
  110. name = slugger(name);
  111. if (name.indexOf("://") > -1) {
  112. if (!self.children[name]) {
  113. self.children[name] = new electra(name, false, self);
  114. self.pingup("created", self.children[name](), name)
  115. self.emit("added", name);
  116. }
  117. return self.children[name]
  118. }
  119. if (name.indexOf("/") > -1) {
  120. //its a path;
  121. var arr = name.split("/");
  122. var newname = arr.shift();
  123. if (!self.children[newname]) {
  124. self.children[newname] = new electra(newname, false, self);
  125. self.pingup("created", self.children[newname](), newname, arr)
  126. self.emit("added", newname);
  127. }
  128. return self.children[newname](arr.join("/"))
  129. }
  130. if (!self.children[name]) {
  131. self.children[name] = new electra(name, false, self);
  132. self.pingup("created", self.children[name](), name)
  133. }
  134. return self.children[name]
  135. }
  136. return xinner
  137. }
  138. electra.prototype.__proto__ = EventEmitter.prototype
  139. electra.prototype.inherits = function(obj_) {
  140. var obj = typeof(obj_) === "function" ? obj_() : typeof(obj_) === "object" ? obj_ : typeof(obj_) === "string" ? inner(obj_)() : false
  141. if (obj) {
  142. if(obj == this){
  143. return this;
  144. }
  145. this._inherits[obj.path] = obj;
  146. obj._extends[this.path] = this;
  147. }
  148. return this;
  149. }
  150. electra.prototype.uninherits = function(obj_) {
  151. var obj = typeof(obj_) === "function" ? obj_() : typeof(obj_) === "object" ? obj_ : typeof(obj_) === "string" ? inner(obj_)() : false
  152. if (obj && this._inherits[obj.path]) {
  153. delete this._inherits[obj.path]
  154. delete obj._extends[this.path]
  155. }
  156. return this;
  157. }
  158. electra.prototype.extends = function(obj_) {
  159. var obj = typeof(obj_) === "function" ? obj_() : typeof(obj_) === "object" ? obj_ : typeof(obj_) === "string" ? inner(obj_)() : false
  160. if (obj) {
  161. if(obj == this){
  162. return this;
  163. }
  164. this._extends[obj.path] = obj;
  165. obj._inherits[this.path] = this;
  166. }
  167. return this;
  168. }
  169. electra.prototype.unextends = function(obj_) {
  170. var obj = typeof(obj_) === "function" ? obj_() : typeof(obj_) === "object" ? obj_ : typeof(obj_) === "string" ? inner(obj_)() : false
  171. if (obj && this._extends[obj.path]) {
  172. delete this._extends[obj.path]
  173. delete obj._inherits[this.path]
  174. }
  175. return this;
  176. }
  177. electra.prototype.isa = function(elef) {
  178. var self = this;
  179. var args = Array.prototype.slice.apply(arguments, [0]);
  180. if (args.length > 1) {
  181. args.map(function(n) {
  182. var ele = typeof(n) === "function" ? n() : n;
  183. self._isa[ele.path] = ele
  184. ele._hasa[self.path] = self;
  185. self.pingup("isa", self, ele.path)
  186. ele.pingup("isa", self, ele.path)
  187. })
  188. } else {
  189. if (typeof(elef) === "object" && elef.length) {
  190. elef.map(function(n) {
  191. var ele = typeof(n) === "function" ? n() : n;
  192. self._isa[ele.path] = ele
  193. ele._hasa[self.path] = self;
  194. self.pingup("isa", self, ele.path)
  195. ele.pingup("isa", self, ele.path)
  196. })
  197. } else {
  198. var ele = typeof(elef) === "function" ? elef() : typeof(elef) === "object" ? elef : typeof(elef) === "string" ? inner(elef)() : false
  199. if (ele) {
  200. this._isa[ele.path] = ele
  201. ele._hasa[this.path] = this;
  202. self.pingup("isa", self, ele.path)
  203. ele.pingup("isa", self, ele.path)
  204. }
  205. }
  206. }
  207. return this;
  208. }
  209. electra.prototype.unisa = function(elef) {
  210. var self = this;
  211. var args = Array.prototype.slice.apply(arguments, [0]);
  212. if (args.length > 1) {
  213. args.map(function(n) {
  214. var ele = typeof(n) === "function" ? n() : n;
  215. delete self._isa[ele.path]
  216. delete ele._hasa[self.path]
  217. self.pingup("isa", self, ele.path)
  218. ele.pingup("isa", self, ele.path)
  219. })
  220. } else {
  221. if (typeof(elef) === "object" && elef.length) {
  222. elef.map(function(n) {
  223. var ele = typeof(n) === "function" ? n() : n;
  224. delete self._isa[ele.path]
  225. delete ele._hasa[self.path]
  226. self.pingup("isa", self, ele.path)
  227. ele.pingup("isa", self, ele.path)
  228. })
  229. } else {
  230. var ele = typeof(elef) === "function" ? elef() : elef;
  231. delete this._isa[ele.path]
  232. delete ele._hasa[this.path]
  233. self.pingup("isa", self, ele.path)
  234. ele.pingup("isa", self, ele.path)
  235. }
  236. }
  237. return this;
  238. }
  239. electra.prototype.relatesAsToWith = function(relationout, elef, relationto, withstuff) {
  240. var self = this;
  241. if (typeof(relationto) === "undefined") {
  242. relationto = relationout;
  243. }
  244. if (typeof(withstuff) === "undefined") {
  245. withstuff = "";
  246. }
  247. var withstuffOut = Object.assign({
  248. "out": relationout,
  249. "in": relationto
  250. }, withstuff);
  251. var withstuffTo = Object.assign({
  252. "in": relationout,
  253. "out": relationto
  254. }, withstuff);
  255. if (typeof(elef) === "object" && elef.length) {
  256. elef.map(function(n) {
  257. var ele = typeof(n) === "function" ? n() : n;
  258. self._relations[relationout] = self._relations[relationout] || {}
  259. self._relations[relationout][ele.path] = {
  260. ref: ele,
  261. context: withstuffOut
  262. }
  263. ele._relations[relationto] = ele._relations[relationto] || {}
  264. ele._relations[relationto][self.path] = {
  265. ref: self,
  266. context: withstuffTo
  267. }
  268. })
  269. } else {
  270. var ele = typeof(elef) === "function" ? elef() : elef;
  271. self._relations[relationout] = self._relations[relationout] || {}
  272. self._relations[relationout][ele.path] = {
  273. ref: ele,
  274. context: withstuffOut
  275. }
  276. ele._relations[relationto] = ele._relations[relationto] || {}
  277. ele._relations[relationto][self.path] = {
  278. ref: self,
  279. context: withstuffTo
  280. }
  281. }
  282. return this;
  283. }
  284. electra.prototype.test = function() {
  285. return this;
  286. }
  287. electra.prototype.emita = function() {
  288. var self = this;
  289. var args = Array.prototype.slice.apply(arguments, [0]);
  290. var kk = _.keys(this.parents);
  291. if (kk.length > 0) {
  292. kk.map(function(k) {
  293. self.parents[k].emita.apply(self.parents[k], args)
  294. })
  295. } else {}
  296. var kk = _.keys(this.children);
  297. if (kk.length > 0) {
  298. kk.map(function(k) {
  299. //self.children[k].emita.apply(self.children[k],args)
  300. })
  301. } else {}
  302. /* _.map(this.children,function(child,name){
  303. child().emita.apply(child(),args);
  304. }) **/
  305. }
  306. electra.prototype.emitas = function() {
  307. var self = this;
  308. var args = Array.prototype.slice.apply(arguments, [0]);
  309. var kk = _.keys(this.parents);
  310. if (kk.length > 0) {
  311. kk.map(function(k) {
  312. self.parents[k].emita.apply(self.parents[k], args)
  313. })
  314. } else {}
  315. var kk = _.keys(this.children);
  316. if (kk.length > 0) {
  317. kk.map(function(k) {
  318. //self.children[k].emita.apply(self.children[k],args)
  319. })
  320. } else {}
  321. /* _.map(this.children,function(child,name){
  322. child().emita.apply(child(),args);
  323. }) **/
  324. }
  325. electra.prototype.data2 = function(key, value) {
  326. var self = this;
  327. if (typeof(key) === "undefined") {
  328. return this._data;
  329. } else {
  330. if (typeof(value) === "undefined") {
  331. if (typeof(key) === "object") {
  332. var oldvalue = this._data;
  333. this._data = Object.assign(this._data, key);
  334. _.map(this.parents, function(par, nam) {
  335. par.emit("changed", self, "[[object]]", self._data, oldvalue);
  336. })
  337. this.emit("changed", this, "[[object]]", this._data, oldvalue);
  338. return this;
  339. } else {
  340. return this._data[key];
  341. }
  342. } else {
  343. var oldvalue = this._data[key];
  344. this._data[key] = value;
  345. _.map(this.parents, function(par, nam) {
  346. par.emit("changed", self, key, value, oldvalue);
  347. })
  348. this.emit("changed", this, key, value, oldvalue);
  349. return this._data[key];
  350. }
  351. }
  352. }
  353. electra.prototype.getStuff = function(stuff, applybefore, named_, dd) {
  354. var named = named_ || "data";
  355. var dd = dd || false;
  356. var obj = {
  357. name: this.path,
  358. from: named,
  359. data: applybefore(getpath(this, stuff))
  360. }
  361. var arr = [obj];
  362. if (dd) {
  363. return arr
  364. }
  365. arr = [].concat.apply(arr, _.map(this._inherits, function(par, nam) {
  366. return par.getStuff.apply(par, [stuff, applybefore, "inherit_" + named_]);
  367. }))
  368. arr = [].concat.apply(arr, _.map(this.parents, function(par, nam) {
  369. return par.getStuff.apply(par, [stuff, applybefore, "parent_" + named_]);
  370. }))
  371. arr = [].concat.apply(arr, _.map(this._isa, function(par, nam) {
  372. return par.getStuff.apply(par, [stuff, applybefore, "isa_" + named_, true]);
  373. }))
  374. arr = arr.filter(function(a) {
  375. return a.data ? (typeof(a.data) === "function" || a.data.length > 0) : false
  376. });
  377. return arr
  378. }
  379. electra.prototype.getStuff2 = function(stuff, applybefore, named_, dd, inas) {
  380. var self = this;
  381. var named = named_ || "data";
  382. var dd = dd || false;
  383. var inass = inas || [];
  384. var obj = {
  385. name: this.path,
  386. from: named,
  387. data: applybefore(getpath(this, stuff)),
  388. inas: inass
  389. }
  390. var arr = [obj];
  391. if (dd) {
  392. return arr
  393. }
  394. arr = [].concat.apply(arr, _.map(this._inherits, function(par, nam) {
  395. return par.getStuff2.apply(par, [stuff, applybefore, "inherit_" + named_, false, inass.concat([self.path])]);
  396. }))
  397. arr = [].concat.apply(arr, _.map(this.parents, function(par, nam) {
  398. return par.getStuff2.apply(par, [stuff, applybefore, "parent_" + named_, false, inass.concat([self.path])]);
  399. }))
  400. arr = [].concat.apply(arr, _.map(this._isa, function(par, nam) {
  401. return par.getStuff2.apply(par, [stuff, applybefore, "isa_" + named_, false, inass.concat([self.path])]);
  402. }))
  403. arr = arr.filter(function(a) {
  404. return a.data ? (typeof(a.data) === "function" || a.data.length > 0) : false
  405. });
  406. return arr
  407. }
  408. electra.prototype.getStuff3 = function(stuff, applybefore, named_, dd) {
  409. var named = named_ || "data";
  410. var dd = dd || false;
  411. var obj = {
  412. name: this.path,
  413. from: named,
  414. data: applybefore(getpath(this, stuff))
  415. }
  416. var arr = [obj];
  417. if (dd) {
  418. return arr
  419. }
  420. arr = [].concat.apply(arr, _.map(this._inherits, function(par, nam) {
  421. return par.getStuff.apply(par, [stuff, applybefore, "inherit_" + named_]);
  422. }))
  423. arr = [].concat.apply(arr, _.map(this.parents, function(par, nam) {
  424. return par.getStuff.apply(par, [stuff, applybefore, "parent_" + named_]);
  425. }))
  426. arr = arr.filter(function(a) {
  427. return a.data ? (typeof(a.data) === "function" || a.data.length > 0) : false
  428. });
  429. return arr
  430. }
  431. electra.prototype.pingup = function() {
  432. var args = Array.prototype.slice.apply(arguments, [0]);
  433. _.map(this.parents, function(par, nam) {
  434. return par.pingup.apply(par, args);
  435. })
  436. this.emit.apply(this, args);
  437. }
  438. electra.prototype.data = function(key, value) {
  439. var self = this;
  440. if (typeof(key) === "undefined") {
  441. return this._data;
  442. } else {
  443. if (typeof(value) === "undefined") {
  444. if (typeof(key) === "object") {
  445. var oldvalue = this._data;
  446. this._data = Object.assign(this._data, key);
  447. this.pingup("changed", self, "[[object]]", self._data, oldvalue)
  448. return this;
  449. } else {
  450. return this._data[key];
  451. }
  452. } else {
  453. var oldvalue = getpath(this._data, key); //this._data[key];
  454. setpath(this._data, key, value)
  455. // this._data[key] = value;
  456. this.pingup("changed", self, key, value, oldvalue)
  457. return getpath(this._data, key); //this._data[key];
  458. }
  459. }
  460. }
  461. electra.prototype.metas = function(key, value) {
  462. var self = this;
  463. if (typeof(key) === "undefined") {
  464. return this._metas;
  465. } else {
  466. if (typeof(value) === "undefined") {
  467. if (typeof(key) === "object") {
  468. var oldvalue = this._metas;
  469. this._metas = Object.assign(this._metas, key);
  470. this.pingup("changed:meta", self, "[[object]]", self._metas, oldvalue)
  471. return this;
  472. } else {
  473. return this._metas[key];
  474. }
  475. } else {
  476. var oldvalue = this._metas[key];
  477. this._metas[key] = value;
  478. this.pingup("changed:meta", self, key, value, oldvalue)
  479. return this._metas[key];
  480. }
  481. }
  482. }
  483. /* electra.prototype.metas = function() {
  484. var self = this;
  485. var args = Array.prototype.slice.apply(arguments, [0]);
  486. if (args.length === 0) {
  487. return this._metas;
  488. }
  489. args.map(function(obj_) {
  490. var obj = typeof(obj_) === "function" ? obj_() : typeof(obj_) === "object" ? obj_ : typeof(obj_) === "string" ? inner(obj_)() : false
  491. if (obj) {
  492. self._metas.push({
  493. "meta": obj,
  494. "data": {}
  495. })
  496. }
  497. })
  498. return this;
  499. }
  500. electra.prototype.metas3 = function(arr) {
  501. var self = this;
  502. self._metas = self._metas.concat(arr);
  503. return self;
  504. }
  505. */
  506. electra.prototype.exporta = function(path) {
  507. this.export(path, function(err, arr) {
  508. // console.log("EXPORTED", err, JSON.stringify(arr, true, 2));
  509. })
  510. }
  511. electra.prototype.pickdeep = function(prop, cb) {
  512. var self = this;
  513. async.mapLimit(this.children, 10, function(obj, next) {
  514. //setTimeout(function(){
  515. obj().pickdeep(prop, next)
  516. //},1)
  517. // next(null,)
  518. }, function(err, collect) {
  519. cb(err, [].concat.apply([self[prop]], collect));
  520. })
  521. return this;
  522. }
  523. electra.prototype.withAllSync = function(func, cb) {
  524. var self = this;
  525. async.mapLimit(this.children, 10, function(obj, next) {
  526. //setTimeout(function(){
  527. obj().withAllSync(func, next)
  528. //},1)
  529. // next(null,)
  530. }, function(err, collect) {
  531. cb(err, [].concat.apply([func(self)], collect));
  532. })
  533. return this;
  534. }
  535. electra.prototype.withAll = function(func, cb) {
  536. var self = this;
  537. async.mapLimit(this.children, 10, function(obj, next) {
  538. obj().withAll(func, next)
  539. }, function(err, collect) {
  540. func(self, function(err2, result) {
  541. cb(err || err2, [].concat.apply([result], collect));
  542. })
  543. })
  544. return this;
  545. }
  546. electra.prototype.exportSync = function(path, serializers) {
  547. var self = this;
  548. if (self._metas.unsaveable) {
  549. return undefined;
  550. }
  551. var obj = self.innerExport(path, serializers);
  552. var collect = _.map(self.children, function(child, name) {
  553. return child().exportSync(path + "/" + name, serializers)
  554. })
  555. collect.sort(function(a, b) {
  556. return a.name > b.name ? 1 : a.name < b.name ? -1 : 0
  557. })
  558. obj.children = collect;
  559. return obj;
  560. }
  561. electra.prototype.exportFlatSync = function(path) {
  562. var self = this;
  563. return [].concat.apply([self.innerExport(path)], _.map(self.children, function(child, name) {
  564. return child().exportFlatSync(path + "/" + name)
  565. }))
  566. }
  567. electra.prototype.export2 = function(path, cb) {
  568. var self = this;
  569. var obj = self.innerExport(path);
  570. obj.children = _.map(self.children, function(a, n) {
  571. return a().path
  572. });
  573. cb(null, obj);
  574. }
  575. electra.prototype.innerExport = function(path, serializers) {
  576. var self = this;
  577. if (self._metas.unsaveable) {
  578. return {};
  579. }
  580. var obj = {};
  581. // obj._id = self._id;
  582. obj.name = self.name;
  583. obj.path = self.path;
  584. obj.created = self._created.toJSON();
  585. obj.data = self._data;
  586. obj.meta = self._metas;
  587. obj.inherits = _.map(self._inherits, function(a, n) {
  588. return a.path
  589. });
  590. obj.extends = _.map(self._extends, function(a, n) {
  591. return a.path
  592. });
  593. /*obj.parents = _.map(self.parents, function(a, n) {
  594. return a.path
  595. });*/
  596. obj.isa = _.map(self._isa, function(a, n) {
  597. return a.path
  598. });
  599. /*obj.hasa = _.map(self._hasa, function(a, n) {
  600. return a.path
  601. });*/
  602. obj.rel = {};
  603. _.map(self._relations, function(a, n) {
  604. obj.rel[n] = {}
  605. _.map(a, function(aa, nn) {
  606. obj.rel[n][aa.ref.path] = aa.context;
  607. });
  608. });
  609. //clean up
  610. obj.inherits = obj.inherits.length ? obj.inherits : undefined;
  611. obj.extends = obj.extends.length ? obj.extends : undefined;
  612. //obj.parents = obj.parents.length ? obj.parents : undefined;
  613. obj.isa = obj.isa.length ? obj.isa : undefined;
  614. //obj.hasa = obj.hasa.length ? obj.hasa : undefined;
  615. obj.data = _.size(obj.data) ? obj.data : undefined;
  616. obj.meta = _.size(obj.meta) ? obj.meta : undefined;
  617. obj.rel = _.size(obj.rel) ? obj.rel : undefined;
  618. _.keys(obj).map(function(k) {
  619. if (obj[k] === undefined) {
  620. delete obj[k];
  621. //obj[k] = null /* nope */
  622. }
  623. })
  624. var therest = _.omit(self, ignores);
  625. //seriadeep(therest, obj, serializers,1);
  626. _.map(therest, function(val, ke) {
  627. if (typeof(val) === "function") {
  628. obj[ke] = "$$$FUNCTION$$$" + val;
  629. } else {
  630. if (serializers && serializers[ke]) {
  631. obj[ke] = serializers[ke](val);
  632. } else {
  633. _.map(val, function(cval, cprop) {})
  634. obj[ke] = val;
  635. }
  636. }
  637. })
  638. return obj
  639. }
  640. function seriadeep(rest, obj, serializers, one) {
  641. if (one > 2) {
  642. return
  643. }
  644. _.map(rest, function(val, ke) {
  645. if (typeof(val) === "function") {
  646. obj[ke] = "$$$FUNCTION$$$" + val;
  647. } else if (typeof(val) === "object") {
  648. obj[ke] = seriadeep(val, obj, serializers, (one || 1) + 1);
  649. } else {
  650. if (serializers && serializers[ke]) {
  651. obj[ke] = serializers[ke](val)
  652. } else {
  653. obj[ke] = val;
  654. }
  655. }
  656. })
  657. }
  658. electra.prototype.exportSerialised = function(serializers) {
  659. var self = this;
  660. return function(path, cb) {
  661. async.mapLimit(this.children, 10, function(obj, next) {
  662. obj().exportSerialised(serializers)(path + obj().name, next)
  663. // next(null,)
  664. }, function(err, collect) {
  665. var obj = self.innerExport(path, serializers);
  666. collect.sort(function(a, b) {
  667. return a.name > b.name ? 1 : a.name < b.name ? -1 : 0
  668. })
  669. obj.children = collect;
  670. cb(err, obj);
  671. })
  672. return this;
  673. }
  674. }
  675. electra.prototype.export = function(path, cb) {
  676. var self = this;
  677. async.mapLimit(this.children, 10, function(obj, next) {
  678. obj().export(path + obj().name, next)
  679. }, function(err, collect) {
  680. var obj = self.innerExport(path);
  681. if (collect.sort) {
  682. collect.sort(function(a, b) {
  683. return a.name > b.name ? 1 : a.name < b.name ? -1 : 0
  684. })
  685. }
  686. obj.children = collect;
  687. cb(err, obj);
  688. })
  689. return this;
  690. }
  691. return inner
  692. }
  693. }
  694. function getpath(obj, path) {
  695. path = path || "";
  696. var arr = path.split(".");
  697. var t = obj;
  698. var done = false;
  699. if (arr.length) {
  700. while (arr.length && !done) {
  701. var check = arr.shift();
  702. if (check !== "") {
  703. t = t[check];
  704. }
  705. if (typeof t !== "object") {
  706. done = true;
  707. }
  708. }
  709. }
  710. return t;
  711. }
  712. function setpath(obj, path, value) {
  713. if (typeof obj !== "object" || !obj) {
  714. throw new Error("obj is not Object");
  715. }
  716. if (typeof path !== "string" || path === "") {
  717. throw new Error("path must be string with length > 0");
  718. }
  719. var arr = path.split(".");
  720. var done = false;
  721. var t = obj;
  722. if (arr.length > 1) {
  723. while (arr.length && t && !done) {
  724. var check = arr.shift();
  725. if (typeof t[check] === "object" && arr.length > 0) {
  726. t = t[check];
  727. } else {
  728. done = true;
  729. arr.unshift(check);
  730. }
  731. }
  732. var xt = t;
  733. while (arr.length) {
  734. var tt = arr.shift();
  735. if (arr.length) { //go deeper
  736. xt = xt[tt] = {};
  737. } else {
  738. //last
  739. xt[tt] = value;
  740. }
  741. }
  742. } else {
  743. if (arr.length === 1 && arr[0] !== "") {
  744. t[arr[0]] = value;
  745. }
  746. }
  747. }
  748. uberfactory.ignores = ignores = [
  749. "_isdirty",
  750. "inherits",
  751. "uninherits",
  752. "unextends",
  753. "extends",
  754. "isa",
  755. "unisa",
  756. "relatesAsToWith",
  757. "test",
  758. "emita",
  759. "emitas",
  760. "data2",
  761. "getStuff",
  762. "getStuff2",
  763. "getStuff3",
  764. "pingup",
  765. "data",
  766. "metas2",
  767. "metas",
  768. "metas3",
  769. "exporta",
  770. "pickdeep",
  771. "withAllSync",
  772. "withAll",
  773. "innerExport",
  774. "exportFlatSync",
  775. "exportSync",
  776. "export2",
  777. "export",
  778. "domain",
  779. "_events",
  780. "rawListeners",
  781. "_maxListeners",
  782. "setMaxListeners",
  783. "getMaxListeners",
  784. "emit",
  785. "addListener",
  786. "on",
  787. "prependListener",
  788. "once",
  789. "prependOnceListener",
  790. "removeListener",
  791. "removeAllListeners",
  792. "listeners",
  793. "listenerCount",
  794. "eventNames",
  795. "_data", "path", "name", "children", "parents", "_inherits", "_extends", "_isa",
  796. "_hasa", "_relations", "_references", "_created", "_metas",
  797. "_rootname", "exportSerialised"
  798. ]
  799. module.exports = uberfactory;