predicate 2.3.3 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/predicate.rb +7 -0
- data/lib/predicate/nodes/and.rb +9 -0
- data/lib/predicate/nodes/eq.rb +10 -0
- data/lib/predicate/nodes/expr.rb +4 -0
- data/lib/predicate/nodes/in.rb +5 -0
- data/lib/predicate/nodes/tautology.rb +4 -0
- data/lib/predicate/sequel/to_sequel.rb +1 -1
- data/lib/predicate/version.rb +2 -2
- data/spec/predicate/test_free_variables.rb +1 -1
- data/spec/predicate/test_to_hash.rb +40 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 68fd4bd97d5b3848c143403c1e5e161e015f9ccbb6964355af74aa70859997e1
|
4
|
+
data.tar.gz: dfa3f8d83173f44379e0ec59517b3f2dc7ebd7fbb410a3caeb09c8b5ec5bdc93
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7cd3c36e029feee497ea0e9cdd4c0c1cfae338abc5b5e3e74e6b69efd2d184714b5a8e8f21b2cda2dbc91cdd2f35d6894863872f5f5cb15a1efbdcae1b976f7b
|
7
|
+
data.tar.gz: f04c3c2877b6a0f9032e394839e871a55692dbdbbeceddba09f448a308e5f97d36b9a76b65801608f6365dbc9e782ba6fddb8ad24f4adfd43e398a6b4e4e684c
|
data/lib/predicate.rb
CHANGED
@@ -142,4 +142,11 @@ class Predicate
|
|
142
142
|
expr.to_s(scope)
|
143
143
|
end
|
144
144
|
|
145
|
+
# If possible, converts this predicate back to a `{ attr: value, ... }`
|
146
|
+
# hash. Raises an IllegalArgumentError if the predicate cannot be
|
147
|
+
# represented that way.
|
148
|
+
def to_hash
|
149
|
+
expr.to_hash
|
150
|
+
end
|
151
|
+
|
145
152
|
end # class Predicate
|
data/lib/predicate/nodes/and.rb
CHANGED
data/lib/predicate/nodes/eq.rb
CHANGED
@@ -48,5 +48,15 @@ class Predicate
|
|
48
48
|
left.evaluate(tuple) == right.evaluate(tuple)
|
49
49
|
end
|
50
50
|
|
51
|
+
def to_hash
|
52
|
+
if left.identifier? && right.literal? && !right.has_placeholder?
|
53
|
+
{ left.name => right.value }
|
54
|
+
elsif right.identifier? && left.literal? && !left.has_placeholder?
|
55
|
+
{ right.name => left.value }
|
56
|
+
else
|
57
|
+
super
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
51
61
|
end
|
52
62
|
end
|
data/lib/predicate/nodes/expr.rb
CHANGED
data/lib/predicate/nodes/in.rb
CHANGED
data/lib/predicate/version.rb
CHANGED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
class Predicate
|
3
|
+
describe Predicate, "to_hash" do
|
4
|
+
|
5
|
+
let(:p){ Predicate }
|
6
|
+
|
7
|
+
subject{ predicate.to_hash }
|
8
|
+
|
9
|
+
context "tautology" do
|
10
|
+
let(:predicate){ Predicate.tautology }
|
11
|
+
|
12
|
+
it{ expect(subject).to eql({}) }
|
13
|
+
end
|
14
|
+
|
15
|
+
context "contradiction" do
|
16
|
+
let(:predicate){ Predicate.contradiction }
|
17
|
+
|
18
|
+
it{ expect{ subject }.to raise_error(ArgumentError) }
|
19
|
+
end
|
20
|
+
|
21
|
+
context "eq" do
|
22
|
+
let(:predicate){ Predicate.eq(:x, 2) }
|
23
|
+
|
24
|
+
it{ expect(subject).to eql(x: 2) }
|
25
|
+
end
|
26
|
+
|
27
|
+
context "in" do
|
28
|
+
let(:predicate){ Predicate.in(:x, [2,3]) }
|
29
|
+
|
30
|
+
it{ expect(subject).to eql(x: [2,3]) }
|
31
|
+
end
|
32
|
+
|
33
|
+
context "and" do
|
34
|
+
let(:predicate){ Predicate.eq(:x, 3) & Predicate.in(:y, [2,3]) }
|
35
|
+
|
36
|
+
it{ expect(subject).to eql(x: 3, y: [2,3]) }
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
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.4.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: 2020-07-
|
11
|
+
date: 2020-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sexpr
|
@@ -183,6 +183,7 @@ files:
|
|
183
183
|
- spec/predicate/test_qualify.rb
|
184
184
|
- spec/predicate/test_rename.rb
|
185
185
|
- spec/predicate/test_tautology.rb
|
186
|
+
- spec/predicate/test_to_hash.rb
|
186
187
|
- spec/predicate/test_unqualify.rb
|
187
188
|
- spec/sequel/test_to_sequel.rb
|
188
189
|
- spec/spec_helper.rb
|