electra2020.js 30 KB

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