Completed
Push — master ( d362a9...56faa4 )
by Mark
14s queued 11s
created

src/Store/Model/Relation/BelongTo.ts   A

Complexity

Total Complexity 7
Complexity/F 3.5

Size

Lines of Code 47
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 7
mnd 5
bc 5
fnc 2
bpm 2.5
cpm 3.5
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A BelongsTo.setParentProperties 0 18 2
A BelongsTo.setName 0 5 5
1
import Relation from "../Relation.js";
2
import {ModelStaticInterface} from "../../../JeloquentInterfaces";
3
4
export default class BelongsTo extends Relation {
5
6
    constructor(model: ModelStaticInterface, foreignKey: string|null = null, name: string) {
7
        super(model, (foreignKey ?? `${model.snakeCaseClassName}_id`), name);
8
    }
9
10
    get originalValue() {
11
        return this.model.find(this.$parent[`original_${this.foreignKey}`]);
12
    }
13
14
    get value() {
15
        return this.model.find(this.$parent[this.foreignKey]);
16
    }
17
18
    // eslint-disable-next-line @typescript-eslint/no-empty-function
19
    set value(value) {
20
21
    }
22
23
    setName(): BelongsTo {
24
        const className = this.model.snakeCaseClassName;
25
        this.$name = this.$name ?? `${className}`;
26
        return this;
27
    }
28
29
    protected setParentProperties() {
30
        super.setParentProperties();
31
32
        let name = '';
33
        for (const namePart of this.$name.split('_')) {
34
            name += namePart[0].toUpperCase() + namePart.slice(1);
35
        }
36
37
        Object.defineProperty(this.$parent,
38
            `has${name}`, {
39
                get: () => {
40
                    return this.value !== null;
41
                },
42
            }
43
        )
44
45
        return this;
46
    }
47
}