factbase 0.0.48 → 0.0.50

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: 56a6a221e2344538c324a601f49ad655f4d5e3b6682b5e717e8f798a6eb46030
4
- data.tar.gz: 94bb595173fbea968c4a8e8af2b8d6cf08689127e2dfd2c2c296420659da5304
3
+ metadata.gz: f62ac48c3d2c181b0064e519947fa2fed0ecb0b81660663c91319233eb2fc529
4
+ data.tar.gz: f3131120095c0ece6cb01313bad9f55d58145ee289dfc4a4f8bd7e949778521b
5
5
  SHA512:
6
- metadata.gz: fe4eb95604ae3ba22d24ef01a4c86b1dcdf4c9b3ff38969cbc459653a6a72a201d317bdf86f71c22206c771deebb5511ba1c64ce014c77fdf995e18926646fa2
7
- data.tar.gz: b48c9c7243f74713bcf5b9fbad3f0ef901aa79d107d7be78e3d08d9970e898a21f9bb4938cfaac5057edc11c53150b0934a46b125019c132906114a702c90d89
6
+ metadata.gz: d7325ad6538622ef5e8764547314ac2970827b44eb70dd37acde333505bd630370bf4ccfe7d1b35916592b89b4dd346c82bcb0c2f2d7532994cfbef0d2a23467
7
+ data.tar.gz: eb8554fb580ec058551d6f4f43e47dac22ff13ee427195595a1d9f40caf7f51c59c0c697a849136618a625addf82f726b658752caf755b7eab84b3482a0a8b28
@@ -32,7 +32,7 @@ jobs:
32
32
  strategy:
33
33
  matrix:
34
34
  os: [ubuntu-20.04, macos-12, windows-2022]
35
- ruby: [3.2, 3.3]
35
+ ruby: [3.2]
36
36
  runs-on: ${{ matrix.os }}
37
37
  steps:
38
38
  - uses: actions/checkout@v4
data/Gemfile.lock CHANGED
@@ -5,14 +5,15 @@ PATH
5
5
  json (~> 2.7)
6
6
  loog (~> 0.2)
7
7
  nokogiri (~> 1.10)
8
+ tago (~> 0.0)
8
9
  yaml (~> 0.3)
9
10
 
10
11
  GEM
11
12
  remote: https://rubygems.org/
12
13
  specs:
13
- actionpack (7.1.3.3)
14
- actionview (= 7.1.3.3)
15
- activesupport (= 7.1.3.3)
14
+ actionpack (7.1.3.4)
15
+ actionview (= 7.1.3.4)
16
+ activesupport (= 7.1.3.4)
16
17
  nokogiri (>= 1.8.5)
17
18
  racc
18
19
  rack (>= 2.2.4)
@@ -20,13 +21,13 @@ GEM
20
21
  rack-test (>= 0.6.3)
21
22
  rails-dom-testing (~> 2.2)
22
23
  rails-html-sanitizer (~> 1.6)
23
- actionview (7.1.3.3)
24
- activesupport (= 7.1.3.3)
24
+ actionview (7.1.3.4)
25
+ activesupport (= 7.1.3.4)
25
26
  builder (~> 3.1)
26
27
  erubi (~> 1.11)
27
28
  rails-dom-testing (~> 2.2)
28
29
  rails-html-sanitizer (~> 1.6)
29
- activesupport (7.1.3.3)
30
+ activesupport (7.1.3.4)
30
31
  base64
31
32
  bigdecimal
32
33
  concurrent-ruby (~> 1.0, >= 1.0.2)
@@ -39,8 +40,8 @@ GEM
39
40
  ast (2.4.2)
40
41
  base64 (0.2.0)
41
42
  bigdecimal (3.1.8)
42
- builder (3.2.4)
43
- concurrent-ruby (1.2.3)
43
+ builder (3.3.0)
44
+ concurrent-ruby (1.3.3)
44
45
  connection_pool (2.4.1)
45
46
  crass (1.0.6)
46
47
  diff-lcs (1.5.1)
@@ -69,14 +70,14 @@ GEM
69
70
  racc (~> 1.4)
70
71
  nokogiri (1.16.5-x86_64-linux)
71
72
  racc (~> 1.4)
72
- parallel (1.24.0)
73
- parser (3.3.2.0)
73
+ parallel (1.25.1)
74
+ parser (3.3.3.0)
74
75
  ast (~> 2.4.1)
75
76
  racc
76
77
  psych (5.1.2)
77
78
  stringio
78
79
  racc (1.8.0)
79
- rack (3.0.11)
80
+ rack (3.1.3)
80
81
  rack-session (2.0.0)
