predicate 1.3.2 → 1.3.3

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
  SHA1:
3
- metadata.gz: 0fa56aaf631881d7fa0b83d7d0353e5e1fd10518
4
- data.tar.gz: 19cd5e6f8b3e863db2e553ebe57aa41a5431bc7e
3
+ metadata.gz: d0640cc584c97fcc3284e49a237ed100eafb99b6
4
+ data.tar.gz: 477609b676466dd9e6a43e36740929e4ab660d81
5
5
  SHA512:
6
- metadata.gz: 4bf57668af7cacea1d5ff462164bc44437cff7d15f54935550e77aacf24709faf31f94d5c504aea178ab219b8dd50ffa649716d8ee240fd87f024f41ea56159a
7
- data.tar.gz: 6dfd2865bad700185ef9360de2ca8718352b77d32ef9c4995f8d486273aaeb815984144ce005fc9b8e6c4890d61018f00d0119af8f3da1619f8f85e4e3b5bc7a
6
+ metadata.gz: 31a4d9754d7b1c7be5ecc4703cd2b1b6ecfdeab6616e736b3754e276c48468881b79420475d64aa5acf265007daf0fe3309fe3e2cc5fcf69cd6a15f150d67b18
7
+ data.tar.gz: 142229a63ac9c525b1fa90eaf6689a673db467ffd11b9b26c034d75d752ff93258198eb4030ca167effc75fde6573ab9e2a8ed9e4fc7b4e9e3729ff0a5e12e5a
@@ -86,6 +86,11 @@ class Predicate
86
86
  "->(t){ #{code} }"
87
87
  end
88
88
 
89
+ def to_s(scope = nil)
90
+ ToS.call(self, scope: scope)
91
+ end
92
+ alias :inspect :to_s
93
+
89
94
  def to_proc(scope = 't')
90
95
  Kernel.eval(to_ruby_code(scope))
91
96
  end
@@ -0,0 +1,90 @@
1
+ class Predicate
2
+ class ToS < Sexpr::Processor
3
+
4
+ def on_tautology(sexpr)
5
+ "true"
6
+ end
7
+
8
+ def on_contradiction(sexpr)
9
+ "false"
10
+ end
11
+
12
+ def on_qualified_identifier(sexpr)
13
+ "#{sexpr.qualifier}.#{sexpr.name}"
14
+ end
15
+
16
+ def on_identifier(sexpr)
17
+ if s = options[:scope]
18
+ "#{s}.#{sexpr.last.to_s}"
19
+ else
20
+ "@#{sexpr.last.to_s}"
21
+ end
22
+ end
23
+
24
+ def on_not(sexpr)
25
+ "not(" << apply(sexpr.last, sexpr) << ")"
26
+ end
27
+
28
+ def on_and(sexpr)
29
+ sexpr.sexpr_body.map{|term|
30
+ apply(term, sexpr)
31
+ }.join(" AND ")
32
+ end
33
+
34
+ def on_or(sexpr)
35
+ sexpr.sexpr_body.map{|term|
36
+ apply(term, sexpr)
37
+ }.join(" OR ")
38
+ end
39
+
40
+ def on_dyadic(sexpr)
41
+ sexpr.sexpr_body.map{|term|
42
+ apply(term, sexpr)
43
+ }.join(" #{sexpr.operator_symbol} ")
44
+ end
45
+ alias :on_eq :on_dyadic
46
+ alias :on_neq :on_dyadic
47
+ alias :on_lt :on_dyadic
48
+ alias :on_lte :on_dyadic
49
+ alias :on_gt :on_dyadic
50
+ alias :on_gte :on_dyadic
51
+
52
+ def on_in(sexpr)
53
+ "#{apply(sexpr.identifier)} IN #{to_literal(sexpr.values)}"
54
+ end
55
+
56
+ def on_intersect(sexpr)
57
+ "#{apply(sexpr.identifier)} INTERSECTS #{to_literal(sexpr.values)}"
58
+ end
59
+
60
+ def on_literal(sexpr)
61
+ to_literal(sexpr.last)
62
+ end
63
+
64
+ def on_native(sexpr)
65
+ sexpr.last.inspect
66
+ end
67
+
68
+ def on_missing(sexpr)
69
+ raise "Unimplemented: #{sexpr.first}"
70
+ end
71
+
72
+ protected
73
+
74
+ def to_literal(x)
75
+ case x
76
+ when Array then "{" << x.map{|y| to_literal(y) }.join(',') << "}"
77
+ else x.inspect
78
+ end
79
+ end
80
+
81
+ def apply(sexpr, parent = nil)
82
+ code = super(sexpr)
83
+ if parent && (parent.priority >= sexpr.priority)
84
+ code = "(" << code << ")"
85
+ end
86
+ code
87
+ end
88
+
89
+ end
90
+ end
@@ -1,3 +1,4 @@
1
+ require_relative "processors/to_s"
1
2
  require_relative "processors/to_ruby_code"
2
3
  require_relative "processors/renamer"
3
4
  require_relative "processors/qualifier"
@@ -2,7 +2,7 @@ class Predicate
2
2
  module Version
3
3
  MAJOR = 1
4
4
  MINOR = 3
5
- TINY = 2
5
+ TINY = 3
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
data/lib/predicate.rb CHANGED
@@ -125,7 +125,10 @@ class Predicate
125
125
  def to_ruby_code(scope = "t")
126
126
  expr.to_ruby_code(scope)
127
127
  end
128
- alias :to_s :to_ruby_code
128
+
129
+ def to_s(scope = nil)
130
+ expr.to_s(scope)
131
+ end
129
132
 
130
133
  def to_proc
131
134
  @proc ||= expr.to_proc("t")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: predicate
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernard Lambeau
@@ -118,6 +118,7 @@ files:
118
118
  - lib/predicate/processors/qualifier.rb
119
119
  - lib/predicate/processors/renamer.rb
120
120
  - lib/predicate/processors/to_ruby_code.rb
121
+ - lib/predicate/processors/to_s.rb
121
122
  - lib/predicate/sequel.rb
122
123
  - lib/predicate/sequel/to_sequel.rb
123
124
  - lib/predicate/version.rb