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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6adb8570080165285172882eb0c0f8216180efa05a58df348055f9a406fcef24
4
- data.tar.gz: 3add1e5ed2a4d728fb92637c8527b8dfeaa2b66f16622a45608aca1d9f079774
3
+ metadata.gz: 68fd4bd97d5b3848c143403c1e5e161e015f9ccbb6964355af74aa70859997e1
4
+ data.tar.gz: dfa3f8d83173f44379e0ec59517b3f2dc7ebd7fbb410a3caeb09c8b5ec5bdc93
5
5
  SHA512:
6
- metadata.gz: 8967a78cd1bbee2b0e2666f0fe853743d98c5be4730c3d263ce30d541387b71de37ac1d3e9544d487c92782795fcdeeb116099b741a362b60390b35aa2834e6a
7
- data.tar.gz: ed564310b25c229a65824e06d6b37353d326a1dd56b00a6723ad669c4321fa2d4e50d660d5c02b66efbaef695a2063504602e9466c7908b0ebf681943dc28571
6
+ metadata.gz: 7cd3c36e029feee497ea0e9cdd4c0c1cfae338abc5b5e3e74e6b69efd2d184714b5a8e8f21b2cda2dbc91cdd2f35d6894863872f5f5cb15a1efbdcae1b976f7b
7
+ data.tar.gz: f04c3c2877b6a0f9032e394839e871a55692dbdbbeceddba09f448a308e5f97d36b9a76b65801608f6365dbc9e782ba6fddb8ad24f4adfd43e398a6b4e4e684c
@@ -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
@@ -53,5 +53,14 @@ class Predicate
53
53
  sexpr_body.all?{|operand| operand.evaluate(tuple) }
54
54
  end
55
55
 
56
+ def to_hash
57
+ sexpr_body.inject({}) do |p,term|
58
+ p.merge(term.to_hash){|k,v1,v2|
59
+ super unless v1 == v2
60
+ v1
61
+ }
62
+ end
63
+ end
64
+
56
65
  end
57
66
  end
@@ -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
@@ -97,6 +97,10 @@ class Predicate
97
97
  ToS.call(self, scope: scope)
98
98
  end
99
99
 
100
+ def to_hash
101
+ raise ArgumentError, "Unable to represent #{self} to a Hash"
102
+ end
103
+
100
104
  def sexpr(arg)
101
105
  Factory.sexpr(arg)
102
106
  end
@@ -75,5 +75,10 @@ class Predicate
75
75
  left.identifier? && right.literal? && !right.has_placeholder?
76
76
  end
77
77
 
78
+ def to_hash
79
+ return super unless var_against_literal_value?
80
+ { identifier.name => right.value }
81
+ end
82
+
78
83
  end
79
84
  end
@@ -38,5 +38,9 @@ class Predicate
38
38
  true
39
39
  end
40
40
 
41
+ def to_hash
42
+ {}
43
+ end
44
+
41
45
  end
42
46
  end
@@ -110,7 +110,7 @@ class Predicate
110
110
  end
111
111
 
112
112
  def on_unsupported(sexpr)
113
- raise NotSupportedError
113
+ raise NotSupportedError, "Unsupported predicate #{sexpr}"
114
114
  end
115
115
  alias :on_native :on_unsupported
116
116
  alias :on_intersect :on_unsupported
@@ -1,8 +1,8 @@
1
1
  class Predicate
2
2
  module Version
3
3
  MAJOR = 2
4
- MINOR = 3
5
- TINY = 3
4
+ MINOR = 4
5
+ TINY = 0
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
@@ -9,6 +9,6 @@ class Predicate
9
9
 
10
10
  it{ should eq([:x]) }
11
11
  end
12
-
12
+
13
13
  end
14
14
  end
@@ -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.3.3
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-08 00:00:00.000000000 Z
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