factbase 0.0.40 → 0.0.41

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: 129d17fba6d19bc5131f853d6b90c2e963187e84626270be4f7766e926d4c2c9
4
- data.tar.gz: 4196611d541b81bdc1a73bf31f4b1fe15077ad702d8d1ec558f0f61d5e530396
3
+ metadata.gz: 0ea2fe8670484d4eb3eac59caa947b547863a2106fa6ae771b8e9447b30573dc
4
+ data.tar.gz: b45d41378d4b61f0b3d8d01a3454819a4eca7724bd81adda7fe083c9594250ef
5
5
  SHA512:
6
- metadata.gz: 1d87ddb11c2252043c963c6e086a617f216bc15a79441cfa121a0e3de3157f31705cfd1bfea7b6b89996488c76805ee27152f4be00627c7d3f3e1d506636c303
7
- data.tar.gz: 5e913f0d675f19fc03cc83c80301af5fd390de8cdaf730a0ea2612768ae0056286be7f5f42686616ee0b1fc69cf22adc0009e9df5315251971e9c948da5ee739
6
+ metadata.gz: 40d6ca4cce2c4d64fe5e31b609c2fd15d05041358b8ae3be266c991f943ce242217378a8e83f96d8be83c50068fb65e04dff8c8c24708eeea8f79490e283c817
7
+ data.tar.gz: d09b19ac55775c511c0619b0a23be1260496f33d623927153e0b5b373b6919570a9f47104c0e2b1125299799f561e2b6fb39b2cb6ea5486b48ab76a05e15bfd1
data/Gemfile CHANGED
@@ -26,7 +26,7 @@ gemspec
26
26
  gem 'minitest', '5.23.1', require: false
27
27
  gem 'rake', '13.2.1', require: false
28
28
  gem 'rspec-rails', '6.1.2', require: false
29
- gem 'rubocop', '1.64.0', require: false
29
+ gem 'rubocop', '1.64.1', require: false
30
30
  gem 'rubocop-performance', '1.21.0', require: false
31
31
  gem 'rubocop-rspec', '2.29.2', require: false
32
32
  gem 'simplecov', '0.22.0', require: false
data/Gemfile.lock CHANGED
@@ -70,7 +70,7 @@ GEM
70
70
  nokogiri (1.16.5-x86_64-linux)
71
71
  racc (~> 1.4)
72
72
  parallel (1.24.0)
73
- parser (3.3.1.0)
73
+ parser (3.3.2.0)
74
74
  ast (~> 2.4.1)
75
75
  racc
76
76
  psych (5.1.2)
@@ -125,7 +125,7 @@ GEM
125
125
  rspec-mocks (~> 3.13)
126
126
  rspec-support (~> 3.13)
127
127
  rspec-support (3.13.1)
128
- rubocop (1.64.0)
128
+ rubocop (1.64.1)
129
129
  json (~> 2.3)
130
130
  language_server-protocol (>= 3.17.0)
131
131
  parallel (~> 1.10)
@@ -184,7 +184,7 @@ DEPENDENCIES
184
184
  minitest (= 5.23.1)
185
185
  rake (= 13.2.1)
186
186
  rspec-rails (= 6.1.2)
187
- rubocop (= 1.64.0)
187
+ rubocop (= 1.64.1)
188
188
  rubocop-performance (= 1.21.0)
189
189
  rubocop-rspec (= 2.29.2)
190
190
  simplecov (= 0.22.0)
@@ -26,12 +26,17 @@ require_relative 'fact'
26
26
 
27
27
  # Query.
28
28
  #
29
- # This is an internal class, it is not supposed to be instantiated directly.
29
+ # This is an internal class, it is not supposed to be instantiated directly. It
30
+ # is created by the +query()+ method of the +Factbase+ class.
30
31
  #
31
32
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
32
33
  # Copyright:: Copyright (c) 2024 Yegor Bugayenko
33
34
  # License:: MIT
34
35
  class Factbase::Query
36
+ # Constructor.
37
+ # @param [Array<Fact>] maps Array of facts to start with
38
+ # @param [Mutex] mutex Mutex to sync all modifications to the +maps+
39
+ # @param [String] query The query as a string
35
40
  def initialize(maps, mutex, query)
36
41
  @maps = maps
37
42
  @mutex = mutex
@@ -48,7 +53,9 @@ class Factbase::Query
48
53
  @maps.each do |m|
