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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ba74ff534d4a9b58d4fdc9dc671d17275d9d01d65b391b509be68fd6500fb75b
4
- data.tar.gz: 964c06320295c968a80547e7af048bfd6382638414e724734f7b79ab9c739ac1
3
+ metadata.gz: 6fd6ea9f498fb935b7445d0486d1ff4387da5f8e9d71525c762abeb214353b13
4
+ data.tar.gz: f0bbf5080ea295803a10823984d5d99112033a2ea98dbaf1bdc3846fadd65db0
5
5
  SHA512:
6
- metadata.gz: e4ac5d9e093b57a77ca942c11f602e13460d9e9154d6f5b271651c3ea7972c6fc0fb01a19b66c1a462e2c2d0d8760b7479ec75db566ef83fa7c63c1b3ab62db9
7
- data.tar.gz: 275ebdd55119eca9b6ef5513f90ba0a2e9664cf2a70fa3fa28da342fcbb0ce91ade41154cbc04e187c89c33b34473d0a83fe98828aec40c1573dc465f1031518
6
+ metadata.gz: 4422e7cf2eda59e119df2e1aa89fc703a9dc935c1bd80a9d625d6281316786fb2e56d5ad6c4dcd2a06a77a58510df9f74b2a3cf0e735a8f5b4f6014487a6ed4e
7
+ data.tar.gz: 310883c2b4d6c7e8661219969097e199a7bd9f6a6cd3ef520c93026a1b42485cf52b492449320d833aa0e00ff398d17cae00f7cd2ec089edbfb3a5a90eeebf52
@@ -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
@@ -1,7 +1,7 @@
1
1
  class Predicate
2
2
  module Version
3
3
  MAJOR = 2
4
- MINOR = 10
4
+ MINOR = 11
5
5
  TINY = 0
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
@@ -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.10.0
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-04-24 00:00:00.000000000 Z
11
+ date: 2026-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sexpr