boto-rb-web 0.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +17 -0
- data/assets/boto-web/boot.js +16 -0
- data/assets/boto-web/boto-web.css +32 -0
- data/assets/boto-web/dom/animate.js +49 -0
- data/assets/boto-web/dom/flip.js +41 -0
- data/assets/boto-web/dom/keyed.js +15 -0
- data/assets/boto-web/dom/morph.js +112 -0
- data/assets/boto-web/dom/navigation.js +72 -0
- data/assets/boto-web/dom/sync.js +42 -0
- data/assets/boto-web/net/base.js +5 -0
- data/assets/boto-web/net/protocol.js +46 -0
- data/assets/boto-web/net/sse.js +45 -0
- data/assets/boto-web/net/sse_stream.js +84 -0
- data/assets/boto-web/net/token.js +9 -0
- data/assets/boto-web/net/transport.js +99 -0
- data/assets/boto-web/runtime/component.js +138 -0
- data/assets/boto-web/runtime/events.js +54 -0
- data/assets/boto-web/runtime/hooks.js +82 -0
- data/assets/boto-web/runtime/live.js +12 -0
- data/assets/boto-web/runtime/on.js +36 -0
- data/assets/boto-web/runtime/runtime.js +100 -0
- data/assets/boto-web/runtime/scope.js +112 -0
- data/assets/boto-web/runtime/signals.js +123 -0
- data/assets/boto-web/template/decimal.js +93 -0
- data/assets/boto-web/template/helpers.js +31 -0
- data/assets/boto-web/template/html.js +61 -0
- data/assets/boto-web/template/hydrate.js +25 -0
- data/assets/boto-web/template/ruby/core.js +72 -0
- data/assets/boto-web/template/ruby/lists.js +63 -0
- data/assets/boto-web/template/ruby.js +5 -0
- data/lib/boto/component/arguments.rb +58 -0
- data/lib/boto/component/definition.rb +114 -0
- data/lib/boto/component.rb +107 -0
- data/lib/boto/helper.rb +14 -0
- data/lib/boto/view.rb +63 -0
- data/lib/boto/web/app_extension.rb +78 -0
- data/lib/boto/web/assets.rb +59 -0
- data/lib/boto/web/child_path.rb +38 -0
- data/lib/boto/web/code_class.rb +16 -0
- data/lib/boto/web/compiler/analysis/deps.rb +46 -0
- data/lib/boto/web/compiler/analysis/handlers.rb +47 -0
- data/lib/boto/web/compiler/analysis/walker.rb +174 -0
- data/lib/boto/web/compiler/analysis.rb +44 -0
- data/lib/boto/web/compiler/client_helper.rb +34 -0
- data/lib/boto/web/compiler/computed_body.rb +40 -0
- data/lib/boto/web/compiler/directives.rb +86 -0
- data/lib/boto/web/compiler/expression/calls.rb +149 -0
- data/lib/boto/web/compiler/expression/collections.rb +116 -0
- data/lib/boto/web/compiler/expression/literals.rb +75 -0
- data/lib/boto/web/compiler/expression/logic.rb +53 -0
- data/lib/boto/web/compiler/expression/operators.rb +85 -0
- data/lib/boto/web/compiler/expression/strings.rb +31 -0
- data/lib/boto/web/compiler/expression.rb +93 -0
- data/lib/boto/web/compiler/module_writer/handlers_js.rb +75 -0
- data/lib/boto/web/compiler/module_writer/js.rb +24 -0
- data/lib/boto/web/compiler/module_writer/tag_js.rb +132 -0
- data/lib/boto/web/compiler/module_writer/template_js.rb +89 -0
- data/lib/boto/web/compiler/module_writer/type_shape.rb +28 -0
- data/lib/boto/web/compiler/module_writer.rb +79 -0
- data/lib/boto/web/compiler/template.rb +136 -0
- data/lib/boto/web/compiler/type_map.rb +43 -0
- data/lib/boto/web/compiler/type_scope.rb +68 -0
- data/lib/boto/web/compiler.rb +9 -0
- data/lib/boto/web/dispatcher.rb +113 -0
- data/lib/boto/web/events_endpoint.rb +43 -0
- data/lib/boto/web/failure.rb +30 -0
- data/lib/boto/web/head.rb +24 -0
- data/lib/boto/web/helpers.rb +40 -0
- data/lib/boto/web/hooks.rb +39 -0
- data/lib/boto/web/html.rb +65 -0
- data/lib/boto/web/layouts/context.rb +22 -0
- data/lib/boto/web/layouts/template.rb +31 -0
- data/lib/boto/web/layouts.rb +69 -0
- data/lib/boto/web/modules.rb +37 -0
- data/lib/boto/web/page_endpoint.rb +65 -0
- data/lib/boto/web/project_helpers.rb +54 -0
- data/lib/boto/web/protocol.rb +85 -0
- data/lib/boto/web/registry.rb +86 -0
- data/lib/boto/web/registry_error.rb +8 -0
- data/lib/boto/web/renderer/erb_source.rb +199 -0
- data/lib/boto/web/renderer/render_context.rb +37 -0
- data/lib/boto/web/renderer.rb +94 -0
- data/lib/boto/web/runtime.rb +45 -0
- data/lib/boto/web/scoped_css.rb +113 -0
- data/lib/boto/web/secret_key.rb +8 -0
- data/lib/boto/web/settings.rb +27 -0
- data/lib/boto/web/snapshot/changes.rb +26 -0
- data/lib/boto/web/snapshot/inputs.rb +50 -0
- data/lib/boto/web/snapshot/token.rb +42 -0
- data/lib/boto/web/snapshot.rb +111 -0
- data/lib/boto/web/static_file.rb +35 -0
- data/lib/boto/web/stylesheet.rb +53 -0
- data/lib/boto/web/tailwind.rb +69 -0
- data/lib/boto/web/template_preview.rb +31 -0
- data/lib/boto/web/transports/http.rb +36 -0
- data/lib/boto/web/transports/sse.rb +45 -0
- data/lib/boto/web/transports.rb +21 -0
- data/lib/boto/web/version.rb +7 -0
- data/lib/boto/web/view_loader.rb +21 -0
- data/lib/boto/web.rb +17 -0
- metadata +172 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// dependency; `tracking` holds the node running now.
|
|
2
|
+
|
|
3
|
+
let tracking = null;
|
|
4
|
+
let batchDepth = 0;
|
|
5
|
+
let flushing = false;
|
|
6
|
+
const queue = new Set();
|
|
7
|
+
|
|
8
|
+
function subscribe(subs) {
|
|
9
|
+
if (!tracking) return;
|
|
10
|
+
subs.add(tracking);
|
|
11
|
+
tracking.sources.add(subs);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function unlink(node) {
|
|
15
|
+
for (const subs of node.sources) subs.delete(node);
|
|
16
|
+
node.sources.clear();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function notify(subs) {
|
|
20
|
+
for (const node of [...subs]) node.invalidate();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function runTracked(node, fn) {
|
|
24
|
+
const previous = tracking;
|
|
25
|
+
tracking = node;
|
|
26
|
+
try {
|
|
27
|
+
return fn();
|
|
28
|
+
} finally {
|
|
29
|
+
tracking = previous;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function schedule(node) {
|
|
34
|
+
queue.add(node);
|
|
35
|
+
if (batchDepth === 0) flush();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// A flush in progress picks up the effects scheduled while it runs.
|
|
39
|
+
function flush() {
|
|
40
|
+
if (flushing) return;
|
|
41
|
+
flushing = true;
|
|
42
|
+
try {
|
|
43
|
+
while (queue.size) {
|
|
44
|
+
const [node] = queue;
|
|
45
|
+
queue.delete(node);
|
|
46
|
+
node.run();
|
|
47
|
+
}
|
|
48
|
+
} finally {
|
|
49
|
+
flushing = false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function batch(fn) {
|
|
54
|
+
batchDepth++;
|
|
55
|
+
try {
|
|
56
|
+
return fn();
|
|
57
|
+
} finally {
|
|
58
|
+
batchDepth--;
|
|
59
|
+
if (batchDepth === 0) flush();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function signal(initial) {
|
|
64
|
+
let value = initial;
|
|
65
|
+
const subs = new Set();
|
|
66
|
+
return {
|
|
67
|
+
get value() {
|
|
68
|
+
subscribe(subs);
|
|
69
|
+
return value;
|
|
70
|
+
},
|
|
71
|
+
set value(next) {
|
|
72
|
+
if (Object.is(next, value)) return;
|
|
73
|
+
value = next;
|
|
74
|
+
batch(() => notify(subs));
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function computed(fn) {
|
|
80
|
+
const subs = new Set();
|
|
81
|
+
let value;
|
|
82
|
+
let dirty = true;
|
|
83
|
+
const node = {
|
|
84
|
+
sources: new Set(),
|
|
85
|
+
invalidate() {
|
|
86
|
+
if (dirty) return;
|
|
87
|
+
dirty = true;
|
|
88
|
+
notify(subs);
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
return {
|
|
92
|
+
get value() {
|
|
93
|
+
subscribe(subs);
|
|
94
|
+
if (dirty) {
|
|
95
|
+
unlink(node);
|
|
96
|
+
value = runTracked(node, fn);
|
|
97
|
+
dirty = false;
|
|
98
|
+
}
|
|
99
|
+
return value;
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function effect(fn) {
|
|
105
|
+
const node = {
|
|
106
|
+
sources: new Set(),
|
|
107
|
+
disposed: false,
|
|
108
|
+
invalidate() {
|
|
109
|
+
schedule(this);
|
|
110
|
+
},
|
|
111
|
+
run() {
|
|
112
|
+
if (this.disposed) return;
|
|
113
|
+
unlink(this);
|
|
114
|
+
runTracked(this, fn);
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
node.run();
|
|
118
|
+
return () => {
|
|
119
|
+
node.disposed = true;
|
|
120
|
+
unlink(node);
|
|
121
|
+
queue.delete(node);
|
|
122
|
+
};
|
|
123
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Exact decimal for money: the value is `v / 10^s`, with `v` a BigInt and `s` the scale.
|
|
2
|
+
// No division: an exact quotient is not always a finite decimal, so it stays on the server.
|
|
3
|
+
|
|
4
|
+
const PATTERN = /^([+-]?)(\d+)(?:\.(\d+))?$/;
|
|
5
|
+
|
|
6
|
+
const abs = (n) => (n < 0n ? -n : n);
|
|
7
|
+
const pow10 = (n) => 10n ** BigInt(n);
|
|
8
|
+
const make = (v, s) => Object.freeze({ v, s });
|
|
9
|
+
|
|
10
|
+
function parse(text) {
|
|
11
|
+
const match = PATTERN.exec(text);
|
|
12
|
+
if (!match) throw new TypeError(`invalid decimal: ${text}`);
|
|
13
|
+
const [, sign, int, frac = ""] = match;
|
|
14
|
+
const v = BigInt(int + frac);
|
|
15
|
+
return make(sign === "-" ? -v : v, frac.length);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// A number with a fraction already went through a float; accepting it would hide the error.
|
|
19
|
+
function from(x) {
|
|
20
|
+
if (typeof x === "object" && x !== null && typeof x.v === "bigint") return x;
|
|
21
|
+
if (typeof x === "number") {
|
|
22
|
+
if (!Number.isSafeInteger(x)) throw new TypeError("not an exact decimal: pass a string");
|
|
23
|
+
return make(BigInt(x), 0);
|
|
24
|
+
}
|
|
25
|
+
if (typeof x === "string") return parse(x);
|
|
26
|
+
throw new TypeError("expected a string, an integer or a decimal");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Aligns both scales to the larger one, so BigInts add and compare directly.
|
|
30
|
+
function align(a, b) {
|
|
31
|
+
const s = Math.max(a.s, b.s);
|
|
32
|
+
return [a.v * pow10(s - a.s), b.v * pow10(s - b.s), s];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function add(a, b) {
|
|
36
|
+
const [x, y, s] = align(from(a), from(b));
|
|
37
|
+
return make(x + y, s);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function sub(a, b) {
|
|
41
|
+
const [x, y, s] = align(from(a), from(b));
|
|
42
|
+
return make(x - y, s);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function mul(a, b) {
|
|
46
|
+
const x = from(a);
|
|
47
|
+
const y = from(b);
|
|
48
|
+
return make(x.v * y.v, x.s + y.s);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function cmp(a, b) {
|
|
52
|
+
const [x, y] = align(from(a), from(b));
|
|
53
|
+
if (x === y) return 0;
|
|
54
|
+
return x < y ? -1 : 1;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Half up, away from zero (Ruby's BigDecimal ROUND_HALF_UP).
|
|
58
|
+
function roundHalfUp(v, digits) {
|
|
59
|
+
const unit = pow10(digits);
|
|
60
|
+
const magnitude = (abs(v) + unit / 2n) / unit;
|
|
61
|
+
return v < 0n ? -magnitude : magnitude;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function format(v, s) {
|
|
65
|
+
const digits = abs(v).toString().padStart(s + 1, "0");
|
|
66
|
+
const sign = v < 0n ? "-" : "";
|
|
67
|
+
const int = digits.slice(0, digits.length - s);
|
|
68
|
+
if (s === 0) return sign + int;
|
|
69
|
+
return `${sign}${int}.${digits.slice(digits.length - s)}`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function toString(d, scale) {
|
|
73
|
+
const x = from(d);
|
|
74
|
+
if (scale === undefined) return format(x.v, x.s);
|
|
75
|
+
if (scale >= x.s) return format(x.v * pow10(scale - x.s), scale);
|
|
76
|
+
return format(roundHalfUp(x.v, x.s - scale), scale);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export const D = Object.freeze({
|
|
80
|
+
from,
|
|
81
|
+
add,
|
|
82
|
+
sub,
|
|
83
|
+
mul,
|
|
84
|
+
cmp,
|
|
85
|
+
eq: (a, b) => cmp(a, b) === 0,
|
|
86
|
+
lt: (a, b) => cmp(a, b) < 0,
|
|
87
|
+
gt: (a, b) => cmp(a, b) > 0,
|
|
88
|
+
lte: (a, b) => cmp(a, b) <= 0,
|
|
89
|
+
gte: (a, b) => cmp(a, b) >= 0,
|
|
90
|
+
isZero: (a) => from(a).v === 0n,
|
|
91
|
+
sum: (list) => list.reduce((total, x) => add(total, x), make(0n, 0)),
|
|
92
|
+
toString,
|
|
93
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Display helpers, the browser's copy of lib/boto/web/helpers.rb: keep the rules in step.
|
|
2
|
+
|
|
3
|
+
import { D } from "./decimal.js";
|
|
4
|
+
import { R } from "./ruby.js";
|
|
5
|
+
|
|
6
|
+
function number(value, { precision = 0 } = {}) {
|
|
7
|
+
const text = D.toString(D.from(value), precision);
|
|
8
|
+
const sign = text.startsWith("-") ? "-" : "";
|
|
9
|
+
const [whole, fraction = ""] = text.replace("-", "").split(".");
|
|
10
|
+
const grouped = whole.replace(/\B(?=(\d{3})+(?!\d))/g, ".");
|
|
11
|
+
if (precision === 0) return `${sign}${grouped}`;
|
|
12
|
+
|
|
13
|
+
return `${sign}${grouped},${fraction.padEnd(precision, "0")}`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const currency = (value) => `R$ ${number(value, { precision: 2 })}`;
|
|
17
|
+
|
|
18
|
+
// Dates and times arrive as the ISO text of the snapshot: the wall clock is cut as written, as strftime reads it.
|
|
19
|
+
const date = (value) => R.str(value).slice(0, 10);
|
|
20
|
+
|
|
21
|
+
const time = (value) => R.str(value).slice(11, 16);
|
|
22
|
+
|
|
23
|
+
const pluralize = (count, word) => `${R.str(count)} ${count === 1 ? word : `${word}s`}`;
|
|
24
|
+
|
|
25
|
+
// Counts characters, not UTF-16 units, as Ruby's String#length does.
|
|
26
|
+
function truncate(text, { length = 30 } = {}) {
|
|
27
|
+
const chars = [...R.str(text)];
|
|
28
|
+
return chars.length > length ? `${chars.slice(0, length - 3).join("")}...` : text;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const H = Object.freeze({ number, currency, date, time, pluralize, truncate });
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// The HTML the browser writes, with the same spacing, quotes and omissions as lib/boto/web/html.rb.
|
|
2
|
+
|
|
3
|
+
import { R } from "./ruby.js";
|
|
4
|
+
|
|
5
|
+
const ESCAPES = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" };
|
|
6
|
+
const LOCALS_ESCAPES = { "'": "'", "&": "&", "<": "<", ">": ">" };
|
|
7
|
+
const URL_ATTRIBUTES = ["href", "src"];
|
|
8
|
+
|
|
9
|
+
const isNil = (x) => x === null || x === undefined;
|
|
10
|
+
|
|
11
|
+
export const str = (x) => R.str(x);
|
|
12
|
+
|
|
13
|
+
export const esc = (x) => str(x).replace(/[&<>"']/g, (c) => ESCAPES[c]);
|
|
14
|
+
|
|
15
|
+
// Ruby's \s is ASCII only, so the JavaScript one (which takes Unicode spaces too) is not used.
|
|
16
|
+
const unsafeUrl = (value) => /^javascript:/i.test(str(value).replace(/[ \t\r\n\f\v]/g, ""));
|
|
17
|
+
|
|
18
|
+
export function attr(name, value) {
|
|
19
|
+
if (isNil(value) || value === false) return "";
|
|
20
|
+
if (URL_ATTRIBUTES.includes(name) && unsafeUrl(value)) return "";
|
|
21
|
+
|
|
22
|
+
return ` ${name}="${esc(value)}"`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const boolAttr = (name, value) => (R.t(value) ? ` ${name}` : "");
|
|
26
|
+
|
|
27
|
+
export function classAttr(staticText, pairs) {
|
|
28
|
+
const classes = [staticText, ...pairs.filter(([, on]) => R.t(on)).map(([name]) => name)]
|
|
29
|
+
.filter((x) => !isNil(x))
|
|
30
|
+
.flatMap((x) => String(x).split(/\s+/).filter(Boolean));
|
|
31
|
+
|
|
32
|
+
return classes.length === 0 ? "" : ` class="${esc(classes.join(" "))}"`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function styleAttr(staticText, pairs) {
|
|
36
|
+
const declarations = [
|
|
37
|
+
isNil(staticText) ? null : staticText.trim(),
|
|
38
|
+
...pairs.filter(([, value]) => !isNil(value) && value !== false).map(([name, value]) => `${name}: ${str(value)};`),
|
|
39
|
+
]
|
|
40
|
+
.filter((declaration) => !isNil(declaration) && declaration !== "")
|
|
41
|
+
.map((declaration) => (declaration.endsWith(";") ? declaration : `${declaration};`));
|
|
42
|
+
|
|
43
|
+
return declarations.length === 0 ? "" : ` style="${esc(declarations.join(" "))}"`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function localsAttr(locals) {
|
|
47
|
+
if (Object.keys(locals).length === 0) return "";
|
|
48
|
+
|
|
49
|
+
const json = JSON.stringify(plain(locals));
|
|
50
|
+
return ` data-locals='${json.replace(/['&<>]/g, (c) => LOCALS_ESCAPES[c])}'`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Decimals become their text, as the server's JSON does, before the locals are written.
|
|
54
|
+
function plain(value) {
|
|
55
|
+
if (R.isDecimal(value)) return str(value);
|
|
56
|
+
if (Array.isArray(value)) return value.map(plain);
|
|
57
|
+
if (value !== null && typeof value === "object") {
|
|
58
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, plain(item)]));
|
|
59
|
+
}
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// A type is a name ("decimal", "string"...), { list: T } or { schema: { field: T } }; an unknown type passes through.
|
|
2
|
+
|
|
3
|
+
import { D } from "./decimal.js";
|
|
4
|
+
|
|
5
|
+
function eachField(schema, value, fn) {
|
|
6
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, fn(schema[key], item)]));
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function hydrate(type, json) {
|
|
10
|
+
if (json === null || json === undefined) return null;
|
|
11
|
+
if (type === "decimal") return D.from(json);
|
|
12
|
+
if (typeof type !== "object" || type === null) return json;
|
|
13
|
+
if (type.list) return json.map((item) => hydrate(type.list, item));
|
|
14
|
+
|
|
15
|
+
return eachField(type.schema, json, hydrate);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function dehydrate(type, value) {
|
|
19
|
+
if (value === null || value === undefined) return null;
|
|
20
|
+
if (type === "decimal") return D.toString(value);
|
|
21
|
+
if (typeof type !== "object" || type === null) return value;
|
|
22
|
+
if (type.list) return value.map((item) => dehydrate(type.list, item));
|
|
23
|
+
|
|
24
|
+
return eachField(type.schema, value, dehydrate);
|
|
25
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
|
|
2
|
+
import { D } from "../decimal.js";
|
|
3
|
+
|
|
4
|
+
const isNil = (x) => x === null || x === undefined;
|
|
5
|
+
const isPlainObject = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
|
|
6
|
+
export const isDecimal = (x) => typeof x === "object" && x !== null && typeof x.v === "bigint";
|
|
7
|
+
|
|
8
|
+
export function t(x) {
|
|
9
|
+
return !isNil(x) && x !== false;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// `a && b` and `a || b` give back one of the operands, as Ruby does; the right side runs only when needed.
|
|
13
|
+
export function and(a, b) {
|
|
14
|
+
return t(a) ? b() : a;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function or(a, b) {
|
|
18
|
+
return t(a) ? a : b();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function eq(a, b) {
|
|
22
|
+
if (isNil(a) || isNil(b)) return isNil(a) && isNil(b);
|
|
23
|
+
if (Array.isArray(a) || Array.isArray(b)) return eqList(a, b);
|
|
24
|
+
if (isPlainObject(a) || isPlainObject(b)) return eqObject(a, b);
|
|
25
|
+
return a === b;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function eqList(a, b) {
|
|
29
|
+
return Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((x, i) => eq(x, b[i]));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function eqObject(a, b) {
|
|
33
|
+
if (!isPlainObject(a) || !isPlainObject(b)) return false;
|
|
34
|
+
const keys = Object.keys(a);
|
|
35
|
+
return keys.length === Object.keys(b).length && keys.every((k) => k in b && eq(a[k], b[k]));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// BigDecimal#to_s("F"): no trailing zeros, but at least one decimal digit (12.50 -> 12.5, 12 -> 12.0).
|
|
39
|
+
function decimalText(x) {
|
|
40
|
+
const [whole, fraction = ""] = D.toString(x).split(".");
|
|
41
|
+
return `${whole}.${fraction.replace(/0+$/, "") || "0"}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function str(x) {
|
|
45
|
+
if (isNil(x)) return "";
|
|
46
|
+
if (isDecimal(x)) return decimalText(x);
|
|
47
|
+
return String(x);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function include(list, value) {
|
|
51
|
+
return list.some((x) => eq(x, value));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function strip(x) {
|
|
55
|
+
return str(x).trim();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function upcase(x) {
|
|
59
|
+
return str(x).toUpperCase();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function downcase(x) {
|
|
63
|
+
return str(x).toLowerCase();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function startWith(x, prefix) {
|
|
67
|
+
return str(x).startsWith(str(prefix));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function endWith(x, suffix) {
|
|
71
|
+
return str(x).endsWith(str(suffix));
|
|
72
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
|
|
2
|
+
import { t } from "./core.js";
|
|
3
|
+
|
|
4
|
+
function first(list) {
|
|
5
|
+
return list.length > 0 ? list[0] : null;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function last(list) {
|
|
9
|
+
return list.length > 0 ? list[list.length - 1] : null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// The index goes second, as each_with_index gives it: the blocks of an each need it for their paths.
|
|
13
|
+
function map(list, fn) {
|
|
14
|
+
return list.map((x, i) => fn(x, i));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function select(list, fn) {
|
|
18
|
+
return list.filter((x) => t(fn(x)));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function reject(list, fn) {
|
|
22
|
+
return list.filter((x) => !t(fn(x)));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function find(list, fn) {
|
|
26
|
+
const found = list.find((x) => t(fn(x)));
|
|
27
|
+
return found === undefined ? null : found;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function count(list, fn) {
|
|
31
|
+
return select(list, fn).length;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function any(list, fn) {
|
|
35
|
+
return list.some((x) => t(fn(x)));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function all(list, fn) {
|
|
39
|
+
return list.every((x) => t(fn(x)));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function none(list, fn) {
|
|
43
|
+
return !any(list, fn);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function sum(list) {
|
|
47
|
+
return list.reduce((total, x) => total + x, 0);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function compare(a, b) {
|
|
51
|
+
if (a < b) return -1;
|
|
52
|
+
return a > b ? 1 : 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Array.prototype.sort is stable, and the map pass keeps the input array untouched.
|
|
56
|
+
function sortBy(list, fn) {
|
|
57
|
+
return list
|
|
58
|
+
.map((item) => ({ item, key: fn(item) }))
|
|
59
|
+
.sort((a, b) => compare(a.key, b.key))
|
|
60
|
+
.map(({ item }) => item);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export { first, last, map, select, reject, find, count, any, all, none, sum, sortBy };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
class Component
|
|
5
|
+
# The `arg` declarations: each one types a keyword of the `action def` right below it.
|
|
6
|
+
module Arguments
|
|
7
|
+
KEYWORD_KINDS = %i[keyreq key].freeze
|
|
8
|
+
REST_KIND = :keyrest
|
|
9
|
+
|
|
10
|
+
def arg(name, type, **constraints)
|
|
11
|
+
definition.add_arg(Definition::Arg.new(name:, type: Boto::Types.build(type, **constraints), declared_type: type))
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Coerces the keywords of an action from untrusted input; collects the errors of every arg.
|
|
15
|
+
def coerce_args(action_name, given)
|
|
16
|
+
raise ArgumentError, "#{action_name} is not an action of #{name}" unless definition.action?(action_name)
|
|
17
|
+
|
|
18
|
+
coerced = {}
|
|
19
|
+
errors = []
|
|
20
|
+
definition.action_args(action_name).each_value do |arg|
|
|
21
|
+
coerced[arg.name] = arg.type.coerce(given[arg.name])
|
|
22
|
+
rescue Boto::Types::Invalid => e
|
|
23
|
+
errors.concat(e.errors.map { |error| { path: Boto::Types.join_path(arg.name.to_s, error[:path]), message: error[:message] } })
|
|
24
|
+
end
|
|
25
|
+
raise Boto::Types::Invalid, errors if errors.any?
|
|
26
|
+
|
|
27
|
+
coerced
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
# Returns the pending args once they match the keywords of the action; raises otherwise.
|
|
33
|
+
def check_action_arguments!(action_name)
|
|
34
|
+
keywords = action_keyword_names(action_name)
|
|
35
|
+
pending = definition.pending_args
|
|
36
|
+
missing = keywords - pending.keys
|
|
37
|
+
raise ArgumentError, "#{action_name} has keywords without arg: #{list(missing)}" if missing.any?
|
|
38
|
+
|
|
39
|
+
extra = pending.keys - keywords
|
|
40
|
+
raise ArgumentError, "arg without keyword in #{action_name}: #{list(extra)}" if extra.any?
|
|
41
|
+
|
|
42
|
+
pending
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# An action with no method yet has no keywords; the name check of the definition reports that case.
|
|
46
|
+
def action_keyword_names(action_name)
|
|
47
|
+
return [] unless method_defined?(action_name) || private_method_defined?(action_name)
|
|
48
|
+
|
|
49
|
+
parameters = instance_method(action_name).parameters
|
|
50
|
+
raise ArgumentError, "#{action_name} cannot take **rest: declare each keyword with arg" if parameters.any? { |kind, _| kind == REST_KIND }
|
|
51
|
+
|
|
52
|
+
parameters.filter_map { |kind, keyword| keyword if KEYWORD_KINDS.include?(kind) }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def list(names) = names.map { |name| ":#{name}" }.join(", ")
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
class Component
|
|
5
|
+
# What one component class declares. Names are unique across inputs, states, computeds and actions.
|
|
6
|
+
class Definition
|
|
7
|
+
# Coercion of a state value; a schema instance (or a list of them) is kept as it is.
|
|
8
|
+
State = Data.define(:name, :type, :declared_type, :default, :required, :readonly) do
|
|
9
|
+
def coerce(value)
|
|
10
|
+
return value if value.nil? && !required
|
|
11
|
+
return value if already_typed?(value)
|
|
12
|
+
|
|
13
|
+
type.coerce(value)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# The default is copied per instance, so a mutable default ([]) is never shared.
|
|
17
|
+
def default_value
|
|
18
|
+
return nil if default.nil?
|
|
19
|
+
|
|
20
|
+
coerce(default.dup)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def already_typed?(value)
|
|
26
|
+
item = declared_type.is_a?(Array) ? declared_type.first : declared_type
|
|
27
|
+
return false unless Boto::Types::SchemaRef.schema?(item)
|
|
28
|
+
return value.is_a?(Array) && value.all?(item) if declared_type.is_a?(Array)
|
|
29
|
+
|
|
30
|
+
value.is_a?(item)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# An input is optional by default: a missing optional input is nil.
|
|
35
|
+
Input = Data.define(:name, :type, :declared_type, :required) do
|
|
36
|
+
def coerce(value)
|
|
37
|
+
return nil if value.nil? && !required
|
|
38
|
+
|
|
39
|
+
type.coerce(value)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# The type of one keyword of an action, declared by `arg` right above the `action def`.
|
|
44
|
+
Arg = Data.define(:name, :type, :declared_type)
|
|
45
|
+
|
|
46
|
+
attr_accessor :selector, :channels_block
|
|
47
|
+
attr_reader :inputs, :states, :computeds, :actions, :pending_args
|
|
48
|
+
|
|
49
|
+
def initialize
|
|
50
|
+
@inputs = {}
|
|
51
|
+
@states = {}
|
|
52
|
+
@computeds = []
|
|
53
|
+
@actions = []
|
|
54
|
+
@action_args = {}
|
|
55
|
+
@pending_args = {}
|
|
56
|
+
@names = []
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def add_input(input)
|
|
60
|
+
ensure_no_pending_args!
|
|
61
|
+
register(input.name) { @inputs[input.name] = input }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def add_state(state)
|
|
65
|
+
ensure_no_pending_args!
|
|
66
|
+
register(state.name) { @states[state.name] = state }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def add_computed(name)
|
|
70
|
+
ensure_no_pending_args!
|
|
71
|
+
register(name) { @computeds << name }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# The args collected since the last action become this action's keywords.
|
|
75
|
+
def add_action(name, args)
|
|
76
|
+
register(name) do
|
|
77
|
+
@actions << name
|
|
78
|
+
@action_args[name] = args
|
|
79
|
+
@pending_args = {}
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def add_arg(arg)
|
|
84
|
+
raise ArgumentError, "arg #{arg.name} is declared twice before the same action" if @pending_args.key?(arg.name)
|
|
85
|
+
|
|
86
|
+
@pending_args[arg.name] = arg
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def action?(name) = @actions.include?(name)
|
|
90
|
+
|
|
91
|
+
def action_args(name) = @action_args.fetch(name)
|
|
92
|
+
|
|
93
|
+
def input_names = @inputs.keys
|
|
94
|
+
|
|
95
|
+
def state_names = @states.keys
|
|
96
|
+
|
|
97
|
+
private
|
|
98
|
+
|
|
99
|
+
def register(name)
|
|
100
|
+
raise ArgumentError, "#{name} is already declared in this component" if @names.include?(name)
|
|
101
|
+
|
|
102
|
+
@names << name
|
|
103
|
+
yield
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def ensure_no_pending_args!
|
|
107
|
+
return if @pending_args.empty?
|
|
108
|
+
|
|
109
|
+
names = @pending_args.keys.map { |name| ":#{name}" }.join(", ")
|
|
110
|
+
raise ArgumentError, "arg #{names} has no action def after it"
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|