factbase 0.0.23 → 0.0.24

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: 17e8ed984f9d03fcd9885a54fc01e555dc9c9dc561754e3e51fccbc39a44b846
4
+ data.tar.gz: adb616eb6b3eb3793b8d9d4413f8504d6122bf1bc1c14d4d3b5a02c3722a6cd0
5
5
  SHA512:
6
- metadata.gz: b96169f610fc86f97ea99e646f4bbe7a5cb76b0b693699df75186327172b102f892db91f677bfd55dbddbc6c6c3bf9bd2e3481d6331bdc5197c7f7fb4c184454
7
- data.tar.gz: c002777263b5e57350c0365b10033284aa6bff207b8f03e0d8d9f65504dd5a79f94aa2cdf1df5a2e4f28f840e3d27d703b3740927c7514e064aeb57411031511
6
+ metadata.gz: f52fb4f8f4ecfb4e9d72c02960f038e4b6cd539e64f17adb8866d556c7fa0fbd4f0ec897f4203f730736d819ecd3368c003d640704e790b2f8a202d13d9d8bdc
7
+ data.tar.gz: 347ee42c1a93dc731281c4f8fa4026ba105fbb46c3a34886c8cadbd440becfc95ec01300cfbaa9ef0e9ea565c71e2357823fe4cdd3c0d24cf7966ec3fbe23a4e
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.24'
30
30
  s.license = 'MIT'
31
31
  s.summary = 'Factbase'
32
32
  s.description = 'Fact base in memory and on disc'
@@ -0,0 +1,114 @@
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
+ @query.each do |f|
106
+ yield Fact.new(f, @block)
107
+ end
108
+ end
109
+
110
+ def delete!
111
+ @query.delete!
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,56 @@
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
+ end
45
+
46
+ def test_pre_and_inv
47
+ fb = Factbase::Inv.new(Factbase.new) do |p, v|
48
+ raise 'oops' if v.is_a?(String) && p == 'b'
49
+ end
50
+ fb = Factbase::Pre.new(fb) do |f|
51
+ f.id = 42
52
+ end
53
+ f = fb.insert
54
+ assert_equal(42, f.id)
55
+ end
56
+ 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.24
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