Passed
Pull Request — master (#92)
by Mark
01:32
created

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

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 38
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 3
mnd 2
bc 2
fnc 1
bpm 2
cpm 3
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A MorphTo.setFillPropertyOnParent 0 17 3
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 globalThis.Store.classInstances[type].constructor.find(id);
16
    }
17
18
    protected setFillPropertyOnParent(): void {
19
        Object.defineProperty(this.$parent,
20
            `_${this.name}`,
21
            {
22
                set: (value) => {
23
                    if (!Array.isArray(value)) {
24
                        value = [value];
25
                    }
26
27
                    const name = this.$parent.snakeCaseClassName;
28
                    const type = `${name}_type`;
29
                    const id = `${name}_id`;
30
31
                    for (const record of value) {
32
                        record['id'] = record[id];
33
                        globalThis.Store.classInstances[record[type]].constructor.insert(record);
34
                    }
35
                }
36
            });
37
    }
38
}