-
Notifications
You must be signed in to change notification settings - Fork 0
/
category.js
66 lines (50 loc) · 1.59 KB
/
category.js
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
'use strict';
const assert = require('assert');
const NamedObjectMap = require('./named_object_map');
const Serializable = require('./serializable');
const { arrayOf, identical, categorized } = require('./mappers');
const Form = require('./form');
const Fields = require('./fields');
const Validators = require('./validators');
const OptionItem = require('./option_item');
class Category extends Serializable {
constructor() {
super();
this.category = this;
}
set forms(forms) {
this.formsMap = NamedObjectMap.fromArray(forms);
}
get forms() {
return [...this.formsMap.values()];
}
getFormByName(formName) {
assert(this.formsMap.has(formName), `no such form: ${formName}`);
return this.formsMap.get(formName);
}
// helper methods
createForm(props) {
return Object.assign(new Form(), props, {
category: this,
});
}
createField(className, props) {
const Field = Fields.get(className);
return Object.assign(new Field(), props, { category: this });
}
createValidator(className, props) {
const Validator = Validators.get(className);
return Object.assign(new Validator(), props, { category: this });
}
createOptionItem(props) {
return Object.assign(new OptionItem(), props, { category: this });
}
}
Category.property('name', identical);
Category.property('forms', arrayOf(categorized(Form)));
Category.property('namespace', identical);
Category.property('name', identical, true);
Category.property('metadata', identical);
Category.property('autoincrement', identical);
Category.primaryKey = 'name';
module.exports = Category;