« get me outta code hell

SortingRule.js « sorting-rule « things « data « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/things/sorting-rule/SortingRule.js
blob: 5d4bba99a09da2a212c511a5f3e1488952ed38fc (plain)
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
import {V} from '#composite';
import {unique} from '#sugar';
import Thing from '#thing';
import {isStringNonEmpty} from '#validators';

import {exposeConstant} from '#composite/control-flow';
import {flag} from '#composite/wiki-properties';

export class SortingRule extends Thing {
  static [Thing.friendlyName] = `Sorting Rule`;
  static [Thing.wikiData] = 'sortingRules';

  static [Thing.getPropertyDescriptors] = () => ({
    // Update & expose

    active: flag(V(true)),

    message: {
      flags: {update: true, expose: true},
      update: {validate: isStringNonEmpty},
    },

    // Expose only

    isSortingRule: exposeConstant(V(true)),
  });

  static [Thing.yamlDocumentSpec] = {
    fields: {
      'Message': {property: 'message'},
      'Active': {property: 'active'},
    },
  };

  check(opts) {
    return this.constructor.check(this, opts);
  }

  apply(opts) {
    return this.constructor.apply(this, opts);
  }

  static check(rule, opts) {
    const result = this.apply(rule, {...opts, dry: true});
    if (!result) return true;
    if (!result.changed) return true;
    return false;
  }

  static async apply(_rule, _opts) {
    throw new Error(`Not implemented`);
  }

  static async* applyAll(_rules, _opts) {
    throw new Error(`Not implemented`);
  }

  static async* go({dataPath, wikiData, dry}) {
    const rules = wikiData.sortingRules;
    const constructors = unique(rules.map(rule => rule.constructor));

    for (const constructor of constructors) {
      yield* constructor.applyAll(
        rules
          .filter(rule => rule.active)
          .filter(rule => rule.constructor === constructor),
        {dataPath, wikiData, dry});
    }
  }
}