electra2020.js 29 KB

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