ResourceIdentity.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. class ResourceIdentity
  3. {
  4. /**
  5. * Constructor.
  6. *
  7. * @param {string} uniqueIdentifier
  8. * @param {string|null} className
  9. */
  10. constructor(uniqueIdentifier, className = null)
  11. {
  12. this.resource = {uniqueIdentifier, className};
  13. }
  14. /**
  15. * Return the unique identifier of the resource.
  16. *
  17. * @return {string}
  18. */
  19. uniqueIdentifier()
  20. {
  21. return this.resource.uniqueIdentifier;
  22. }
  23. /**
  24. * Return the class name of the resource.
  25. *
  26. * @return {string|null}
  27. */
  28. className()
  29. {
  30. return this.resource.className;
  31. }
  32. /**
  33. * Unserialize a resource identity.
  34. *
  35. * @param {Object} identity
  36. * @return {ResourceIdentity}
  37. */
  38. static unserialize(identity)
  39. {
  40. return new ResourceIdentity(identity.id, identity.class_name);
  41. }
  42. /**
  43. * Serialize the resource identity.
  44. *
  45. * @return {Object}
  46. */
  47. serialize()
  48. {
  49. return {
  50. __rialto_resource__: true,
  51. id: this.uniqueIdentifier(),
  52. class_name: this.className(),
  53. };
  54. }
  55. }
  56. module.exports = ResourceIdentity;