factbase 0.0.33 → 0.0.34

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: 4434f46130fcb63e9fa798f3c6bacf8441e91a680f789015faf03ba5cb2296b3
4
- data.tar.gz: 2d461a92eb2121ca9394e9c07128afa48e3f62a06290678eb68a869fb6fdd501
3
+ metadata.gz: 20c84ac9e67311e6333da8c6da3d69ae445aad64686017a69bc702f50d703223
4
+ data.tar.gz: c275ca48a224b42f09e71551b563585036b866f99acf8dd58e13311bdaa0beec
5
5
  SHA512:
6
- metadata.gz: 4a8683f418df4a0bd0de1d1f8be145a2eb4b1ea2e3bb7993355df88b591b6195dbac3654da7222ff3d66c1f3255c4dfb98520ff68f45721527138e258726d454
7
- data.tar.gz: 027e1d0262aeca3ef34ea2a9ab3bdca0e6f6975c0f5478bee62e707f15668281bf2950be04f6cfca636d55a0b79ba9cd38e9a5679d8f63b7d800f69943b9b40c
6
+ metadata.gz: 1ef21c5e33b3cf5f67e7f5edb56110e9f4d62c970cbc52158ca3922661d9a3d3ed411c26b95b2be97c1d6b6a4a5ebb5c521c87401325da42e585bd61f31b403f
7
+ data.tar.gz: 9f02e0addedcff827c76f7c5a58cae70c84b73c02522ab429ba0607bf967f0412c8a82ba1b8caa49c0c0329498f0f440dc95e861fed563362bd1f7295bea52ba
@@ -64,6 +64,8 @@ class Factbase::Looged
64
64
 
65
65
  # Fact decorator.
66
66
  class Fact
67
+ MAX_LENGTH = 64
68
+
67
69
  def initialize(fact, loog)
68
70
  @fact = fact
69
71
  @loog = loog
@@ -79,7 +81,7 @@ class Factbase::Looged
79
81
  v = args[1]
80
82
  s = v.is_a?(Time) ? v.utc.iso8601 : v.to_s
81
83
  s = v.to_s.inspect if v.is_a?(String)
82
- s = "#{s[0..40]}...#{s[-40..]}" if s.length > 80
84
+ s = "#{s[0..MAX_LENGTH / 2]}...#{s[-MAX_LENGTH / 2..]}" if s.length > MAX_LENGTH
83
85
  @loog.debug("Set '#{k[0..-2]}' to #{s} (#{v.class})") if k.end_with?('=')
84
86
  r
85
87
  end
@@ -20,20 +20,42 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  # SOFTWARE.
22
22
 
23
- require 'minitest/autorun'
24
- require_relative '../../lib/factbase'
25
- require_relative '../../lib/factbase/white_list'
23
+ require_relative '../factbase'
26
24
 
27
- # WhiteList test.
25
+ # Tuples.
28
26
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
29
27
  # Copyright:: Copyright (c) 2024 Yegor Bugayenko
30
28
  # License:: MIT
31
- class TestWhiteList < Minitest::Test
32
- def test_simple_white_listing
33
- fb = Factbase::WhiteList.new(Factbase.new, 'foo', ['hello'])
34
- fb.insert.foo = 'hello'
35
- assert_raises do
36
- fb.insert.foo = 42
29
+ class Factbase::Tuples
30
+ def initialize(fb, queries)
31
+ @fb = fb
32
+ @queries = queries
33
+ end
34
+
35
+ # Iterate them one by one.
36
+ # @yield [Array<Fact>] Arrays of facts one-by-one
37
+ # @return [Integer] Total number of arrays yielded
38
+ def each(&)
39
+ return to_enum(__method__) unless block_given?
40
+ each_rec([], @queries, &)
41
+ end
42
+
43
+ private
44
+
45
+ def each_rec(facts, tail, &)
46
+ qq = tail.dup
47
+ q = qq.shift
48
+ return if q.nil?
49
+ qt = q.gsub(/\{f([0-9]+).([a-z0-9_]+)\}/) do
50
+ facts[Regexp.last_match[1].to_i].send(Regexp.last_match[2])
51
+ end
52
+ @fb.query(qt).each do |f|
53
+ fs = facts + [f]
54
+ if qq.empty?
55
+ yield fs
56
+ else
57
+ each_rec(fs, qq, &)
58
+ end
37
59
  end
38
60
  end
39
61
  end
data/lib/factbase.rb CHANGED
@@ -29,7 +29,7 @@ require 'yaml'
29
29
  # License:: MIT
30
30
  class Factbase
31
31
  # Current version of the gem (changed by .rultor.yml on every release)
32
- VERSION = '0.0.33'
32
+ VERSION = '0.0.34'
33
33
 
34
34
  # Constructor.
35
35
  def initialize(facts = [])
@@ -90,7 +90,7 @@ class TestLooged < Minitest::Test
90
90
  'Inserted new fact #1',
91
91
  'Inserted new fact #2',
92
92
  'Set \'bar\' to 3 (Integer)',
