factbase 0.0.18 → 0.0.20
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 +4 -4
- data/factbase.gemspec +1 -1
- data/lib/factbase/fact.rb +4 -0
- data/lib/factbase/looged.rb +24 -9
- data/lib/factbase/syntax.rb +2 -0
- data/lib/factbase/term.rb +6 -1
- data/test/factbase/test_looged.rb +38 -0
- data/test/factbase/test_query.rb +7 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd5f2d977ba317805b5306d1a7a04c6f26e8cd6be228c254fdcd6ae322c9ecfb
|
4
|
+
data.tar.gz: a0f30e312586a23952f0df00a2ddf16de2818cafca8043d03acce21a0fbfa29b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 056656b1a3034c5e5e7025de734767d5c42957ba02e2f276a55a4257f4adaf3e01bf15b28488a8fd7727718f9d36980872d80cb5f63c0cf0a85675c93d6e712c
|
7
|
+
data.tar.gz: 1639980c0f9de4db14857068f97f5b3931027aa1ec49b3bdbe402828c017dc4c8b0bcd0469f6d17f0329cd3008748e06d044a0448fef4f2af4f6dce6e930f4c6
|
data/factbase.gemspec
CHANGED
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
|
|
26
26
|
s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
|
27
27
|
s.required_ruby_version = '>=2.3'
|
28
28
|
s.name = 'factbase'
|
29
|
-
s.version = '0.0.
|
29
|
+
s.version = '0.0.20'
|
30
30
|
s.license = 'MIT'
|
31
31
|
s.summary = 'Factbase'
|
32
32
|
s.description = 'Fact base in memory and on disc'
|
data/lib/factbase/fact.rb
CHANGED
@@ -21,6 +21,7 @@
|
|
21
21
|
# SOFTWARE.
|
22
22
|
|
23
23
|
require 'json'
|
24
|
+
require 'time'
|
24
25
|
require_relative '../factbase'
|
25
26
|
|
26
27
|
# Fact.
|
@@ -33,10 +34,13 @@ class Factbase::Fact
|
|
33
34
|
@map = map
|
34
35
|
end
|
35
36
|
|
37
|
+
# Convert it to a string.
|
38
|
+
# @return [String] String representation of it (in JSON)
|
36
39
|
def to_s
|
37
40
|
@map.to_json
|
38
41
|
end
|
39
42
|
|
43
|
+
# When a method is missing, this method is called.
|
40
44
|
def method_missing(*args)
|
41
45
|
k = args[0].to_s
|
42
46
|
if k.end_with?('=')
|
data/lib/factbase/looged.rb
CHANGED
@@ -109,23 +109,38 @@ class Factbase::Looged
|
|
109
109
|
end
|
110
110
|
|
111
111
|
def each(&)
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
112
|
+
if block_given?
|
113
|
+
r = @query.each(&)
|
114
|
+
raise ".each of #{@query.class} returned #{r.class}" unless r.is_a?(Integer)
|
115
|
+
if r.zero?
|
116
|
+
@loog.debug("Nothing found by '#{@expr}'")
|
117
|
+
else
|
118
|
+
@loog.debug("Found #{r} fact(s) by '#{@expr}'")
|
119
|
+
end
|
120
|
+
r
|
116
121
|
else
|
117
|
-
|
122
|
+
array = []
|
123
|
+
# rubocop:disable Style/MapIntoArray
|
124
|
+
@query.each do |f|
|
125
|
+
array << f
|
126
|
+
end
|
127
|
+
# rubocop:enable Style/MapIntoArray
|
128
|
+
if array.empty?
|
129
|
+
@loog.debug("Nothing found by '#{@expr}'")
|
130
|
+
else
|
131
|
+
@loog.debug("Found #{array.size} fact(s) by '#{@expr}'")
|
132
|
+
end
|
133
|
+
array
|
118
134
|
end
|
119
|
-
r
|
120
135
|
end
|
121
136
|
|
122
137
|
def delete!
|
123
138
|
r = @query.delete!
|
124
|
-
|
125
|
-
if
|
139
|
+
raise ".delete! of #{@query.class} returned #{r.class}" unless r.is_a?(Integer)
|
140
|
+
if r.zero?
|
126
141
|
@loog.debug("Nothing deleted by '#{@expr}'")
|
127
142
|
else
|
128
|
-
@loog.debug("Deleted #{r
|
143
|
+
@loog.debug("Deleted #{r} fact(s) by '#{@expr}'")
|
129
144
|
end
|
130
145
|
r
|
131
146
|
end
|
data/lib/factbase/syntax.rb
CHANGED
data/lib/factbase/term.rb
CHANGED
@@ -30,18 +30,23 @@ require_relative 'fact'
|
|
30
30
|
class Factbase::Term
|
31
31
|
attr_reader :op, :operands
|
32
32
|
|
33
|
+
# Ctor.
|
34
|
+
# @param [Symbol] operator Operator
|
35
|
+
# @param [Array] operands Operands
|
33
36
|
def initialize(operator, operands)
|
34
37
|
@op = operator
|
35
38
|
@operands = operands
|
36
39
|
end
|
37
40
|
|
38
41
|
# Does it match the fact?
|
39
|
-
# @param [Factbase::Fact] The fact
|
42
|
+
# @param [Factbase::Fact] fact The fact
|
40
43
|
# @return [bool] TRUE if matches
|
41
44
|
def matches?(fact)
|
42
45
|
send(@op, fact)
|
43
46
|
end
|
44
47
|
|
48
|
+
# Turns it into a string.
|
49
|
+
# @return [String] The string of it
|
45
50
|
def to_s
|
46
51
|
items = []
|
47
52
|
items << @op
|
@@ -43,4 +43,42 @@ class TestLooged < Minitest::Test
|
|
43
43
|
assert_equal(2, found)
|
44
44
|
assert_equal(2, fb.size)
|
45
45
|
end
|
46
|
+
|
47
|
+
def test_returns_int
|
48
|
+
fb = Factbase.new
|
49
|
+
fb.insert
|
50
|
+
fb.insert
|
51
|
+
assert_equal(2, Factbase::Looged.new(fb, Loog::NULL).query('()').each(&:to_s))
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_returns_int_when_empty
|
55
|
+
fb = Factbase.new
|
56
|
+
assert_equal(0, Factbase::Looged.new(fb, Loog::NULL).query('()').each(&:to_s))
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_logs_when_enumerator
|
60
|
+
fb = Factbase::Looged.new(Factbase.new, Loog::NULL)
|
61
|
+
assert_equal(0, fb.query('()').each.to_a.size)
|
62
|
+
fb.insert
|
63
|
+
assert_equal(1, fb.query('()').each.to_a.size)
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_proper_logging
|
67
|
+
log = Loog::Buffer.new
|
68
|
+
fb = Factbase::Looged.new(Factbase.new, log)
|
69
|
+
fb.insert
|
70
|
+
fb.insert.bar = 3
|
71
|
+
fb.insert
|
72
|
+
fb.query('(exists bar)').each(&:to_s)
|
73
|
+
fb.query('(not (exists bar))').delete!
|
74
|
+
[
|
75
|
+
'Inserted fact #1',
|
76
|
+
'Inserted fact #2',
|
77
|
+
'Set \'bar\' to \'"3"\' (Integer)',
|
78
|
+
'Found 1 fact(s) by \'(exists bar)\'',
|
79
|
+
'Deleted 2 fact(s) by \'(not (exists bar))\''
|
80
|
+
].each do |s|
|
81
|
+
assert(log.to_s.include?("#{s}\n"), "#{log}\n")
|
82
|
+
end
|
83
|
+
end
|
46
84
|
end
|
data/test/factbase/test_query.rb
CHANGED
@@ -77,4 +77,11 @@ class TestQuery < Minitest::Test
|
|
77
77
|
maps << { 'foo' => [42] }
|
78
78
|
assert_equal(1, Factbase::Query.new(maps, Mutex.new, '(eq foo 42)').each.to_a.size)
|
79
79
|
end
|
80
|
+
|
81
|
+
def test_returns_int
|
82
|
+
maps = []
|
83
|
+
maps << { 'foo' => 1 }
|
84
|
+
q = Factbase::Query.new(maps, Mutex.new, '(eq foo 1)')
|
85
|
+
assert_equal(1, q.each(&:to_s))
|
86
|
+
end
|
80
87
|
end
|