factbase 0.0.18 → 0.0.20

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: e0ff9bc1f66c58988b0a6758b11694dc5cbcc0b7c004f862ec00fd075b599ec1
4
- data.tar.gz: 9c75cc130b0b641cbcf67bab0f11cc0b794a68e6449fb0c3e1fac38b337194e7
3
+ metadata.gz: fd5f2d977ba317805b5306d1a7a04c6f26e8cd6be228c254fdcd6ae322c9ecfb
4
+ data.tar.gz: a0f30e312586a23952f0df00a2ddf16de2818cafca8043d03acce21a0fbfa29b
5
5
  SHA512:
6
- metadata.gz: eaa74d8e6c7f30a4f743aa69b51186803bfeb6959b1e9e2f8a929e7ad8813aeff3bc2beb4431157440bd8af5991bf163390fceb3ba2cb200b5167901aa2521f9
7
- data.tar.gz: cb79dff273324c4292187f048ed1f7ec8e9559ad391242c071e5e097141e6b7bb42f501fc684e01f4c5adb4fc30ade033115719acbeb4393cd9465765ff246d9
6
+ metadata.gz: 056656b1a3034c5e5e7025de734767d5c42957ba02e2f276a55a4257f4adaf3e01bf15b28488a8fd7727718f9d36980872d80cb5f63c0cf0a85675c93d6e712c
7
+ data.tar.gz: 1639980c0f9de4db14857068f97f5b3931027aa1ec49b3bdbe402828c017dc4c8b0bcd0469f6d17f0329cd3008748e06d044a0448fef4f2af4f6dce6e930f4c6
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.18'
29
+ s.version = '0.0.20'
30
30
  s.license = 'MIT'
31
31
  s.summary = 'Factbase'
32
32
  s.description = 'Fact base in memory and on disc'
data/lib/factbase/fact.rb CHANGED
@@ -21,6 +21,7 @@
21
21
  # SOFTWARE.
22
22
 
23
23
  require 'json'
24
+ require 'time'
24
25
  require_relative '../factbase'
25
26
 
26
27
  # Fact.
@@ -33,10 +34,13 @@ class Factbase::Fact
33
34
  @map = map
34
35
  end
35
36
 
37
+ # Convert it to a string.
38
+ # @return [String] String representation of it (in JSON)
36
39
  def to_s
37
40
  @map.to_json
38
41
  end
39
42
 
43
+ # When a method is missing, this method is called.
40
44
  def method_missing(*args)
41
45
  k = args[0].to_s
42
46
  if k.end_with?('=')
@@ -109,23 +109,38 @@ class Factbase::Looged
109
109
  end
110
110
 
111
111
  def each(&)
112
- r = @query.each(&)
113
- c = r.size
114
- if c.zero?
115
- @loog.debug("Nothing found by '#{@expr}'")
112
+ if block_given?
113
+ r = @query.each(&)
114
+ raise ".each of #{@query.class} returned #{r.class}" unless r.is_a?(Integer)
115
+ if r.zero?
116
+ @loog.debug("Nothing found by '#{@expr}'")
117
+ else
118
+ @loog.debug("Found #{r} fact(s) by '#{@expr}'")
119
+ end
120
+ r
116
121
  else
117
- @loog.debug("Found #{c} facts by '#{@expr}'")
122
+ array = []
123
+ # rubocop:disable Style/MapIntoArray
124
+ @query.each do |f|
125
+ array << f
126
+ end
127
+ # rubocop:enable Style/MapIntoArray
128
+ if array.empty?
129
+ @loog.debug("Nothing found by '#{@expr}'")
130
+ else
131
+ @loog.debug("Found #{array.size} fact(s) by '#{@expr}'")
132
+ end
133
+ array
118
134
  end
119
- r
120
135
  end
121
136
 
122
137
  def delete!
123
138
  r = @query.delete!
124
- c = r.size
125
- if c.zero?
139
+ raise ".delete! of #{@query.class} returned #{r.class}" unless r.is_a?(Integer)
140
+ if r.zero?
126
141
  @loog.debug("Nothing deleted by '#{@expr}'")
