src/Store/Model/Field/MorphTo.ts   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 32
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 2
mnd 2
bc 2
fnc 0
bpm 0
cpm 0
noi 0
1
import Field from "../Field";
2
3
export default class MorphTo extends Field {
4
5
    constructor(name) {
6
        super(name);
7
    }
8
9
    get value() {
10
        const name = this.$parent.snakeCaseClassName;
11
12
        const type = this.$parent[`${name}_type`];
13
        const id = this.$parent[`${name}_id`];
14
15
        return Object.getPrototypeOf(globalThis.Store.classInstances[type]).constructor.find(id);
16
    }
17
18
    set _value(value) {
19
        if (!Array.isArray(value)) {
20
            value = [value];
21
        }
22
23
        const name = this.$parent.snakeCaseClassName;
24
        const type = `${name}_type`;
25
        const id = `${name}_id`;
26
27
        for (const record of value) {
28
            record['id'] = record[id];
29
            Object.getPrototypeOf(globalThis.Store.classInstances[record[type]]).constructor.insert(record);
30
        }
31
    }
32
}