factbase 0.0.15 → 0.0.17

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: 8ea11eaa2c72f162214fffedf64db4674ef27aef158f613ecea3304e35d528c3
4
- data.tar.gz: b2c11cab690d59314a866a668b18913df9856ff8dcd8e0fd1ff906dc6601bd41
3
+ metadata.gz: d7ba6b0db6da2ae4a2075896ff5b8365716d881eab694e0c848063cfb0b19270
4
+ data.tar.gz: 53e1f409d20404d60827545927f1b88797708889d18fc0177d176a7ae93e9a68
5
5
  SHA512:
6
- metadata.gz: d8a5a44265b0f9c6e32e669802831fec089e04049e2871167bbda0361ea99ad3b464f16cda94c5fd7b32b2cc539379b5e33acfc8e26c2488cfe605c61d3fe537
7
- data.tar.gz: 010122d90a6564bc5d7041ed1d3107449062810e494504bc14bb6c64df71f0e7bbf077fc0300301e31d2078a3efc34ce8b5c0b722eefeb288befd465b139eb27
6
+ metadata.gz: 5f2db2983906007c6d4e12bb63b730e277d055c0f4768f41f6bc3ac68a6bccf6fa59226fa8ab417c41f0f53dfa163af59d7732e9082800494268e3d047de6f0c
7
+ data.tar.gz: 9c77ce233f61637e0bec39b72da43fdc5403d0e098b4f4505747bce634233810acaaa88e65aa59c6ded4f8aef870b25823f04f051999ec9e221a26b5520960f9
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.15'
29
+ s.version = '0.0.17'
30
30
  s.license = 'MIT'
31
31
  s.summary = 'Factbase'
32
32
  s.description = 'Fact base in memory and on disc'
@@ -38,6 +38,7 @@ Gem::Specification.new do |s|
38
38
  s.rdoc_options = ['--charset=UTF-8']
39
39
  s.extra_rdoc_files = ['README.md', 'LICENSE.txt']
40
40
  s.add_runtime_dependency 'json', '~> 2.7'
41
+ s.add_runtime_dependency 'loog', '~>0.2'
41
42
  s.add_runtime_dependency 'nokogiri', '~> 1.10'
42
43
  s.add_runtime_dependency 'yaml', '~> 0.3'
43
44
  s.metadata['rubygems_mfa_required'] = 'true'
@@ -0,0 +1,122 @@
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 'loog'
24
+
25
+ # A decorator of a Factbase, that logs all operations.
26
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
27
+ # Copyright:: Copyright (c) 2024 Yegor Bugayenko
28
+ # License:: MIT
29
+ class Factbase::Looged
30
+ def initialize(fb, loog)
31
+ @fb = fb
32
+ @loog = loog
33
+ end
34
+
35
+ def empty?
36
+ @fb.empty?
37
+ end
38
+
39
+ def size
40
+ @fb.size
41
+ end
42
+
43
+ def insert
44
+ f = @fb.insert
45
+ @loog.debug("Inserted fact ##{f.id}")
46
+ Fact.new(f, @loog)
47
+ end
48
+
49
+ def query(query)
50
+ Query.new(@fb.query(query), query, @loog)
51
+ end
52
+
53
+ def export
54
+ @fb.export
55
+ end
56
+
57
+ def import(bytes)
58
+ @fb.import(bytes)
59
+ end
60
+
61
+ def to_json(opt = nil)
62
+ @fb.to_json(opt)
63
+ end
64
+
65
+ def to_xml
66
+ @fb.to_xml
67
+ end
68
+
69
+ def to_yaml
70
+ @fb.to_yaml
71
+ end
72
+
73
+ # Fact decorator.
74
+ class Fact
75
+ def initialize(fact, loog)
76
+ @fact = fact
77
+ @loog = loog
78
+ end
79
+
80
+ def to_s
81
+ @fact.to_s
82
+ end
83
+
84
+ def method_missing(*args)
85
+ r = @fact.method_missing(*args)
86
+ k = args[0].to_s
87
+ @loog.debug("Set '#{k[0..-2]}' to '#{args[1]}'") if k.end_with?('=')
88
+ r
89
+ end
90
+
91
+ # rubocop:disable Style/OptionalBooleanParameter
92
+ def respond_to?(method, include_private = false)
93
+ # rubocop:enable Style/OptionalBooleanParameter
94
+ @fact.respond_to?(method, include_private)
95
+ end
96
+
97
+ def respond_to_missing?(method, include_private = false)
98
+ @fact.respond_to_missing?(method, include_private)
99
+ end
100
+ end
101
+
102
+ # Query decorator.
103
+ class Query
104
+ def initialize(query, expr, loog)
105
+ @query = query
106
+ @expr = expr
107
+ @loog = loog
108
+ end
109
+
110
+ def each(&)
111
+ r = @query.each(&)
112
+ @loog.debug("Found #{r} facts by '#{@expr}'")
113
+ r
114
+ end
115
+
116
+ def delete!
117
+ r = @query.delete!
118
+ @loog.debug("Deleted #{r} facts by '#{@expr}'")
119
+ r
120
+ end
121
+ end
122
+ end
@@ -99,7 +99,7 @@ class Factbase::Syntax
99
99
  list << :open
