-
Notifications
You must be signed in to change notification settings - Fork 1
/
GdprGroupBuilder.ts
201 lines (181 loc) · 5.75 KB
/
GdprGroupBuilder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { GdprStorage } from "../GdprStorage"
import { GdprManagerBuilder } from "./GdprManagerBuilder"
import { GdprGuard } from "../GdprGuard"
import { GdprGuardGroup } from "../GdprGuardGroup"
import { GdprGuardBuilder } from "./GdprGuardBuilder"
/**
* Builder for a gdpr group
* @class GdprGroupBuilder
* @extends {GdprManagerBuilder}
* @export
*/
class GdprGroupBuilder extends GdprManagerBuilder {
public guards: GdprGuard[] = [];
/**
* @ignore
* @protected
*/
protected constructor(
protected parent: GdprManagerBuilder,
protected name: string,
protected description: string,
storage: GdprStorage,
protected enable: boolean,
protected require: boolean,
) {
super();
this.storage = storage;
if (!!require)
this.enable = true;
}
/**
* Factory for a group builder
* @static
* @param {GdprManagerBuilder} mb The parent manager builder
* @param {string} name The name of the group
* @param {string} [description] The description of the group
* @param {?GdprStorage} [storage] The storage of the group
* @param {boolean} [enabled=true] Whether or not the group should be enabled
* @param {boolean} [required=true] Whether or not the group should be required
* @returns {GdprGroupBuilder}
* @memberof GdprGroupBuilder
*/
public static create(mb: GdprManagerBuilder, name: string, description: string = "", storage: GdprStorage | null = null, enabled: boolean = true, required: boolean = false): GdprGroupBuilder {
return new GdprGroupBuilder(mb, name, description, storage || GdprStorage.Cookie, enabled, required);
}
public startGroup(storage: GdprStorage | null = null, name: string = "", description: string = ""): GdprGroupBuilder {
return super.startGroup(storage || this.parent.storage, name, description);
}
public startRequiredGroup(storage: GdprStorage | null = null, name: string = "", description: string = ""): GdprGroupBuilder {
return this.startGroup(storage, name, description).required();
}
/**
* End the group using the current builder state
* @returns {GdprManagerBuilder}
* @memberof GdprGroupBuilder
*/
public endGroup(): GdprManagerBuilder {
const enable = this.require || this.enable;
const group = GdprGuardGroup.for(this.name, this.description, enable, this.require);
const guards = [...this.guards, ...this.groups];
guards.forEach(guard => group.addGuard(guard));
if (this.require)
group.makeRequired();
this.parent.groups.push(group);
return this.parent;
}
/**
* Set the name of the group
* @param {string} name The new name for the group
* @returns {GdprGroupBuilder}
* @memberof GdprGroupBuilder
*/
public withName(name: string): GdprGroupBuilder {
return this.edit(b => b.name = name);
}
/**
* Set the description of the group
* @param {string} description The new description for the group
* @returns {GdprGroupBuilder}
* @memberof GdprGroupBuilder
*/
public withDescription(description: string): GdprGroupBuilder {
return this.edit(b => b.description = description);
}
/**
* Set the storage of the group
* @param {GdprStorage} storage The new storage for the group
* @returns {GdprGroupBuilder}
* @memberof GdprGroupBuilder
*/
public storedIn(storage: GdprStorage): GdprGroupBuilder {
return this.edit(b => b.storage = storage);
}
/**
* Mark as enabled
* @returns {GdprGroupBuilder}
* @memberof GdprGroupBuilder
*/
public enabled(): GdprGroupBuilder {
return this.edit(b => b.enable = true);
}
/**
* Mark as disabled
* @returns {GdprGroupBuilder}
* @memberof GdprGroupBuilder
*/
public disabled(): GdprGroupBuilder {
return this.edit(b => b.enable = false);
}
/**
* Mark as required
* @returns {GdprGroupBuilder}
* @memberof GdprGroupBuilder
*/
public required(): GdprGroupBuilder {
return this.edit(b => b.require = true);
}
/**
* Start adding a guard
* @param {?GdprStorage} [storage] The storage for the guard (by default it uses the group's storage)
* @returns {GdprGuardBuilder}
* @memberof GdprGroupBuilder
*/
public startGuard(storage: GdprStorage | null = null): GdprGuardBuilder {
return GdprGuardBuilder.create(this, storage || this.storage, this.enable);
}
/**
* Start adding a required guard
* @param {?GdprStorage} [storage] The storage for the guard (by default it uses the group's storage)
* @returns {GdprGuardBuilder}
* @memberof GdprGroupBuilder
*/
public startRequiredGuard(storage: GdprStorage | null): GdprGuardBuilder {
return this.startGuard(storage).required();
}
/**
* Add an enabled guard
* @param {string} name The name of the guard
* @param {string} [description] The description of the guard
* @param {?GdprStorage} [storage] The storage of the guard
* @returns {GdprGroupBuilder}
* @memberof GdprGroupBuilder
*/
public withEnabledGuard(name: string, description: string = "", storage: GdprStorage | null = null): GdprGroupBuilder {
return this.startGuard(storage)
.withName(name)
.withDescription(description)
.enabled()
.endGuard();
}
/**
* Add a disabled guard
* @param {string} name The name of the guard
* @param {string} [description] The description of the guard
* @param {?GdprStorage} [storage] The storage of the guard
* @returns {GdprGroupBuilder}
* @memberof GdprGroupBuilder
*/
public withDisabledGuard(name: string, description: string = "", storage: GdprStorage | null = null): GdprGroupBuilder {
return this.startGuard(storage)
.withName(name)
.withDescription(description)
.disabled()
.endGuard();
}
/**
* Edit the builder state
* @ignore
* @protected
* @param {(builder: GdprGroupBuilder) => any} cb
* @returns {GdprGroupBuilder}
* @memberof GdprGroupBuilder
*/
protected edit(cb: (builder: GdprGroupBuilder) => any): GdprGroupBuilder {
cb(this);
return this;
}
}
export {
GdprGroupBuilder,
}