refactor: Resource properties

This commit is contained in:
cha0s 2019-04-28 08:29:01 -05:00
parent 6e5d5384e1
commit f550be1529

View File

@ -1,6 +1,15 @@
import uuid from 'uuid/v4'; import uuid from 'uuid/v4';
export class Resource { import {compose} from '@avocado/core';
import {Property} from '@avocado/mixins';
const decorate = compose(
Property('instanceUuid'),
Property('uri'),
Property('uuid'),
)
export class Resource extends decorate(class {}) {
static load(uri) { static load(uri) {
return this.read(uri).then((json) => { return this.read(uri).then((json) => {
@ -41,45 +50,28 @@ export class Resource {
} }
constructor() { constructor() {
this._instanceUuid = uuid(); super();
this._uri = undefined; this.instanceUuid = uuid();
this._uuid = undefined;
} }
fromJSON({uri, uuid}) { fromJSON({uri, uuid}) {
this._uri = uri; if (uri) {
this._uuid = uuid; this.uri = uri;
}
if (uuid) {
this.uuid = uuid;
}
return this; return this;
} }
get instanceUuid() {
return this._instanceUuid;
}
regenerateUuid() { regenerateUuid() {
this._uuid = uuid(); this.uuid = uuid();
}
get uuid() {
return this._uuid;
}
set uuid(uuid) {
this._uuid = uuid;
}
get uri() {
return this._uri;
}
set uri(uri) {
this._uri = uri;
} }
toJSON() { toJSON() {
return { return {
uuid: this._uuid, uuid: this.uuid,
uri: this._uri, uri: this.uri,
}; };
} }