81
82
  rack (>= 3.0.0)
82
83
  rack-test (2.1.0)
@@ -91,9 +92,9 @@ GEM
91
92
  rails-html-sanitizer (1.6.0)
92
93
  loofah (~> 2.21)
93
94
  nokogiri (~> 1.14)
94
- railties (7.1.3.3)
95
- actionpack (= 7.1.3.3)
96
- activesupport (= 7.1.3.3)
95
+ railties (7.1.3.4)
96
+ actionpack (= 7.1.3.4)
97
+ activesupport (= 7.1.3.4)
97
98
  irb
98
99
  rackup (>= 1.0.0)
99
100
  rake (>= 12.2)
@@ -104,10 +105,10 @@ GEM
104
105
  rdoc (6.7.0)
105
106
  psych (>= 4.0.0)
106
107
  regexp_parser (2.9.2)
107
- reline (0.5.7)
108
+ reline (0.5.9)
108
109
  io-console (~> 0.5)
109
- rexml (3.2.8)
110
- strscan (>= 3.0.9)
110
+ rexml (3.3.0)
111
+ strscan
111
112
  rspec-core (3.13.0)
112
113
  rspec-support (~> 3.13.0)
113
114
  rspec-expectations (3.13.0)
@@ -138,10 +139,10 @@ GEM
138
139
  unicode-display_width (>= 2.4.0, < 3.0)
139
140
  rubocop-ast (1.31.3)
140
141
  parser (>= 3.3.1.0)
141
- rubocop-capybara (2.20.0)
142
- rubocop (~> 1.41)
143
- rubocop-factory_bot (2.25.1)
142
+ rubocop-capybara (2.21.0)
144
143
  rubocop (~> 1.41)
144
+ rubocop-factory_bot (2.26.1)
145
+ rubocop (~> 1.61)
145
146
  rubocop-performance (1.21.0)
146
147
  rubocop (>= 1.48.1, < 2.0)
147
148
  rubocop-ast (>= 1.31.1, < 2.0)
@@ -150,8 +151,8 @@ GEM
150
151
  rubocop-capybara (~> 2.17)
151
152
  rubocop-factory_bot (~> 2.22)
152
153
  rubocop-rspec_rails (~> 2.28)
153
- rubocop-rspec_rails (2.28.3)
154
- rubocop (~> 1.40)
154
+ rubocop-rspec_rails (2.29.1)
155
+ rubocop (~> 1.61)
155
156
  ruby-progressbar (1.13.0)
156
157
  simplecov (0.22.0)
157
158
  docile (~> 1.1)
@@ -164,6 +165,7 @@ GEM
164
165
  simplecov_json_formatter (0.1.4)
165
166
  stringio (3.1.0)
166
167
  strscan (3.1.0)
168
+ tago (0.0.2)
167
169
  thor (1.3.1)
168
170
  tzinfo (2.0.6)
169
171
  concurrent-ruby (~> 1.0)
data/factbase.gemspec CHANGED
@@ -42,9 +42,10 @@ Gem::Specification.new do |s|
42
42
  s.files = `git ls-files`.split($RS)
43
43
  s.rdoc_options = ['--charset=UTF-8']
44
44
  s.extra_rdoc_files = ['README.md', 'LICENSE.txt']
45
- s.add_runtime_dependency 'json', '~> 2.7'
45
+ s.add_runtime_dependency 'json', '~>2.7'
46
46
  s.add_runtime_dependency 'loog', '~>0.2'
47
- s.add_runtime_dependency 'nokogiri', '~> 1.10'
48
- s.add_runtime_dependency 'yaml', '~> 0.3'
47
+ s.add_runtime_dependency 'nokogiri', '~>1.10'
48
+ s.add_runtime_dependency 'tago', '~>0.0'
49
+ s.add_runtime_dependency 'yaml', '~>0.3'
49
50
  s.metadata['rubygems_mfa_required'] = 'true'
50
51
  end
@@ -22,6 +22,7 @@
22
22
 
23
23
  require 'time'
24
24
  require 'loog'
25
+ require 'tago'
25
26
  require_relative 'syntax'
26
27
 
27
28
  # A decorator of a Factbase, that logs all operations.
@@ -53,14 +54,22 @@ class Factbase::Looged
53
54
  end
54
55
 
55
56
  def txn(this = self, &)
57
+ start = Time.now
56
58
  before = @fb.size
57
- tail = nil
59
+ id = nil
60
+ rollback = false
58
61
  r = @fb.txn(this) do |fbt|