127
142
  else
128
- @loog.debug("Deleted #{r.size} facts by '#{@expr}'")
143
+ @loog.debug("Deleted #{r} fact(s) by '#{@expr}'")
129
144
  end
130
145
  r
131
146
  end
@@ -29,6 +29,8 @@ require_relative 'term'
29
29
  # Copyright:: Copyright (c) 2024 Yegor Bugayenko
30
30
  # License:: MIT
31
31
  class Factbase::Syntax
32
+ # Ctor.
33
+ # @param [String] query The query, for example "(eq id 42)"
32
34
  def initialize(query)
33
35
  @query = query
34
36
  end
data/lib/factbase/term.rb CHANGED
@@ -30,18 +30,23 @@ require_relative 'fact'
30
30
  class Factbase::Term
31
31
  attr_reader :op, :operands
32
32
 
33
+ # Ctor.
34
+ # @param [Symbol] operator Operator
35
+ # @param [Array] operands Operands
33
36
  def initialize(operator, operands)
34
37
  @op = operator
35
38
  @operands = operands
36
39
  end
37
40
 
38
41
  # Does it match the fact?
39
- # @param [Factbase::Fact] The fact
42
+ # @param [Factbase::Fact] fact The fact
40
43
  # @return [bool] TRUE if matches
41
44
  def matches?(fact)
42
45
  send(@op, fact)
43
46
  end
44
47
 
48
+ # Turns it into a string.
49
+ # @return [String] The string of it
45
50
  def to_s
46
51
  items = []
47
52
  items << @op
@@ -43,4 +43,42 @@ class TestLooged < Minitest::Test
43
43
  assert_equal(2, found)
44
44
  assert_equal(2, fb.size)
45
45
  end
46
+
47
+ def test_returns_int
48
+ fb = Factbase.new
49
+ fb.insert
50
+ fb.insert
51
+ assert_equal(2, Factbase::Looged.new(fb, Loog::NULL).query('()').each(&:to_s))
52
+ end
53
+
54
+ def test_returns_int_when_empty
55
+ fb = Factbase.new
56
+ assert_equal(0, Factbase::Looged.new(fb, Loog::NULL).query('()').each(&:to_s))
57
+ end
58
+
59
+ def test_logs_when_enumerator
60
+ fb = Factbase::Looged.new(Factbase.new, Loog::NULL)
61
+ assert_equal(0, fb.query('()').each.to_a.size)
62
+ fb.insert
63
+ assert_equal(1, fb.query('()').each.to_a.size)
64
+ end
65
+
66
+ def test_proper_logging
67
+ log = Loog::Buffer.new
68
+ fb = Factbase::Looged.new(Factbase.new, log)
69
+ fb.insert
70
+ fb.insert.bar = 3
71
+ fb.insert
72
+ fb.query('(exists bar)').each(&:to_s)
73
+ fb.query('(not (exists bar))').delete!
74
+ [
75
+ 'Inserted fact #1',
76
+ 'Inserted fact #2',
77
+ 'Set \'bar\' to \'"3"\' (Integer)',
78
+ 'Found 1 fact(s) by \'(exists bar)\'',
79
+ 'Deleted 2 fact(s) by \'(not (exists bar))\''
80
+ ].each do |s|
81
+ assert(log.to_s.include?("#{s}\n"), "#{log}\n")
82
+ end
83
+ end
46
84
  end
@@ -77,4 +77,11 @@ class TestQuery < Minitest::Test
77
77
  maps << { 'foo' => [42] }
78
78
  assert_equal(1, Factbase::Query.new(maps, Mutex.new, '(eq foo 42)').each.to_a.size)
79
79
  end
80
+
81
+ def test_returns_int
82
+ maps = []
83
+ maps << { 'foo' => 1 }
84
+ q = Factbase::Query.new(maps, Mutex.new, '(eq foo 1)')
85
+ assert_equal(1, q.each(&:to_s))
86
+ end
80
87
  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.18
4
+ version: 0.0.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko