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';
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) {
return this.read(uri).then((json) => {
@ -41,45 +50,28 @@ export class Resource {
}
constructor() {
this._instanceUuid = uuid();
this._uri = undefined;
this._uuid = undefined;
super();
this.instanceUuid = uuid();
}
fromJSON({uri, uuid}) {
this._uri = uri;
this._uuid = uuid;
if (uri) {
this.uri = uri;
}
if (uuid) {
this.uuid = uuid;
}
return this;
}
get instanceUuid() {
return this._instanceUuid;
}
regenerateUuid() {
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;
this.uuid = uuid();
}
toJSON() {
return {
uuid: this._uuid,
uri: this._uri,
uuid: this.uuid,
uri: this.uri,
};
}