factbase 0.0.23 → 0.0.25

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
  SHA256:
3
- metadata.gz: 27e9aaa353b400d0004ccefcb53fda8fa0d1ac8f2a6d2f4eae8892dbbf2b503f
4
- data.tar.gz: 746387ad73281024c9f41c3b444c8986e0a064af0bb0531b12777ef1637d0e3c
3
+ metadata.gz: d9e0276640bc70144f57a3926e20b6e2d44063da6fe7236fb956867a06e102ba
4
+ data.tar.gz: c7826200059da686fe4025aadae2aa1d83eda7e061e4e72c888e327a3d24adda
5
5
  SHA512:
6
- metadata.gz: b96169f610fc86f97ea99e646f4bbe7a5cb76b0b693699df75186327172b102f892db91f677bfd55dbddbc6c6c3bf9bd2e3481d6331bdc5197c7f7fb4c184454
7
- data.tar.gz: c002777263b5e57350c0365b10033284aa6bff207b8f03e0d8d9f65504dd5a79f94aa2cdf1df5a2e4f28f840e3d27d703b3740927c7514e064aeb57411031511
6
+ metadata.gz: 8362694429ca241aaa592d36f662b2939ddaec04017ae37326611db4c9a697ab95321f6b3d406a9f4d65be86f9f42ba019ca4c8ec0e8b3ec5c77967bf75107d2
7
+ data.tar.gz: 7c96e521d770381536b956e43781c909f597d39b8098178b0c0b77e15e4733c2eaded65a91704ca81c42f214168ad89505f8cfe6830e84e994c247213ef138df
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.23'
29
+ s.version = '0.0.25'
30
30
  s.license = 'MIT'
31
31
  s.summary = 'Factbase'
32
32
  s.description = 'Fact base in memory and on disc'
@@ -0,0 +1,115 @@
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
+ # A decorator of a Factbase, that checks invariants on every set.
24
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
25
+ # Copyright:: Copyright (c) 2024 Yegor Bugayenko
26
+ # License:: MIT
27
+ class Factbase::Inv
28
+ def initialize(fb, &block)
29
+ @fb = fb
30
+ @block = block
31
+ end
32
+
33
+ def empty?
34
+ @fb.empty?
35
+ end
36
+
37
+ def size
38
+ @fb.size
39
+ end
40
+
41
+ def insert
42
+ Fact.new(@fb.insert, @block)
43
+ end
44
+
45
+ def query(query)
46
+ Query.new(@fb.query(query), @block)
47
+ end
48
+
49
+ def export
50
+ @fb.export
51
+ end
52
+
53
+ def import(bytes)
54
+ @fb.import(bytes)
55
+ end
56
+
57
+ def to_json(opt = nil)
58
+ @fb.to_json(opt)
59
+ end
60
+
61
+ def to_xml
62
+ @fb.to_xml
63
+ end
64
+
65
+ def to_yaml
66
+ @fb.to_yaml
67
+ end
68
+
69
+ # Fact decorator.
70
+ class Fact
71
+ def initialize(fact, block)
72
+ @fact = fact
73
+ @block = block
74
+ end
75
+
76
+ def to_s
77
+ @fact.to_s
78
+ end
79
+
80
+ def method_missing(*args)
81
+ k = args[0].to_s
82
+ @block.call(k[0..-2], args[1]) if k.end_with?('=')
83
+ @fact.method_missing(*args)
84
+ end
85
+
86
+ # rubocop:disable Style/OptionalBooleanParameter
87
+ def respond_to?(method, include_private = false)
88
+ # rubocop:enable Style/OptionalBooleanParameter
89
+ @fact.respond_to?(method, include_private)
90
+ end
91
+
92
+ def respond_to_missing?(method, include_private = false)
93
+ @fact.respond_to_missing?(method, include_private)
94
+ end
95
+ end
96
+
97
+ # Query decorator.
98
+ class Query
99
+ def initialize(query, block)
100
+ @query = query
101
+ @block = block
102
+ end
103
+
104
+ def each
105
+ return to_enum(__method__) unless block_given?
106
+ @query.each do |f|
107
+ yield Fact.new(f, @block)
108
+ end
109
+ end
110
+
111
+ def delete!
112
+ @query.delete!
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,57 @@
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/inv'
26
+ require_relative '../../lib/factbase/pre'
27
+
28
+ # Test.
29
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
30
+ # Copyright:: Copyright (c) 2024 Yegor Bugayenko
31
+ # License:: MIT
32
+ class TestInv < Minitest::Test
33
+ def test_simple_checking
34
+ fb = Factbase::Inv.new(Factbase.new) do |p, v|
35
+ raise 'oops' if v.is_a?(String) && p == 'b'
36
+ end
37
+ f = fb.insert
38
+ f.a = 42
39
+ assert_raises do
40
+ f.b = 'here we should crash'
41
+ end
42
+ f.c = 256
43
+ assert_equal(42, f.a)
44
+ assert_equal(1, fb.query('()').each.to_a.size)
45
+ end
46
+
47
+ def test_pre_and_inv
48
+ fb = Factbase::Inv.new(Factbase.new) do |p, v|
49
+ raise 'oops' if v.is_a?(String) && p == 'b'
50
+ end
51
+ fb = Factbase::Pre.new(fb) do |f|
52
+ f.id = 42
53
+ end
54
+ f = fb.insert
55
+ assert_equal(42, f.id)
56
+ end
57
+ end
@@ -33,5 +33,6 @@ class TestPre < Minitest::Test
33
33
  fb = Factbase::Pre.new(Factbase.new) { |f| f.foo = 42 }
34
34
  f = fb.insert
35
35
  assert_equal(42, f.foo)
36
+ assert_equal(1, fb.query('()').each.to_a.size)
36
37
  end
37
38
  end
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.23
4
+ version: 0.0.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
@@ -97,6 +97,7 @@ files:
97
97
  - factbase.gemspec
98
98
  - lib/factbase.rb
99
99
  - lib/factbase/fact.rb
100
+ - lib/factbase/inv.rb
100
101
  - lib/factbase/looged.rb
101
102
  - lib/factbase/pre.rb
102
103
  - lib/factbase/query.rb
@@ -106,6 +107,7 @@ files:
106
107
  - lib/factbase/white_list.rb
107
108
  - renovate.json
108
109
  - test/factbase/test_fact.rb
110
+ - test/factbase/test_inv.rb
109
111
  - test/factbase/test_looged.rb
110
112
  - test/factbase/test_pre.rb
111
113
  - test/factbase/test_query.rb