factbase 0.3.0 → 0.4.0

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: da15ba272d70a23c1f7138efd317a27e19f37846c7305b005e75fb23d9222004
4
- data.tar.gz: 37588b7eb0b03659b5e6373a4f42bda9e89cd652cc954bc6a7988faf705d35ad
3
+ metadata.gz: a78379a3da6efaffa3672290e0ec7f38f2ec5b83032719388e07b517a4fb9e99
4
+ data.tar.gz: c172a777ddbb8e3799cf8e1fb55437a5172086724a8ed623ed08ff373ece7b87
5
5
  SHA512:
6
- metadata.gz: ba914de891e44ee3580e42be96710c3a2deecd75c2ba7fae07d78bb84920af2ffbf602b587e0aa81c1b138b9603f0c58903c21a933177e04e3fdcf1124d367b6
7
- data.tar.gz: 3c681187d0df5285eb0922f7ae718c12009c26adb84c9a9ba7ad2676c095e5dcd68e01dce538a87c52f02e3475d62e4833349995c6d69cd444f3a9730fd4ba52
6
+ metadata.gz: 39ee04049fe211ef67de1eee8281ced0f6c2cba9eb132d80ab10c4d0d6dc3c22dde24675a1ee5810cf0b53be9c8f1e4e8afc622873537f44179cd9251fccff9e
7
+ data.tar.gz: 62390b4ef6e29ead3174f6b6a6d68d72e69cf2a35d667faf6c5aff05f310f1e42360da6917798f999a411e37894863b3956babf849d837fbc3ec4ced9306510d
@@ -23,7 +23,7 @@
23
23
  require 'others'
24
24
  require_relative '../factbase'
25
25
 
26
- # Accumulator of props.
26
+ # Accumulator of props, a decorator of +Factbase::Fact+.
27
27
  #
28
28
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
29
29
  # Copyright:: Copyright (c) 2024 Yegor Bugayenko
data/lib/factbase/pre.rb CHANGED
@@ -24,7 +24,18 @@ require 'loog'
24
24
  require 'decoor'
25
25
  require_relative '../factbase'
26
26
 
27
- # A decorator of a Factbase, that runs a provided block on every +insert+.
27
+ # A decorator of a +Factbase+, that runs a provided block on every +insert+.
28
+ #
29
+ # For example, you can use this decorator if you want to put some properties
30
+ # into every fact that gets into the factbase:
31
+ #
32
+ # fb = Factbase::Pre.new(Factbase.new) do |f, fbt|
33
+ # f.when = Time.now
34
+ # end
35
+ #
36
+ # The second argument passed to the block is the factbase, while the first
37
+ # one is the fact just inserted.
38
+ #
28
39
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
29
40
  # Copyright:: Copyright (c) 2024 Yegor Bugayenko
30
41
  # License:: MIT
@@ -43,7 +54,7 @@ class Factbase::Pre
43
54
 
44
55
  def insert
45
56
  f = @fb.insert
46
- @block.call(f)
57
+ @block.call(f, self)
47
58
  f
48
59
  end
49
60
 
data/lib/factbase.rb CHANGED
@@ -81,7 +81,7 @@ require 'yaml'
81
81
  # License:: MIT
82
82
  class Factbase
83
83
  # Current version of the gem (changed by .rultor.yml on every release)
84
- VERSION = '0.3.0'
84
+ VERSION = '0.4.0'
85
85
 
86
86
  # An exception that may be thrown in a transaction, to roll it back.
87
87
  class Rollback < StandardError; end
@@ -67,4 +67,13 @@ class TestAccum < Minitest::Test
67
67
  a = Factbase::Accum.new(f, {}, false)
68
68
  assert(a['foo'].nil?)
69
69
  end
70
+
71
+ def test_prints_to_string
72
+ map = {}
73
+ f = Factbase::Fact.new(Mutex.new, map)
74
+ props = {}
75
+ a = Factbase::Accum.new(f, props, true)
76
+ a.foo = 42
77
+ assert_equal('[ foo: [42] ]', f.to_s)
78
+ end
70
79
  end
@@ -35,4 +35,18 @@ class TestPre < Minitest::Test
35
35
  assert_equal(42, f.foo)
36
36
  assert_equal(1, fb.query('(always)').each.to_a.size)
37
37
  end
38
+
39
+ def test_in_transaction
40
+ fb =
41
+ Factbase::Pre.new(Factbase.new) do |f, fbt|
42
+ f.total = fbt.size
43
+ end
44
+ fb.txn do |fbt|
45
+ fbt.insert
46
+ fbt.insert
47
+ end
48
+ arr = fb.query('(always)').each.to_a
49
+ assert_equal(1, arr[0].total)
50
+ assert_equal(2, arr[1].total)
51
+ end
38
52
  end
@@ -58,7 +58,10 @@ class TestRules < Minitest::Test
58
58
  '(when (exists a) (exists b))'
59
59
  )
60
60
  f = fb.insert
61
- assert(f.to_s.length.positive?)
61
+ f.foo = 42
62
+ s = f.to_s
63
+ assert(s.length.positive?, s)
64
+ assert_equal('[ foo: [42] ]', s)
62
65
  end
63
66
 
64
67
  def test_query_one
@@ -59,4 +59,13 @@ class TestTee < Minitest::Test
59
59
  t = Factbase::Tee.new(prim, t)
60
60
  assert_equal(7, t['$bar'])
61
61
  end
62
+
63
+ def test_prints_to_string
64
+ prim = Factbase::Fact.new(Mutex.new, {})
65
+ prim.foo = 42
66
+ upper = Factbase::Fact.new(Mutex.new, {})
67
+ upper.bar = 13
68
+ t = Factbase::Tee.new(prim, upper)
69
+ assert_equal('[ foo: [42] ]', t.to_s)
70
+ end
62
71
  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.3.0
4
+ version: 0.4.0
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-08-08 00:00:00.000000000 Z
11
+ date: 2024-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backtrace