59
- tail = Factbase::Looged.elapsed do
60
- yield fbt
61
- end
62
+ id = fbt.object_id
63
+ yield fbt
64
+ rescue Factbase::Rollback => e
65
+ rollback = true
66
+ raise e
67
+ end
68
+ if rollback
69
+ @loog.debug("Txn ##{id} rolled back in #{start.ago}")
70
+ else
71
+ @loog.debug("Txn ##{id} #{r ? 'modified' : 'didn\'t touch'} #{before} facts in #{start.ago}")
62
72
  end
63
- @loog.debug("Txn #{r ? 'modified' : 'didn\'t touch'} #{before} facts #{tail}")
64
73
  r
65
74
  end
66
75
 
@@ -172,6 +181,6 @@ class Factbase::Looged
172
181
  def self.elapsed
173
182
  start = Time.now
174
183
  yield
175
- "in #{format('%.2f', (Time.now - start) * 1000)}ms"
184
+ "in #{start.ago}"
176
185
  end
177
186
  end
@@ -24,18 +24,30 @@ require_relative '../factbase'
24
24
  require_relative '../factbase/syntax'
25
25
 
26
26
  # A decorator of a Factbase, that checks rules on every set.
27
+ #
28
+ # Say, you want every fact to have +foo+ property. You want any attempt
29
+ # to insert a fact without this property to lead to a runtime error. Here is how:
30
+ #
31
+ # fb = Factbase.new
32
+ # fb = Factabase::Rules.new(fb, '(exists foo)')
33
+ # fb.txn do |fbt|
34
+ # f = fbt.insert
35
+ # f.bar = 3
36
+ # end # Runtime exception here (transaction won't commit)
37
+ #
27
38
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
28
39
  # Copyright:: Copyright (c) 2024 Yegor Bugayenko
29
40
  # License:: MIT
30
41
  class Factbase::Rules
31
- def initialize(fb, rules, check = Check.new(rules))
42
+ def initialize(fb, rules, check = Check.new(rules), uid: nil)
32
43
  @fb = fb
33
44
  @rules = rules
34
45
  @check = check
46
+ @uid = uid
35
47
  end
36
48
 
37
49
  def dup
38
- Factbase::Rules.new(@fb.dup, @rules, @check)
50
+ Factbase::Rules.new(@fb.dup, @rules, @check, uid: @uid)
39
51
  end
40
52
 
41
53
  def size
@@ -52,15 +64,16 @@ class Factbase::Rules
52
64
 
53
65
  def txn(this = self, &)
54
66
  before = @check
55
- @check = Blind.new
56
- modified = @fb.txn(this, &)
57
- @check = before
58
- if modified
59
- @fb.query('(always)').each do |f|
67
+ later = Later.new(@uid)
68
+ @check = later
69
+ @fb.txn(this) do |fbt|
70
+ yield fbt
71
+ @check = before
72
+ fbt.query('(always)').each do |f|
73
+ next unless later.include?(f)
60
74
  @check.it(f)
61
75
  end
62
76
  end
63
- modified
64
77
  end
65
78
 
66
79
  def export
@@ -88,7 +101,7 @@ class Factbase::Rules
88
101
  def method_missing(*args)
89
102
  r = @fact.method_missing(*args)
90
103
  k = args[0].to_s
91
- @check.it(self) if k.end_with?('=')
104
+ @check.it(@fact) if k.end_with?('=')
92
105
  r
93
106
  end
94
107
 
@@ -143,9 +156,19 @@ class Factbase::Rules
143
156
  # Check one fact (never complaining).
144
157
  #
145
158
  # This is an internal class, it is not supposed to be instantiated directly.
146
- class Blind
147
- def it(_fact)
148
- true
159
+ class Later
160
+ def initialize(uid)
161
+ @uid = uid
162
+ @facts = Set.new
163
+ end
164
+
165
+ def it(fact)
166
+ @facts << fact.send(@uid) unless @uid.nil?
167
+ end
168
+
169
+ def include?(fact)
170
+ return true if @uid.nil?
171
+ @facts.include?(fact.send(@uid))
149
172
  end
150
173
  end
151
174
  end
data/lib/factbase.rb CHANGED
@@ -79,7 +79,7 @@ require 'yaml'
79
79
  # License:: MIT
80
80
  class Factbase
81
81
  # Current version of the gem (changed by .rultor.yml on every release)
82
- VERSION = '0.0.48'
82
+ VERSION = '0.0.50'
83
83
 
84
84
  # An exception that may be thrown in a transaction, to roll it back.
85
85
  class Rollback < StandardError; end