49
54
  f = Factbase::Fact.new(@mutex, m)
50
55
  r = term.evaluate(f, @maps)
51
- raise 'Unexpected evaluation result, must be boolean' unless r.is_a?(TrueClass) || r.is_a?(FalseClass)
56
+ unless r.is_a?(TrueClass) || r.is_a?(FalseClass)
57
+ raise "Unexpected evaluation result (#{r.class}), must be Boolean at #{@query}"
58
+ end
52
59
  next unless r
53
60
  yield f
54
61
  yielded += 1
data/lib/factbase/term.rb CHANGED
@@ -66,7 +66,9 @@ class Factbase::Term
66
66
  def evaluate(fact, maps)
67
67
  send(@op, fact, maps)
68
68
  rescue NoMethodError => e
69
- raise "Term '#{@op}' is not defined: #{e.message}"
69
+ raise "Term '#{@op}' is not defined at #{self}: #{e.message}"
70
+ rescue StandardError => e
71
+ raise "#{e.message} at #{self}"
70
72
  end
71
73
 
72
74
  # Simplify it if possible.
@@ -111,19 +113,19 @@ class Factbase::Term
111
113
 
112
114
  def not(fact, maps)
113
115
  assert_args(1)
114
- !only_bool(the_values(0, fact, maps))
116
+ !only_bool(the_values(0, fact, maps), 0)
115
117
  end
116
118
 
117
119
  def or(fact, maps)
118
120
  (0..@operands.size - 1).each do |i|
119
- return true if only_bool(the_values(i, fact, maps))
121
+ return true if only_bool(the_values(i, fact, maps), 0)
120
122
  end
121
123
  false
122
124
  end
123
125
 
124
126
  def and(fact, maps)
125
127
  (0..@operands.size - 1).each do |i|
126
- return false unless only_bool(the_values(i, fact, maps))
128
+ return false unless only_bool(the_values(i, fact, maps), 0)
127
129
  end
128
130
  true
129
131
  end
@@ -339,6 +341,7 @@ class Factbase::Term
339
341
  end
340
342
 
341
343
  def agg(_fact, maps)
344
+ assert_args(2)
342
345
  selector = @operands[0]
343
346
  raise "A term expected, but #{selector} provided" unless selector.is_a?(Factbase::Term)
344
347
  term = @operands[1]
@@ -374,10 +377,12 @@ class Factbase::Term
374
377
  v
375
378
  end
376
379
 
377
- def only_bool(val)
380
+ def only_bool(val, pos)
378
381
  val = val[0] if val.is_a?(Array)
379
382
  return false if val.nil?
380
- raise "Boolean expected, while #{val.class} received" unless val.is_a?(TrueClass) || val.is_a?(FalseClass)
383
+ unless val.is_a?(TrueClass) || val.is_a?(FalseClass)
384
+ raise "Boolean expected, while #{val.class} received from #{@operands[pos]}"
385
+ end
381
386
  val
382
387
  end
383
388
 
data/lib/factbase.rb CHANGED
@@ -79,12 +79,13 @@ 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.40'
82
+ VERSION = '0.0.41'
83
83
 
84
84
  # An exception that may be thrown in a transaction, to roll it back.
85
85
  class Rollback < StandardError; end
86
86
 
87
87
  # Constructor.
88
+ # @param [Array<Hash>] facts Array of facts to start with
88
89
  def initialize(facts = [])
89
90
  @maps = facts
90
91
  @mutex = Mutex.new
@@ -183,6 +183,22 @@ class TestTerm < Minitest::Test
183
183
  assert_equal([42], t.evaluate(fact('foo' => 4), []))
184
184
  end
185
185
 
186
+ def test_report_missing_term
187
+ t = Factbase::Term.new(:something, [])
188
+ msg = assert_raises do
189
+ t.evaluate(fact, [])
190
+ end.message
191
+ assert(msg.include?('not defined at (something)'), msg)
192
+ end
193
+
194
+ def test_report_other_error
195
+ t = Factbase::Term.new(:at, [])
196
+ msg = assert_raises do
197
+ t.evaluate(fact, [])
198
+ end.message
199
+ assert(msg.include?('at (at)'), msg)
200
+ end
201
+
186
202
  private
187
203
 
188
204
  def fact(map = {})
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.40
4
+ version: 0.0.41
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-05-30 00:00:00.000000000 Z
11
+ date: 2024-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json