resourceful-coder.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. var coder = {};
  2. //
  3. // Remark: Code comments show code that gets generated
  4. //
  5. exports.code = function (resource) {
  6. var str = '';
  7. //
  8. // var Creature = resourceful.define('creature');
  9. //
  10. str += 'var ' + resource._resource + " = resourceful.define('" + resource.lowerResource + "');\n\n";
  11. var schema = resource.schema.properties;
  12. Object.keys(schema).forEach(function(p){
  13. var type = schema[p].type.split('');
  14. type[0] = type[0].toUpperCase();
  15. type = type.join('');
  16. if (p !== 'id') {
  17. //
  18. // Creature.property('awesome', Boolean, {
  19. //
  20. str += resource._resource + '.property("' + p + '", ' + type + ', { \n';
  21. Object.keys(schema[p]).forEach(function(o, i){
  22. var comma = ",";
  23. if(i === Object.keys(schema[p]).length - 4) { // TODO: < 4 is a magic number, thats not right
  24. comma = "";
  25. }
  26. if(o !== "type" && o !== "messages" && o !== 'conditions') {
  27. var value = schema[p][o];
  28. if(Array.isArray(value)) {
  29. value = "['" + value.join("', '") + "']";
  30. } else if (type === "Number"){
  31. // do nothing
  32. } else if (type === "Boolean"){
  33. // do nothing
  34. } else {
  35. value = '"' + value + '"';
  36. }
  37. //
  38. // "default": false,
  39. //
  40. str += ' "' + o + '": ' + value + '' + comma + ' \n';
  41. }
  42. });
  43. //
  44. // });
  45. //
  46. str += '});\n\n'
  47. }
  48. });
  49. //
  50. // For every remote method on the resource
  51. //
  52. for (var m in resource) {
  53. if(typeof resource[m] === "function" && resource[m].remote === true) {
  54. //
  55. // Creature.feed = function () {};
  56. //
  57. str += resource._resource + '.' + m + ' = ' + resource[m].toString() + ';\n';
  58. //
  59. // Creature.feed.remote = true;
  60. //
  61. str += resource._resource + '.remote = true;\n'
  62. }
  63. }
  64. return str;
  65. };