93
- 'Set \'str\' to "Он поскорей звонит. Вбегает\n К нем...м\n Отъехать в поле к двум дубкам." (String)',
93
+ 'Set \'str\' to "Он поскорей звонит. Вбегает\n ... Отъехать в поле к двум дубкам." (String)',
94
94
  'Found 1 fact(s) by \'(exists bar)\'',
95
95
  'Deleted 3 fact(s) by \'(not (exists bar))\''
96
96
  ].each do |s|
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2024 Yegor Bugayenko
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the 'Software'), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ require 'minitest/autorun'
24
+ require_relative '../../lib/factbase'
25
+ require_relative '../../lib/factbase/tuples'
26
+
27
+ # Test.
28
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
29
+ # Copyright:: Copyright (c) 2024 Yegor Bugayenko
30
+ # License:: MIT
31
+ class TestTuples < Minitest::Test
32
+ def test_passes_facts
33
+ fb = Factbase.new
34
+ f1 = fb.insert
35
+ f1.foo = 42
36
+ f2 = fb.insert
37
+ f2.bar = 55
38
+ Factbase::Tuples.new(fb, ['(exists foo)', '(exists bar)']).each do |a, b|
39
+ assert_equal(42, a.foo)
40
+ assert_equal(55, b.bar)
41
+ end
42
+ end
43
+
44
+ def test_with_empty_list_of_queries
45
+ fb = Factbase.new
46
+ f1 = fb.insert
47
+ f1.foo = 42
48
+ tuples = Factbase::Tuples.new(fb, [])
49
+ assert(tuples.each.to_a.empty?)
50
+ end
51
+
52
+ def test_is_reusable
53
+ fb = Factbase.new
54
+ f1 = fb.insert
55
+ f1.foo = 42
56
+ tuples = Factbase::Tuples.new(fb, ['(exists foo)'])
57
+ assert_equal(1, tuples.each.to_a.size)
58
+ assert_equal(1, tuples.each.to_a.size)
59
+ end
60
+
61
+ def test_with_modifications
62
+ fb = Factbase.new
63
+ f1 = fb.insert
64
+ f1.foo = 42
65
+ Factbase::Tuples.new(fb, ['(exists foo)']).each do |fs|
66
+ fs[0].bar = 1
67
+ end
68
+ assert_equal(1, fb.query('(exists bar)').each.to_a.size)
69
+ end
70
+
71
+ def test_with_txn
72
+ fb = Factbase.new
73
+ f1 = fb.insert
74
+ f1.foo = 42
75
+ Factbase::Tuples.new(fb, ['(exists foo)']).each do |fs|
76
+ fb.txn do |fbt|
77
+ f = fbt.insert
78
+ f.bar = 1
79
+ end
80
+ fs[0].xyz = 'hey'
81
+ end
82
+ assert_equal(1, fb.query('(exists bar)').each.to_a.size)
83
+ assert_equal(1, fb.query('(exists xyz)').each.to_a.size)
84
+ end
85
+ end
@@ -110,8 +110,7 @@ class TestFactbase < Minitest::Test
110
110
  Factbase::Rules.new(Factbase.new, '(always)'),
111
111
  Factbase::Inv.new(Factbase.new) { |_, _| true },
112
112
  Factbase::Pre.new(Factbase.new) { |_| true },
113
- Factbase::Looged.new(Factbase.new, Loog::NULL),
114
- Factbase::Spy.new(Factbase.new, 'ff')
113
+ Factbase::Looged.new(Factbase.new, Loog::NULL)
115
114
  ].each do |d|
116
115
  f = d.insert
117
116
  f.foo = 42
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.0.33
4
+ version: 0.0.34
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
@@ -103,13 +103,12 @@ files:
103
103
  - lib/factbase/pre.rb
104
104
  - lib/factbase/query.rb
105
105
  - lib/factbase/rules.rb
106
- - lib/factbase/spy.rb
107
106
  - lib/factbase/syntax.rb
108
107
  - lib/factbase/term.rb
109
108
  - lib/factbase/to_json.rb
110
109
  - lib/factbase/to_xml.rb
111
110
  - lib/factbase/to_yaml.rb
112
- - lib/factbase/white_list.rb
111
+ - lib/factbase/tuples.rb
113
112
  - renovate.json
114
113
  - test/factbase/test_fact.rb
115
114
  - test/factbase/test_inv.rb
@@ -117,13 +116,12 @@ files:
117
116
  - test/factbase/test_pre.rb
118
117
  - test/factbase/test_query.rb
119
118
  - test/factbase/test_rules.rb
120
- - test/factbase/test_spy.rb
121
119
  - test/factbase/test_syntax.rb
122
120
  - test/factbase/test_term.rb
123
121
  - test/factbase/test_to_json.rb
124
122
  - test/factbase/test_to_xml.rb
125
123
  - test/factbase/test_to_yaml.rb
126
- - test/factbase/test_white_list.rb
124
+ - test/factbase/test_tuples.rb
127
125
  - test/test__helper.rb
128
126
  - test/test_factbase.rb
129
127
  homepage: http://github.com/yegor256/factbase.rb