100
100
  when ')'
101
101
  list << :close
102
- when ' '
102
+ when ' ', "\n", "\t", "\r"
103
103
  # ignore it
104
104
  else
105
105
  acc += c
data/lib/factbase.rb CHANGED
@@ -35,6 +35,12 @@ class Factbase
35
35
  @mutex = Mutex.new
36
36
  end
37
37
 
38
+ # Is it empty?
39
+ # @return [Boolean] TRUE if there are no facts inside
40
+ def empty?
41
+ @maps.empty?
42
+ end
43
+
38
44
  # Size.
39
45
  # @return [Integer] How many facts are in there
40
46
  def size
@@ -0,0 +1,46 @@
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 'loog'
25
+ require_relative '../../lib/factbase/looged'
26
+
27
+ # Test.
28
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
29
+ # Copyright:: Copyright (c) 2024 Yegor Bugayenko
30
+ # License:: MIT
31
+ class TestLooged < Minitest::Test
32
+ def test_simple_setting
33
+ fb = Factbase::Looged.new(Factbase.new, Loog::NULL)
34
+ fb.insert
35
+ fb.insert.bar = 3
36
+ found = 0
37
+ fb.query('(exists id)').each do |f|
38
+ assert(42, f.id.positive?)
39
+ f.foo = 42
40
+ assert_equal(42, f.foo)
41
+ found += 1
42
+ end
43
+ assert_equal(2, found)
44
+ assert_equal(2, fb.size)
45
+ end
46
+ end
@@ -50,12 +50,12 @@ class TestQuery < Minitest::Test
50
50
  '(eq num 444)' => 0,
51
51
  '(eq time 0)' => 0,
52
52
  '(gt num 60)' => 1,
53
- '(and (lt pi 100) (gt num 1000))' => 0,
53
+ "(and (lt pi 100) \n\n (gt num 1000))" => 0,
54
54
  '(exists pi)' => 1,
55
55
  '(not (exists hello))' => 3,
56
56
  '(absent time)' => 2,
57
57
  '(and (absent time) (exists pi))' => 1,
58
- '(and (exists time) (not (exists pi)))' => 1,
58
+ "(and (exists time) (not (\t\texists pi)))" => 1,
59
59
  "(or (eq num 66) (lt time #{(Time.now - 200).utc.iso8601}))" => 1
60
60
  }.each do |q, r|
61
61
  assert_equal(r, Factbase::Query.new(maps, Mutex.new, q).each.to_a.size, q)
@@ -44,8 +44,8 @@ class TestSyntax < Minitest::Test
44
44
  '()',
45
45
  '(foo)',
46
46
  '(foo (bar) (zz 77) )',
47
- "(eq foo 'Hello, world!')",
48
- "(or ( a 4) (b 5) () (and () (c 5) (r 7 8s 8is 'Foo')))"
47
+ "(eq foo \n\n 'Hello, world!'\n)\n",
48
+ "(or ( a 4) (b 5) () (and () (c 5) \t\t(r 7 8s 8is 'Foo')))"
49
49
  ].each do |q|
50
50
  Factbase::Syntax.new(q).to_term
51
51
  end
@@ -57,6 +57,13 @@ class TestFactbase < Minitest::Test
57
57
  assert_equal(1, f2.query('(eq foo 42)').each.to_a.count)
58
58
  end
59
59
 
60
+ def test_empty_or_not
61
+ fb = Factbase.new
62
+ assert(fb.empty?)
63
+ fb.insert
64
+ assert(!fb.empty?)
65
+ end
66
+
60
67
  def test_to_json
61
68
  fb = Factbase.new
62
69
  f = fb.insert
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factbase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-14 00:00:00.000000000 Z
11
+ date: 2024-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: loog
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.2'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: nokogiri
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -82,6 +96,7 @@ files:
82
96
  - factbase.gemspec
83
97
  - lib/factbase.rb
84
98
  - lib/factbase/fact.rb
99
+ - lib/factbase/looged.rb
85
100
  - lib/factbase/query.rb
86
101
  - lib/factbase/spy.rb
87
102
  - lib/factbase/syntax.rb
@@ -89,6 +104,7 @@ files:
89
104
  - lib/factbase/white_list.rb
90
105
  - renovate.json
91
106
  - test/factbase/test_fact.rb
107
+ - test/factbase/test_looged.rb
92
108
  - test/factbase/test_query.rb
93
109
  - test/factbase/test_spy.rb
94
110
  - test/factbase/test_syntax.rb