liquidum 1.4.5 → 1.5.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 +4 -4
- data/.shipkit.yml +1 -1
- data/CHANGELOG.md +9 -0
- data/README.md +33 -0
- data/app/javascript/liquidum/conditions.js +143 -0
- data/app/models/liquidum/conditions.rb +115 -0
- data/config/importmap.rb +4 -0
- data/lib/liquidum/engine.rb +13 -0
- data/lib/liquidum/version.rb +1 -1
- data/package.json +8 -0
- metadata +5 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b176903dc2da7c2f01f5f71ae56bc7b679dac1beca506240b39d4cd6c628dc9e
|
|
4
|
+
data.tar.gz: a9e3c95f00f076c326640b3e182f13006f4cf6cc4db992f5c274d709b48ee285
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a02ee81925d3a68f8e543f9128fe88f6dc78a6578f6b64c007463156fb9004c30a7473ec2a12aeca5702a33f7d02dc65c061aac60c6ff344e2e50031f7e006f9
|
|
7
|
+
data.tar.gz: 457bb9194fe9343fbb8a634aeaca8859e0c6ec44c78e17885d74ff511ef2a2f3d28334f0febaaca5d2c562e520ffc4bbb659a647e3b36a037e32c6181771a8a3
|
data/.shipkit.yml
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
---
|
|
2
|
-
previous_version: 1.4.
|
|
2
|
+
previous_version: 1.4.5
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.5.0
|
|
4
|
+
|
|
5
|
+
- `Liquidum::Conditions`: rules on Liquid variables, met with all or any of
|
|
6
|
+
them, as Nuntius and Adiuvo each had their own (`Nuntius::Conditions`,
|
|
7
|
+
`Adiuvo::Conditions`).
|
|
8
|
+
- The same in the browser, `app/javascript/liquidum/conditions.js`, pinned as
|
|
9
|
+
`liquidum/conditions` for an app with importmap-rails. Both are tested with
|
|
10
|
+
the same cases.
|
|
11
|
+
|
|
3
12
|
## Unreleased
|
|
4
13
|
|
|
5
14
|
- `config.translation_scope` gives the scope of the `t` filter's keys that
|
data/README.md
CHANGED
|
@@ -127,6 +127,39 @@ Liquidum.configure do |config|
|
|
|
127
127
|
end
|
|
128
128
|
```
|
|
129
129
|
|
|
130
|
+
## Conditions
|
|
131
|
+
|
|
132
|
+
`Liquidum::Conditions` checks rules on Liquid variables: "if [all] of the
|
|
133
|
+
following conditions are met: [screening.state] [is] [processed]". Nuntius
|
|
134
|
+
sends a template's message only when they're met, Adiuvo runs a rule.
|
|
135
|
+
|
|
136
|
+
```ruby
|
|
137
|
+
conditions = Liquidum::Conditions.new(
|
|
138
|
+
"match" => "all", # or "any"
|
|
139
|
+
"rules" => [{"variable" => "ticket.priority", "operator" => "is", "value" => "urgent"}]
|
|
140
|
+
)
|
|
141
|
+
conditions.met?("ticket" => ticket) # the assigns a template renders with
|
|
142
|
+
conditions.to_h # to store, without the rules that don't count
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
A rule's operator is one of Mensa's: `is`, `isnt`, `matches` (contains),
|
|
146
|
+
`does_not_match`, `gt`, `gteq`, `lt`, `lteq`, `between` (with `value_to`),
|
|
147
|
+
`is_empty` and `isnt_empty`. A value compares as a number, a date or a time
|
|
148
|
+
when the variable is one, a list is one of its elements, and true and false
|
|
149
|
+
are also yes and ja. Rules may come numbered, as a form sends them.
|
|
150
|
+
|
|
151
|
+
The browser has the same, so a form or a test run can show what the server
|
|
152
|
+
will do: `app/javascript/liquidum/conditions.js`, pinned as
|
|
153
|
+
`liquidum/conditions` in an app with importmap-rails, on the variables as
|
|
154
|
+
JSON (and `Date`s):
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
import { met } from "liquidum/conditions"
|
|
158
|
+
met({ match: "all", rules: [{ variable: "ticket.priority", operator: "is", value: "urgent" }] }, { ticket })
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Both are tested with the same cases (`test/fixtures/files/conditions_cases.json`).
|
|
162
|
+
|
|
130
163
|
## Installation
|
|
131
164
|
|
|
132
165
|
Add this line to your application's Gemfile:
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// Conditions on Liquid variables in the browser, as Liquidum::Conditions
|
|
2
|
+
// works them out on the server: "if [all] of the following conditions are
|
|
3
|
+
// met: [screening.state] [is] [processed]", with Mensa's operators. Both are
|
|
4
|
+
// tested with the cases in test/fixtures/files/conditions_cases.json, so a
|
|
5
|
+
// form or an automation's test run in the browser goes the way the server
|
|
6
|
+
// will.
|
|
7
|
+
//
|
|
8
|
+
// The values are JSON as the server sends a drop (strings, numbers, true and
|
|
9
|
+
// false, lists, objects), and Dates: a value compares as a date when it is a
|
|
10
|
+
// Date, as on the server when it is one there.
|
|
11
|
+
//
|
|
12
|
+
// import { met } from "liquidum/conditions"
|
|
13
|
+
// met({ match: "all", rules: [{ variable: "ticket.priority", operator: "is", value: "urgent" }] }, { ticket })
|
|
14
|
+
|
|
15
|
+
export const MATCHES = ["all", "any"]
|
|
16
|
+
export const OPERATORS = ["is", "isnt", "matches", "does_not_match", "gt", "gteq", "lt", "lteq", "between", "is_empty", "isnt_empty"]
|
|
17
|
+
export const WITHOUT_VALUE = ["is_empty", "isnt_empty"]
|
|
18
|
+
const TRUE_VALUES = ["true", "yes", "ja", "1", "y", "j"]
|
|
19
|
+
// What BigDecimal() takes
|
|
20
|
+
const NUMBER = /^[-+]?(\d+(_\d+)*(\.(\d+(_\d+)*)?)?|\.\d+(_\d+)*)([eE][-+]?\d+)?$/
|
|
21
|
+
const DATE = /^(\d{4})-(\d{2})-(\d{2})/
|
|
22
|
+
|
|
23
|
+
// Whether the values meet {match, rules}, as Liquidum::Conditions#met?
|
|
24
|
+
export function met(conditions = {}, values = {}) {
|
|
25
|
+
const rules = parse(conditions?.rules)
|
|
26
|
+
if (!rules.length) return true
|
|
27
|
+
|
|
28
|
+
const results = (rule) => compare(rule, valueAt(values, rule.variable))
|
|
29
|
+
return conditions.match === "any" ? rules.some(results) : rules.every(results)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// The rules that count, as Liquidum::Conditions has them: with a variable and
|
|
33
|
+
// a known operator; numbered as a form sends them, or a list
|
|
34
|
+
export function parse(rules) {
|
|
35
|
+
const list = Array.isArray(rules) ? rules : rules && typeof rules === "object" ? Object.values(rules) : []
|
|
36
|
+
return list
|
|
37
|
+
.filter((rule) => rule && typeof rule === "object" && String(rule.variable ?? "").trim() && OPERATORS.includes(rule.operator))
|
|
38
|
+
.map((rule) => ({ variable: String(rule.variable).trim(), operator: rule.operator, value: String(rule.value ?? "").trim(), value_to: String(rule.value_to ?? "").trim() }))
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// A variable's value as Liquid looks it up: "contract.lines[0].amount",
|
|
42
|
+
// "contract.lines.size", first and last; null when it isn't there
|
|
43
|
+
export function valueAt(values, variable) {
|
|
44
|
+
const steps = String(variable)
|
|
45
|
+
.replace(/\[(\d+)\]/g, ".$1")
|
|
46
|
+
.replace(/\[["']([^"']*)["']\]/g, ".$1")
|
|
47
|
+
.split(".")
|
|
48
|
+
.filter((step) => step !== "")
|
|
49
|
+
let current = values
|
|
50
|
+
for (const step of steps) {
|
|
51
|
+
current = stepInto(current, step)
|
|
52
|
+
if (current === undefined) return null
|
|
53
|
+
}
|
|
54
|
+
return current === undefined ? null : current
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function stepInto(current, step) {
|
|
58
|
+
if (current == null || typeof current !== "object" || current instanceof Date) {
|
|
59
|
+
return typeof current === "string" && step === "size" ? current.length : undefined
|
|
60
|
+
}
|
|
61
|
+
if (Array.isArray(current)) {
|
|
62
|
+
if (/^-?\d+$/.test(step)) return current.at(Number(step))
|
|
63
|
+
if (step === "size") return current.length
|
|
64
|
+
if (step === "first") return current[0]
|
|
65
|
+
if (step === "last") return current.at(-1)
|
|
66
|
+
return undefined
|
|
67
|
+
}
|
|
68
|
+
if (Object.hasOwn(current, step)) return current[step]
|
|
69
|
+
if (step === "size") return Object.keys(current).length
|
|
70
|
+
return undefined
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function compare(rule, actual) {
|
|
74
|
+
const order = (value) => orderOf(actual, value)
|
|
75
|
+
switch (rule.operator) {
|
|
76
|
+
case "is_empty":
|
|
77
|
+
return isEmpty(actual)
|
|
78
|
+
case "isnt_empty":
|
|
79
|
+
return !isEmpty(actual)
|
|
80
|
+
case "is":
|
|
81
|
+
return equal(actual, rule.value)
|
|
82
|
+
case "isnt":
|
|
83
|
+
return !equal(actual, rule.value)
|
|
84
|
+
case "matches":
|
|
85
|
+
return text(actual).includes(rule.value.toLowerCase())
|
|
86
|
+
case "does_not_match":
|
|
87
|
+
return !text(actual).includes(rule.value.toLowerCase())
|
|
88
|
+
case "gt":
|
|
89
|
+
return order(rule.value) > 0
|
|
90
|
+
case "gteq":
|
|
91
|
+
return order(rule.value) >= 0
|
|
92
|
+
case "lt":
|
|
93
|
+
return order(rule.value) < 0
|
|
94
|
+
case "lteq":
|
|
95
|
+
return order(rule.value) <= 0
|
|
96
|
+
case "between": {
|
|
97
|
+
const from = order(rule.value)
|
|
98
|
+
const to = order(rule.value_to)
|
|
99
|
+
return from >= 0 && to <= 0
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return false
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function isEmpty(actual) {
|
|
106
|
+
if (actual == null) return true
|
|
107
|
+
if (typeof actual === "string" || Array.isArray(actual)) return actual.length === 0
|
|
108
|
+
if (typeof actual === "object" && !(actual instanceof Date)) return Object.keys(actual).length === 0
|
|
109
|
+
return false
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// A list is one of its elements; true and false also as yes or ja
|
|
113
|
+
function equal(actual, value) {
|
|
114
|
+
if (Array.isArray(actual)) return actual.some((element) => equal(element, value))
|
|
115
|
+
if (typeof actual === "boolean") return actual === TRUE_VALUES.includes(value.toLowerCase())
|
|
116
|
+
return orderOf(actual, value) === 0 || text(actual) === value.toLowerCase()
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function text(actual) {
|
|
120
|
+
const list = Array.isArray(actual) ? actual : actual == null ? [] : [actual]
|
|
121
|
+
return list.map((element) => (element instanceof Date ? element.toISOString().slice(0, 10) : String(element))).join(", ").toLowerCase()
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// actual <=> value, with value as a number or date when actual is one; NaN
|
|
125
|
+
// when they can't be compared, so every comparison with it is false
|
|
126
|
+
function orderOf(actual, value) {
|
|
127
|
+
let other
|
|
128
|
+
if (typeof actual === "number") other = NUMBER.test(value) ? Number(value.replaceAll("_", "")) : null
|
|
129
|
+
else if (actual instanceof Date) other = date(value)
|
|
130
|
+
else if (typeof actual === "string") other = value
|
|
131
|
+
if (other == null) return NaN
|
|
132
|
+
|
|
133
|
+
const [a, b] = actual instanceof Date ? [actual.getTime(), other.getTime()] : [actual, other]
|
|
134
|
+
return a < b ? -1 : a > b ? 1 : 0
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function date(value) {
|
|
138
|
+
const match = String(value).match(DATE)
|
|
139
|
+
if (!match) return null
|
|
140
|
+
const [year, month, day] = match.slice(1).map(Number)
|
|
141
|
+
const result = new Date(Date.UTC(year, month - 1, day))
|
|
142
|
+
return result.getUTCMonth() === month - 1 && result.getUTCDate() === day ? result : null
|
|
143
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Liquidum
|
|
4
|
+
# Conditions on Liquid variables: "if [all] of the following conditions are
|
|
5
|
+
# met: [screening.state] [is] [processed]". A rule's variable is a Liquid
|
|
6
|
+
# variable (a drop's, see Drop.variables), compared with Mensa's operators.
|
|
7
|
+
# Nuntius sends a message only when its template's are met, Adiuvo runs a
|
|
8
|
+
# rule, an automation takes a branch.
|
|
9
|
+
#
|
|
10
|
+
# The browser has the same (app/javascript/liquidum/conditions.js, pinned as
|
|
11
|
+
# liquidum/conditions); both are tested with the cases in
|
|
12
|
+
# test/fixtures/files/conditions_cases.json, so they agree.
|
|
13
|
+
#
|
|
14
|
+
# Stored as {"match" => "all" | "any", "rules" => [{"variable", "operator",
|
|
15
|
+
# "value", "value_to"}]}, value_to only for between.
|
|
16
|
+
class Conditions
|
|
17
|
+
MATCHES = %w[all any].freeze
|
|
18
|
+
OPERATORS = %w[is isnt matches does_not_match gt gteq lt lteq between is_empty isnt_empty].freeze
|
|
19
|
+
WITHOUT_VALUE = %w[is_empty isnt_empty].freeze
|
|
20
|
+
TRUE_VALUES = %w[true yes ja 1 y j].freeze
|
|
21
|
+
|
|
22
|
+
Rule = Data.define(:variable, :operator, :value, :value_to)
|
|
23
|
+
|
|
24
|
+
attr_reader :match, :rules
|
|
25
|
+
|
|
26
|
+
# data: the stored hash, or a form's (rules may be numbered)
|
|
27
|
+
def initialize(data = {})
|
|
28
|
+
data = data.to_h.deep_stringify_keys
|
|
29
|
+
rules = data["rules"]
|
|
30
|
+
rules = rules.values if rules.is_a?(Hash)
|
|
31
|
+
|
|
32
|
+
@match = MATCHES.include?(data["match"]) ? data["match"] : "all"
|
|
33
|
+
@rules = Array(rules).filter_map do |rule|
|
|
34
|
+
rule = rule.to_h.stringify_keys
|
|
35
|
+
next if rule["variable"].blank? || !OPERATORS.include?(rule["operator"])
|
|
36
|
+
|
|
37
|
+
Rule.new(variable: rule["variable"].strip, operator: rule["operator"], value: rule["value"].to_s.strip, value_to: rule["value_to"].to_s.strip)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def to_h
|
|
42
|
+
{"match" => match, "rules" => rules.map { |rule| rule.to_h.stringify_keys }}
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def empty?
|
|
46
|
+
rules.empty?
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Whether the Liquid variables meet them
|
|
50
|
+
def met?(assigns)
|
|
51
|
+
return true if empty?
|
|
52
|
+
|
|
53
|
+
context = Liquid::Context.new([assigns.to_h.stringify_keys])
|
|
54
|
+
results = rules.lazy.map { |rule| compare(rule, value_at(context, rule.variable)) }
|
|
55
|
+
(match == "any") ? results.any? : results.all?
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
def value_at(context, variable)
|
|
61
|
+
value = context[variable]
|
|
62
|
+
value.respond_to?(:to_liquid) ? value.to_liquid : value
|
|
63
|
+
rescue
|
|
64
|
+
nil
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def compare(rule, actual)
|
|
68
|
+
case rule.operator
|
|
69
|
+
when "is_empty" then empty_value?(actual)
|
|
70
|
+
when "isnt_empty" then !empty_value?(actual)
|
|
71
|
+
when "is" then equal?(actual, rule.value)
|
|
72
|
+
when "isnt" then !equal?(actual, rule.value)
|
|
73
|
+
when "matches" then text(actual).include?(rule.value.downcase)
|
|
74
|
+
when "does_not_match" then !text(actual).include?(rule.value.downcase)
|
|
75
|
+
when "gt" then order(actual, rule.value)&.positive?
|
|
76
|
+
when "gteq" then order(actual, rule.value)&.>=(0)
|
|
77
|
+
when "lt" then order(actual, rule.value)&.negative?
|
|
78
|
+
when "lteq" then order(actual, rule.value)&.<=(0)
|
|
79
|
+
when "between"
|
|
80
|
+
from = order(actual, rule.value)
|
|
81
|
+
to = order(actual, rule.value_to)
|
|
82
|
+
from && to && from >= 0 && to <= 0
|
|
83
|
+
end || false
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def empty_value?(actual)
|
|
87
|
+
actual.nil? || (actual.respond_to?(:empty?) && actual.empty?)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# A collection is one of its elements; true and false also as yes or ja
|
|
91
|
+
def equal?(actual, value)
|
|
92
|
+
return actual.any? { |element| equal?(element, value) } if actual.is_a?(Array)
|
|
93
|
+
return actual == TRUE_VALUES.include?(value.downcase) if [true, false].include?(actual)
|
|
94
|
+
|
|
95
|
+
order(actual, value)&.zero? || text(actual) == value.downcase
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def text(actual)
|
|
99
|
+
Array(actual).join(", ").downcase
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# actual <=> value, with value as a number, date or time when actual is
|
|
103
|
+
def order(actual, value)
|
|
104
|
+
other = case actual
|
|
105
|
+
when Numeric then BigDecimal(value)
|
|
106
|
+
when ActiveSupport::TimeWithZone, Time, DateTime then Time.zone.parse(value)
|
|
107
|
+
when Date then Date.parse(value)
|
|
108
|
+
when String then value
|
|
109
|
+
end
|
|
110
|
+
other.nil? ? nil : actual <=> other
|
|
111
|
+
rescue ArgumentError, TypeError
|
|
112
|
+
nil
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
data/config/importmap.rb
ADDED
data/lib/liquidum/engine.rb
CHANGED
|
@@ -11,5 +11,18 @@ module Liquidum
|
|
|
11
11
|
require_dependency(c)
|
|
12
12
|
end
|
|
13
13
|
end
|
|
14
|
+
|
|
15
|
+
# Its JavaScript (app/javascript/liquidum), pinned for an app with
|
|
16
|
+
# importmap-rails; an app that bundles imports it from there
|
|
17
|
+
initializer "liquidum.assets" do |app|
|
|
18
|
+
app.config.assets.paths << root.join("app/javascript") if app.config.respond_to?(:assets)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
initializer "liquidum.importmap", before: "importmap" do |app|
|
|
22
|
+
if app.config.respond_to?(:importmap)
|
|
23
|
+
app.config.importmap.paths << root.join("config/importmap.rb")
|
|
24
|
+
app.config.importmap.cache_sweepers << root.join("app/javascript")
|
|
25
|
+
end
|
|
26
|
+
end
|
|
14
27
|
end
|
|
15
28
|
end
|
data/lib/liquidum/version.rb
CHANGED
data/package.json
ADDED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: liquidum
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tom de Grunt
|
|
@@ -297,8 +297,10 @@ files:
|
|
|
297
297
|
- app/controllers/liquidum/tags_controller.rb
|
|
298
298
|
- app/controllers/liquidum/variables_controller.rb
|
|
299
299
|
- app/helpers/liquidum/application_helper.rb
|
|
300
|
+
- app/javascript/liquidum/conditions.js
|
|
300
301
|
- app/jobs/liquidum/application_job.rb
|
|
301
302
|
- app/models/concerns/liquidum/to_liquid.rb
|
|
303
|
+
- app/models/liquidum/conditions.rb
|
|
302
304
|
- app/models/liquidum/sandbox.rb
|
|
303
305
|
- app/tables/liquidum/application_table.rb
|
|
304
306
|
- app/tables/liquidum/sandboxes_table.rb
|
|
@@ -308,6 +310,7 @@ files:
|
|
|
308
310
|
- bin/de
|
|
309
311
|
- bin/rails
|
|
310
312
|
- bin/rubocop
|
|
313
|
+
- config/importmap.rb
|
|
311
314
|
- config/locales/en.yml
|
|
312
315
|
- config/locales/nl.yml
|
|
313
316
|
- config/routes.rb
|
|
@@ -348,6 +351,7 @@ files:
|
|
|
348
351
|
- lib/liquidum/version.rb
|
|
349
352
|
- liquidum.gemspec
|
|
350
353
|
- mise.toml
|
|
354
|
+
- package.json
|
|
351
355
|
homepage: https://github.com/entdec/liquidum
|
|
352
356
|
licenses:
|
|
353
357
|
- MIT
|