data/lib/factbase/spy.rb DELETED
@@ -1,101 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright (c) 2024 Yegor Bugayenko
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the 'Software'), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in all
13
- # copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- # SOFTWARE.
22
-
23
- require_relative '../factbase'
24
-
25
- # Spy.
26
- # Author:: Yegor Bugayenko (yegor256@gmail.com)
27
- # Copyright:: Copyright (c) 2024 Yegor Bugayenko
28
- # License:: MIT
29
- class Factbase::Spy
30
- def initialize(fb, key)
31
- @fb = fb
32
- @key = key
33
- @caught = []
34
- end
35
-
36
- def caught_keys
37
- @caught
38
- end
39
-
40
- def dup
41
- Factbase::Spy.new(@fb.dup, @key)
42
- end
43
-
44
- def size
45
- @fb.size
46
- end
47
-
48
- def query(expr)
49
- scan(Factbase::Syntax.new(expr).to_term)
50
- @fb.query(expr)
51
- end
52
-
53
- def insert
54
- SpyFact.new(@fb.insert, @key, @caught)
55
- end
56
-
57
- def export
58
- @fb.export
59
- end
60
-
61
- def txn(this = self, &)
62
- @fb.txn(this, &)
63
- end
64
-
65
- def import(data)
66
- @fb.import(data)
67
- end
68
-
69
- # A fact that is spying.
70
- class SpyFact
71
- def initialize(fact, key, caught)
72
- @fact = fact
73
- @key = key
74
- @caught = caught
75
- end
76
-
77
- def method_missing(*args)
78
- @caught << args[1] if args[0].to_s == "#{@key}="
79
- @fact.method_missing(*args)
80
- end
81
-
82
- # rubocop:disable Style/OptionalBooleanParameter
83
- def respond_to?(method, include_private = false)
84
- # rubocop:enable Style/OptionalBooleanParameter
85
- @fact.respond_to?(method, include_private)
86
- end
87
-
88
- def respond_to_missing?(method, include_private = false)
89
- @fact.respond_to_missing?(method, include_private)
90
- end
91
- end
92
-
93
- private
94
-
95
- def scan(term)
96
- @caught << term.operands[1] if term.op == :eq && term.operands[0].to_s == @key
97
- term.operands.each do |o|
98
- scan(o) if o.is_a?(Factbase::Term)
99
- end
100
- end
101
- end
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright (c) 2024 Yegor Bugayenko
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the 'Software'), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in all
13
- # copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- # SOFTWARE.
22
-
23
- require_relative '../factbase'
24
-
25
- # White list.
26
- # Author:: Yegor Bugayenko (yegor256@gmail.com)
27
- # Copyright:: Copyright (c) 2024 Yegor Bugayenko
28
- # License:: MIT
29
- class Factbase::WhiteList
30
- def initialize(fb, key, list)
31
- @fb = fb
32
- @key = key
33
- @allowed = list
34
- end
35
-
36
- def query(expr)
37
- @fb.query(expr)
38
- end
39
-
40
- def insert
41
- WhiteFact.new(@fb.insert, @key, @allowed)
42
- end
43
-
44
- def export
45
- @fb.export
46
- end
47
-
48
- def import(data)
49
- @fb.import(data)
50
- end
51
-
52
- def to_json(opt = nil)
53
- @fb.to_json(opt)
54
- end
55
-
56
- # A fact that is allows only values from the list.
57
- class WhiteFact
58
- def initialize(fact, key, list)
59
- @fact = fact
60
- @key = key
61
- @allowed = list
62
- end
63
-
64
- def method_missing(*args)
65
- raise "#{args[0]} '#{args[1]}' not allowed" if args[0].to_s == "#{@key}=" && !@allowed.include?(args[1])
66
- @fact.method_missing(*args)
67
- end
68
-
69
- # rubocop:disable Style/OptionalBooleanParameter
70
- def respond_to?(method, include_private = false)
71
- # rubocop:enable Style/OptionalBooleanParameter
72
- @fact.respond_to?(method, include_private)
73
- end
74
-
75
- def respond_to_missing?(method, include_private = false)
76
- @fact.respond_to_missing?(method, include_private)
77
- end
78
- end
79
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright (c) 2024 Yegor Bugayenko
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the 'Software'), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in all
13
- # copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- # SOFTWARE.
22
-
23
- require 'minitest/autorun'
24
- require_relative '../../lib/factbase'
25
- require_relative '../../lib/factbase/spy'
26
-
27
- # Spy test.
28
- # Author:: Yegor Bugayenko (yegor256@gmail.com)
29
- # Copyright:: Copyright (c) 2024 Yegor Bugayenko
30
- # License:: MIT
31
- class TestSpy < Minitest::Test
32
- def test_simple_spying
33
- fb = Factbase::Spy.new(Factbase.new, 'foo')
34
- fb.insert.foo = 42
35
- fb.insert.foo = 'hello'
36
- fb.query('(eq foo "hello")').each do |f|
37
- assert_equal('hello', f.foo)
38
- end
39
- assert_equal(3, fb.caught_keys.size)
40
- assert(fb.caught_keys.include?('hello'))
41
- assert(fb.caught_keys.include?(42))
42
- end
43
- end