@@ -30,8 +30,8 @@ require_relative '../../lib/factbase/flatten'
30
30
  # License:: MIT
31
31
  class TestFlatten < Minitest::Test
32
32
  def test_mapping
33
- maps = [{ 'b' => [42] }, { 'a' => 33 }, { 'c' => %w[hey you] }]
34
- to = Factbase::Flatten.new(maps).it
33
+ maps = [{ 'b' => [42], 'i' => 1 }, { 'a' => 33, 'i' => 0 }, { 'c' => %w[hey you], 'i' => 2 }]
34
+ to = Factbase::Flatten.new(maps, 'i').it
35
35
  assert(33, to[0]['a'])
36
36
  assert(42, to[1]['b'])
37
37
  assert(2, to[2]['c'].size)
@@ -57,6 +57,15 @@ class TestLooged < Minitest::Test
57
57
  assert(log.to_s.include?('modified'), log)
58
58
  end
59
59
 
60
+ def test_with_txn_rollback
61
+ log = Loog::Buffer.new
62
+ fb = Factbase::Looged.new(Factbase.new, log)
63
+ assert(!fb.txn { raise Factbase::Rollback })
64
+ assert_equal(0, fb.size)
65
+ assert(log.to_s.include?('rolled back'), log)
66
+ assert(!log.to_s.include?('didn\'t touch'), log)
67
+ end
68
+
60
69
  def test_with_modifying_txn
61
70
  log = Loog::Buffer.new
62
71
  fb = Factbase::Looged.new(Factbase.new, log)
@@ -46,11 +46,13 @@ class TestQuery < Minitest::Test
46
46
  maps = []
47
47
  maps << { 'num' => 42, 'name' => 'Jeff' }
48
48
  maps << { 'pi' => 3.14, 'num' => [42, 66, 0], 'name' => 'peter' }
49
- maps << { 'time' => Time.now - 100, 'num' => 0, 'hi' => [4] }
49
+ maps << { 'time' => Time.now - 100, 'num' => 0, 'hi' => [4], 'nome' => ['Walter'] }
50
50
  {
51
51
  '(eq num 444)' => 0,
52
+ '(eq hi 4)' => 1,
52
53
  '(eq time 0)' => 0,
53
54
  '(gt num 60)' => 1,
55
+ '(gt hi 3)' => 1,
54
56
  "(and (lt pi 100) \n\n (gt num 1000))" => 0,
55
57
  '(exists pi)' => 1,
56
58
  '(eq pi +3.14)' => 1,
@@ -70,6 +72,7 @@ class TestQuery < Minitest::Test
70
72
  '(and (one pi) (eq pi (div -6.28 -2)))' => 1,
71
73
  '(gt (size num) 2)' => 1,
72
74
  '(matches name "^[a-z]+$")' => 1,
75
+ '(matches nome "^Walter$")' => 1,
73
76
  '(lt (size num) 2)' => 2,
74
77
  '(eq (size _hello) 0)' => 3,
75
78
  '(eq num pi)' => 0,
@@ -23,6 +23,7 @@
23
23
  require 'minitest/autorun'
24
24
  require_relative '../../lib/factbase'
25
25
  require_relative '../../lib/factbase/rules'
26
+ require_relative '../../lib/factbase/pre'
26
27
 
27
28
  # Test.
28
29
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
@@ -66,6 +67,18 @@ class TestRules < Minitest::Test
66
67
  assert(ok)
67
68
  end
68
69
 
70
+ def test_rollback_on_violation
71
+ fb = Factbase::Rules.new(Factbase.new, '(when (exists a) (exists b))')
72
+ assert_raises do
73
+ fb.txn do |fbt|
74
+ f = fbt.insert
75
+ f.a = 1
76
+ f.c = 2
77
+ end
78
+ end
79
+ assert_equal(0, fb.size)
80
+ end
81
+
69
82
  def test_in_combination_with_pre
70
83
  fb = Factbase::Rules.new(Factbase.new, '(when (exists a) (exists b))')
71
84
  fb = Factbase::Pre.new(fb) do |f|
@@ -81,6 +94,6 @@ class TestRules < Minitest::Test
81
94
  end
82
95
  end
83
96
  assert(ok)
84
- assert_equal(1, fb.query('(eq hello 42)').each.to_a.size)
97
+ assert_equal(0, fb.query('(eq hello 42)').each.to_a.size)
85
98
  end
86
99
  end
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.48
4
+ version: 0.0.50
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-06-11 00:00:00.000000000 Z
11
+ date: 2024-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: tago
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: yaml
57
71
  requirement: !ruby/object:Gem::Requirement