Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. // @package analysis.js
  2. // @note analysis Model
  3.  
  4. // note, using es5 for server compatibility (not that it has to run on the server, but we run `node file.js` to debug and test)
  5. var _ = require('lodash');
  6. var Model = require('ampersand-model');
  7. var PouchDocument = require('./pouch-document'); // &-Model with _id & _rev props
  8. var isoDateMixin = require('ampersand-state-mixin-datatype-iso-date');
  9. var md5 = require('md5');
  10.  
  11. // alternatively could do Model.extend(PouchDocument, { ... our new vals ... });
  12. var Analysis = PouchDocument.extend(isoDateMixin, {
  13. props: {
  14. // _id & _rev specified from PouchDocument
  15. consortiumId: {
  16. type: 'string',
  17. required: true
  18. },
  19. sha: ['string', true], // string, required
  20. complete: ['iso-date', true], // forces dates in ISO 8601 long string
  21. result: ['any', true] // ? different analysis may need different formats
  22. },
  23. derived: {
  24. _id: {
  25. // id should be a unique value representing this file in this consoritum
  26. deps: ['sha', 'consortiumId'],
  27. fn: function() { // note, Model _caches_ derived attributes :)
  28. return md5(this.sha + this.consortiumId)
  29. }
  30. }
  31. },
  32. /**
  33. * Extend serialize to get derived attrs on the cloned obj
  34. **/
  35. serialize: function() {
  36. var tempSerialized = Model.prototype.serialize.call(this);
  37. _.extend(tempSerialized, {
  38. _id: this._id
  39. });
  40. return tempSerialized;
  41. }
  42. });
  43.  
  44. module.exports = Analysis;
  45.  
  46. var a1;
  47. try {
  48. a1 = new Analysis({
  49. _someInvalidProp: 123,
  50. _id: 23 // shouldnt be a number,
  51. });
  52. console.dir(a1.serialize());
  53. } catch(err) {
  54. console.error(err.message);
  55. // pass
  56. }
  57. var a2 = new Analysis({
  58. consortiumId: 'abc',
  59. sha: '123',
  60. // complete: 'Tue Sep 01 2015 14:01:56 GMT-0700', // makes a biiig fuss if not in ISO 8601 format
  61. complete: require('moment')().format(),
  62. result: { bananas: 'oranges' },
  63. _rev: 'asdf923-sd-2'
  64. });
  65.  
  66. console.dir(a2.serialize());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement