factbase 0.9.7 → 0.9.8

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: e7d1b649fb808d571609f3231f90dbd57ba57683993d52ee2eaddd674e2aeb65
4
- data.tar.gz: 717ea095ae2e1092d71509b39f1581cfc4b65d47ca9f39cb704d694eabbd91d9
3
+ metadata.gz: 1b9679d9fad6ba549ccccd428809c330ba6f2c3eefb3a8c32cd6b2ea9668f8e1
4
+ data.tar.gz: 5103850e97643459fda4640e5564fe8073bc0c949b699a325a83360bb32e7dfc
5
5
  SHA512:
6
- metadata.gz: 9f6955bdf61ad153a2cf956935310fbd1d59782a34e479ae94e48c70ab8a1002dfdf61f2384e968bbdedb0be58d19ea556841d7b7300aa110927f78f1fdabaf9
7
- data.tar.gz: 2364a4421fcf35c42b6e316e33f11afaf4abc69b33502b475fa08fc28e2e45b8dbe138cf72d26b8938212d2807dfd8c3b50aa40b166e481e15f363d2cf5629c8
6
+ metadata.gz: cb9d62637fde56598cb1c2c01d492b6f2e0e9f144ee7eba1cbca88779be1214afc1f7ec43687baec3a9bc232ccb127856a9aad4b5732965667c2f69c91341229
7
+ data.tar.gz: 10c1b8ddeeb23a0b1ed8de099aad0bf55c2e19370fba0d6686384a24769a57f3483cb10de5313588b0607e8dee9b4484050dde929b8e7fe2a9b053a96f242b20
@@ -54,8 +54,7 @@ module Factbase::IndexedTerm
54
54
  end
55
55
  vv =
56
56
  if @operands[1].is_a?(Symbol)
57
- sym = @operands[1].to_s.gsub(/^\$/, '')
58
- params[sym] || []
57
+ params[@operands[1].to_s] || []
59
58
  else
60
59
  [@operands[1]]
61
60
  end
@@ -84,8 +83,7 @@ module Factbase::IndexedTerm
84
83
  tuples = _as_tuples(
85
84
  @operands.sort_by { |o| o.operands.first }.map do |o|
86
85
  if o.operands[1].is_a?(Symbol)
87
- sym = o.operands[1].to_s.gsub(/^\$/, '')
88
- params[sym] || []
86
+ params[o.operands[1].to_s] || []
89
87
  else
90
88
  [o.operands[1]]
91
89
  end
@@ -61,7 +61,6 @@ class Factbase::Rules
61
61
  # Fact decorator.
62
62
  #
63
63
  # This is an internal class, it is not supposed to be instantiated directly.
64
- #
65
64
  class Fact
66
65
  def initialize(fact, check, fb)
67
66
  @fact = fact
@@ -130,9 +129,10 @@ class Factbase::Rules
130
129
  end
131
130
 
132
131
  def it(fact, _fb)
132
+ return if @uid.nil?
133
133
  a = fact[@uid]
134
134
  return if a.nil?
135
- @facts << a[0] unless @uid.nil?
135
+ @facts << a[0]
136
136
  end
137
137
 
138
138
  def include?(fact)
data/lib/factbase/tee.rb CHANGED
@@ -31,10 +31,13 @@ class Factbase::Tee
31
31
  end
32
32
 
33
33
  others do |*args|
34
- if args[0].to_s == '[]' && args[1].to_s.start_with?('$')
34
+ if args[0].to_s == '[]' && args[1].start_with?('$')
35
35
  n = args[1].to_s
36
36
  n = n[1..] unless @upper.is_a?(Factbase::Tee)
37
- @upper[n]
37
+ r = @upper[n]
38
+ r = @fact[n] if r.nil?
39
+ r = [r] unless r.respond_to?(:each) || r.nil?
40
+ r
38
41
  else
39
42
  @fact.method_missing(*args)
40
43
  end
data/lib/factbase/term.rb CHANGED
@@ -218,6 +218,10 @@ class Factbase::Term
218
218
  [v]
219
219
  end
220
220
  end
221
+ raise 'Why not array?' unless v.is_a?(Array)
222
+ unless v.all? { |i| [Float, Integer, String, Time, TrueClass, FalseClass].any? { |t| i.is_a?(t) } }
223
+ raise 'Wrong type inside'
224
+ end
221
225
  v
222
226
  end
223
227
  end
data/lib/factbase.rb CHANGED
@@ -82,7 +82,7 @@ require 'yaml'
82
82
  # License:: MIT
83
83
  class Factbase
84
84
  # Current version of the gem (changed by .rultor.yml on every release)
85
- VERSION = '0.9.7' unless const_defined?(:VERSION)
85
+ VERSION = '0.9.8' unless const_defined?(:VERSION)
86
86
 
87
87
  # An exception that may be thrown in a transaction, to roll it back.
88
88
  class Rollback < StandardError; end
@@ -19,7 +19,7 @@ class TestIndexedTerm < Factbase::Test
19
19
  idx = {}
20
20
  term.redress!(Factbase::IndexedTerm, idx:)
21
21
  maps = Factbase::Taped.new([{ 'foo' => [42] }, { 'bar' => [7] }, { 'foo' => [22, 42] }, { 'foo' => [] }])
22
- n = term.predict(maps, { a: 1 })
22
+ n = term.predict(maps, {})
23
23
  assert_equal(2, n.size)
24
24
  assert_kind_of(Factbase::Taped, n)
25
25
  end
@@ -29,7 +29,7 @@ class TestIndexedTerm < Factbase::Test
29
29
  idx = {}
30
30
  term.redress!(Factbase::IndexedTerm, idx:)
31
31
  maps = Factbase::Taped.new([{ 'foo' => [42] }, { 'bar' => [7] }, { 'foo' => [22, 42] }, { 'foo' => [] }])
32
- n = term.predict(maps, { a: 1 })
32
+ n = term.predict(maps, {})
33
33
  assert_equal(3, n.size)
34
34
  assert_kind_of(Factbase::Taped, n)
35
35
  end
@@ -39,7 +39,7 @@ class TestIndexedTerm < Factbase::Test
39
39
  idx = {}
40
40
  term.redress!(Factbase::IndexedTerm, idx:)
41
41
  maps = Factbase::Taped.new([{ 'foo' => [42] }, { 'bar' => [7] }, { 'foo' => [22, 42] }, { 'foo' => [] }])
42
- n = term.predict(maps, { a: 1 })
42
+ n = term.predict(maps, {})
43
43
  assert_equal(1, n.size)
44
44
  assert_kind_of(Factbase::Taped, n)
45
45
  end
@@ -49,7 +49,7 @@ class TestIndexedTerm < Factbase::Test
49
49
  idx = {}
50
50
  term.redress!(Factbase::IndexedTerm, idx:)
51
51
  maps = Factbase::Taped.new([{ 'foo' => [42] }, { 'bar' => [7], 'foo' => [22, 42] }, { 'foo' => [22] }])
52
- n = term.predict(maps, { a: 1 })
52
+ n = term.predict(maps, {})
53
53
  assert_equal(1, n.size)
54
54
  assert_kind_of(Factbase::Taped, n)
55
55
  end
@@ -59,13 +59,13 @@ class TestIndexedTerm < Factbase::Test
59
59
  :and,
60
60
  [
61
61
  Factbase::Term.new(:eq, [:foo, 42]),
62
- Factbase::Term.new(:eq, %i[bar $a])
62
+ Factbase::Term.new(:eq, %i[bar $jeff])
63
63
  ]
64
64
  )
65
65
  idx = {}
66
66
  term.redress!(Factbase::IndexedTerm, idx:)
67
67
  maps = Factbase::Taped.new([{ 'foo' => [42] }, { 'bar' => [7], 'foo' => [22, 42] }, { 'foo' => [22, 42] }])
68
- n = term.predict(maps, { 'a' => [7] })
68
+ n = term.predict(maps, Factbase::Tee.new({}, { 'jeff' => [7] }))
69
69
  assert_equal(1, n.size)
70
70
  assert_kind_of(Factbase::Taped, n)
71
71
  end
@@ -90,7 +90,7 @@ class TestIndexedTerm < Factbase::Test
90
90
  idx = {}
91
91
  term.redress!(Factbase::IndexedTerm, idx:)
92
92
  maps = Factbase::Taped.new([{ 'foo' => [42] }, { 'bar' => [7], 'foo' => [22, 42] }, { 'foo' => [22, 42] }])
93
- n = term.predict(maps, { a: 1 })
93
+ n = term.predict(maps, {})
94
94
  assert_equal(3, n.size)
95
95
  assert_kind_of(Factbase::Taped, n)
96
96
  end
@@ -100,7 +100,7 @@ class TestIndexedTerm < Factbase::Test
100
100
  idx = {}
101
101
  term.redress!(Factbase::IndexedTerm, idx:)
102
102
  maps = Factbase::Taped.new([{ 'foo' => [42] }, { 'alpha' => [] }, {}])
103
- n = term.predict(maps, { a: 1 })
103
+ n = term.predict(maps, {})
104
104
  assert_nil(n)
105
105
  end
106
106
  end
@@ -57,7 +57,7 @@ class TestAggregates < Factbase::Test
57
57
  { 'a' => [4], 'b' => [55] }
58
58
  ]
59
59
  t = Factbase::Syntax.new('(empty (eq b $x))').to_term
60
- assert(t.evaluate(Factbase::Fact.new({ 'x' => 42 }), maps, Factbase.new))
61
- refute(t.evaluate(Factbase::Fact.new({ 'x' => 44 }), maps, Factbase.new))
60
+ assert(t.evaluate(Factbase::Fact.new({ 'x' => [42] }), maps, Factbase.new))
61
+ refute(t.evaluate(Factbase::Fact.new({ 'x' => [44] }), maps, Factbase.new))
62
62
  end
63
63
  end
@@ -156,18 +156,22 @@ class TestQuery < Factbase::Test
156
156
 
157
157
  def test_reading_one
158
158
  maps = [
159
- { 'foo' => [42] },
159
+ { 'foo' => [42], 'hello' => [4] },
160
160
  { 'bar' => [4, 5] }
161
161
  ]
162
162
  with_factbases(maps) do |badge, fb|
163
163
  {
164
+ '(agg (and (eq foo 42) (eq hello $v)) (min foo))' => 42,
165
+ '(agg (or (eq foo 42) (eq bar 4)) (min foo))' => 42,
164
166
  '(agg (exists foo) (first foo))' => [42],
165
167
  '(agg (exists z) (first z))' => nil,
166
168
  '(agg (always) (count))' => 2,
167
169
  '(agg (eq bar $v) (count))' => 1,
170
+ '(agg (eq foo 42) (min foo))' => 42,
171
+ '(agg (and (eq foo 42)) (min foo))' => 42,
168
172
  '(agg (eq z 40) (count))' => 0
169
173
  }.each do |q, expected|
170
- result = fb.query(q).one(fb, v: 4)
174
+ result = fb.query(q).one(fb, 'v' => 4)
171
175
  if expected.nil?
172
176
  assert_nil(result, "#{q} -> nil in #{badge}")
173
177
  else
@@ -190,6 +194,16 @@ class TestQuery < Factbase::Test
190
194
  end
191
195
  end
192
196
 
197
+ def test_finds_with_subsitution
198
+ maps = [{ 'foo' => [42] }, { 'bar' => [7] }, { 'foo' => [666] }]
199
+ with_factbases(maps) do |badge, fb|
200
+ assert_equal(1, fb.query('(eq 2 (agg (eq foo $foo) (count)))').each.to_a.size, "with #{badge}")
201
+ fb.txn do |fbt|
202
+ assert_equal(1, fbt.query('(eq 2 (agg (eq foo $foo) (count)))').each.to_a.size, "with #{badge} (txn)")
203
+ end
204
+ end
205
+ end
206
+
193
207
  def test_scans_and_inserts
194
208
  with_factbases do |_, fb|
195
209
  fb.insert.foo = 42
@@ -277,8 +291,8 @@ class TestQuery < Factbase::Test
277
291
  found += 1
278
292
  end
279
293
  assert_equal(1, found)
280
- assert_equal(1, Factbase::Query.new(maps, '(eq foo $bar)', Factbase.new).each(Factbase.new, bar: 42).to_a.size)
281
- assert_equal(0, Factbase::Query.new(maps, '(eq foo $bar)', Factbase.new).each(Factbase.new, bar: 555).to_a.size)
294
+ assert_equal(1, Factbase::Query.new(maps, '(eq foo $bar)', Factbase.new).each(Factbase.new, bar: [42]).to_a.size)
295
+ assert_equal(0, Factbase::Query.new(maps, '(eq foo $bar)', Factbase.new).each(Factbase.new, bar: [555]).to_a.size)
282
296
  end
283
297
 
284
298
  def test_with_nil_alias
@@ -37,10 +37,10 @@ class TestTee < Factbase::Test
37
37
  map = {}
38
38
  prim = Factbase::Fact.new(map)
39
39
  prim.foo = 42
40
- t = Factbase::Tee.new(Factbase::Fact.new({}), { 'bar' => 7 })
41
- assert_equal(7, t['$bar'])
40
+ t = Factbase::Tee.new(Factbase::Fact.new({}), { 'bar' => [7] })
41
+ assert_equal([7], t['$bar'])
42
42
  t = Factbase::Tee.new(prim, t)
43
- assert_equal(7, t['$bar'])
43
+ assert_equal([7], t['$bar'])
44
44
  end
45
45
 
46
46
  def test_prints_to_string
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factbase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.7
4
+ version: 0.9.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko