predicate 2.10.0 → 2.11.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/lib/predicate/nodes/or.rb +32 -0
- data/lib/predicate/version.rb +1 -1
- data/spec/predicate/test_to_hash.rb +18 -0
- data/spec/predicate/test_to_hashes.rb +6 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6fd6ea9f498fb935b7445d0486d1ff4387da5f8e9d71525c762abeb214353b13
|
|
4
|
+
data.tar.gz: f0bbf5080ea295803a10823984d5d99112033a2ea98dbaf1bdc3846fadd65db0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4422e7cf2eda59e119df2e1aa89fc703a9dc935c1bd80a9d625d6281316786fb2e56d5ad6c4dcd2a06a77a58510df9f74b2a3cf0e735a8f5b4f6014487a6ed4e
|
|
7
|
+
data.tar.gz: 310883c2b4d6c7e8661219969097e199a7bd9f6a6cd3ef520c93026a1b42485cf52b492449320d833aa0e00ff398d17cae00f7cd2ec089edbfb3a5a90eeebf52
|
data/lib/predicate/nodes/or.rb
CHANGED
|
@@ -10,5 +10,37 @@ class Predicate
|
|
|
10
10
|
sexpr_body.any?{|op| op.evaluate(tuple) }
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
+
# A disjunction can be represented as a `{ attr => [values] }` hash only
|
|
14
|
+
# when every operand constrains the *same single* attribute to a literal
|
|
15
|
+
# or a set of literals, e.g. `x == 1 OR x == 2 OR x IN [3, 4]`. Operands
|
|
16
|
+
# are folded together into an IN-style hash. As soon as an operand touches
|
|
17
|
+
# another attribute, or cannot itself be represented as a hash, the whole
|
|
18
|
+
# disjunction is not representable and an ArgumentError is raised (as for
|
|
19
|
+
# any other non-representable predicate).
|
|
20
|
+
def to_hash
|
|
21
|
+
merged = sexpr_body.inject(nil) do |acc, term|
|
|
22
|
+
hash = term.to_hash
|
|
23
|
+
return super unless hash.size == 1
|
|
24
|
+
|
|
25
|
+
attr, value = hash.first
|
|
26
|
+
values = value.is_a?(Array) ? value : [value]
|
|
27
|
+
|
|
28
|
+
if acc.nil?
|
|
29
|
+
[attr, values.dup]
|
|
30
|
+
elsif acc.first == attr
|
|
31
|
+
[attr, acc.last + values]
|
|
32
|
+
else
|
|
33
|
+
return super
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
return super if merged.nil?
|
|
38
|
+
{ merged.first => merged.last.uniq }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def to_hashes
|
|
42
|
+
[ to_hash, {} ]
|
|
43
|
+
end
|
|
44
|
+
|
|
13
45
|
end
|
|
14
46
|
end
|
data/lib/predicate/version.rb
CHANGED
|
@@ -36,5 +36,23 @@ class Predicate
|
|
|
36
36
|
it{ expect(subject).to eql(x: 3, y: [2,3]) }
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
+
context "or of eqs on the same attribute" do
|
|
40
|
+
let(:predicate){ Predicate.eq(:x, 2) | Predicate.eq(:x, 3) }
|
|
41
|
+
|
|
42
|
+
it{ expect(subject).to eql(x: [2,3]) }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
context "or mixing eq and in on the same attribute" do
|
|
46
|
+
let(:predicate){ Predicate.eq(:x, 2) | Predicate.in(:x, [3,4]) }
|
|
47
|
+
|
|
48
|
+
it{ expect(subject).to eql(x: [2,3,4]) }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
context "or on distinct attributes" do
|
|
52
|
+
let(:predicate){ Predicate.eq(:x, 2) | Predicate.eq(:y, 3) }
|
|
53
|
+
|
|
54
|
+
it{ expect{ subject }.to raise_error(ArgumentError) }
|
|
55
|
+
end
|
|
56
|
+
|
|
39
57
|
end
|
|
40
58
|
end
|
|
@@ -48,6 +48,12 @@ class Predicate
|
|
|
48
48
|
it{ expect(subject).to eql([{y: [2,3]},{x: 3}]) }
|
|
49
49
|
end
|
|
50
50
|
|
|
51
|
+
context "or of eqs on the same attribute" do
|
|
52
|
+
let(:predicate){ Predicate.eq(:x, 2) | Predicate.eq(:x, 3) }
|
|
53
|
+
|
|
54
|
+
it{ expect(subject).to eql([{x: [2,3]},{}]) }
|
|
55
|
+
end
|
|
56
|
+
|
|
51
57
|
context "not nil & eq" do
|
|
52
58
|
let(:predicate){ Predicate.neq(:x, nil) & Predicate.eq(:x, [2,3]) }
|
|
53
59
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: predicate
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.11.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bernard Lambeau
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-07-09 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: sexpr
|