factbase 0.0.56 → 0.0.58

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: d26f7abaafccccb51ef4077d66fed7a4a8ef846df2a7d46191d7113b27a2a689
4
- data.tar.gz: 8d0a64e42a4a695edc7ef5a7958ceb69aa6a965e53778782bd865c49a8752a4f
3
+ metadata.gz: 7e13671937b0df97f981c14d5dd95f18174a59694b7cae5c425de45aea36bc0e
4
+ data.tar.gz: ce5a4cc54e772b37cf57e379bbceba7149905e61c1e15ad87cec6a1434b394ed
5
5
  SHA512:
6
- metadata.gz: 65c7a43401b8933e212dea979319cc75a0e42349123f0cd98871ee4d1493002b2f280a229820101ef481168c64a2e97333a549036dd3c88dfa3d05b4230d9085
7
- data.tar.gz: 9106f2c57b7840e95de6a6dba53859f0d0c4e0bea5770b22d558c35894e847145fe0b9d5640d017441baa91be48bcbc4a09cf38ae8390ab9d0efe379b5add37b
6
+ metadata.gz: 5115395546357ab98c6035605667e664228a1d84a802475721eb05a35d836e5d8041f29c66ffa96fe31b02fd5f006776896e8765bb9d9535705602ed0337e0a8
7
+ data.tar.gz: 95d4751859aec0fb9ad1adbcaaee2b30e178f67cfd7ba95d596ee4e0337ac02fdc88697f07db330f24c7f82c4ad33b4bd4aa0a0d6e1bb0f19e28f0e2b5d0aa3e
data/Gemfile CHANGED
@@ -23,7 +23,7 @@
23
23
  source 'https://rubygems.org'
24
24
  gemspec
25
25
 
26
- gem 'minitest', '5.24.0', require: false
26
+ gem 'minitest', '5.24.1', require: false
27
27
  gem 'rake', '13.2.1', require: false
28
28
  gem 'rspec-rails', '6.1.3', require: false
29
29
  gem 'rubocop', '1.64.1', require: false
data/Gemfile.lock CHANGED
@@ -64,8 +64,8 @@ GEM
64
64
  loofah (2.22.0)
65
65
  crass (~> 1.0.2)
66
66
  nokogiri (>= 1.12.0)
67
- loog (0.5.1)
68
- minitest (5.24.0)
67
+ loog (0.5.2)
68
+ minitest (5.24.1)
69
69
  mutex_m (0.2.0)
70
70
  nokogiri (1.16.6-arm64-darwin)
71
71
  racc (~> 1.4)
@@ -113,7 +113,7 @@ GEM
113
113
  regexp_parser (2.9.2)
114
114
  reline (0.5.9)
115
115
  io-console (~> 0.5)
116
- rexml (3.3.0)
116
+ rexml (3.3.1)
117
117
  strscan
118
118
  rspec-core (3.13.0)
119
119
  rspec-support (~> 3.13.0)
@@ -180,7 +180,7 @@ PLATFORMS
180
180
 
181
181
  DEPENDENCIES
182
182
  factbase!
183
- minitest (= 5.24.0)
183
+ minitest (= 5.24.1)
184
184
  rake (= 13.2.1)
185
185
  rspec-rails (= 6.1.3)
186
186
  rubocop (= 1.64.1)
data/README.md CHANGED
@@ -97,6 +97,12 @@ Also, some simple arithmetic:
97
97
  * `(times v1 v2)` is a multiplication of `∏v1` and `∏v2`
98
98
  * `(div v1 v2)` is a division of `∏v1` by `∏v2`
99
99
 
100
+ Types may be converted:
101
+
102
+ * `(to_int v)` is an integer of `v`
103
+ * `(to_str v)` is a string of `v`
104
+ * `(to_float v)` is a float of `v`
105
+
100
106
  One term is for meta-programming:
101
107
 
102
108
  * `(defn f "self.to_s")` defines a new term using Ruby syntax and returns `true`
@@ -43,6 +43,10 @@ class Factbase::Accum
43
43
  "#{@fact} + #{@props}"
44
44
  end
45
45
 
46
+ def all_properties
47
+ @fact.all_properties
48
+ end
49
+
46
50
  others do |*args|
47
51
  k = args[0].to_s
48
52
  if k.end_with?('=')
data/lib/factbase/fact.rb CHANGED
@@ -57,13 +57,19 @@ class Factbase::Fact
57
57
  "[ #{@map.map { |k, v| "#{k}: #{v}" }.join(', ')} ]"
58
58
  end
59
59
 
60
+ # Get a list of all props.
61
+ # @return [Array<String>] List of all props in the fact
62
+ def all_properties
63
+ @map.keys
64
+ end
65
+
60
66
  # When a method is missing, this method is called.
61
67
  others do |*args|
62
68
  k = args[0].to_s
63
69
  if k.end_with?('=')
64
70
  kk = k[0..-2]
65
71
  raise "Invalid prop name '#{kk}'" unless kk.match?(/^[a-z_][_a-zA-Z0-9]*$/)
66
- raise "Prohibited prop name '#{kk}'" if kk == 'to_s'
72
+ raise "Prohibited prop name '#{kk}'" if methods.include?(kk.to_sym)
67
73
  v = args[1]
68
74
  raise "Prop value can't be nil" if v.nil?
69
75
  raise "Prop value can't be empty" if v == ''
data/lib/factbase/inv.rb CHANGED
@@ -66,6 +66,10 @@ class Factbase::Inv
66
66
  @fact.to_s
67
67
  end
68
68
 
69
+ def all_properties
70
+ @fact.all_properties
71
+ end
72
+
69
73
  others do |*args|
70
74
  k = args[0].to_s
71
75
  @block.call(k[0..-2], args[1]) if k.end_with?('=')
@@ -86,6 +86,14 @@ class Factbase::Looged
86
86
  @loog = loog
87
87
  end
88
88
 
89
+ def to_s
90
+ @fact.to_s
91
+ end
92
+
93
+ def all_properties
94
+ @fact.all_properties
95
+ end
96
+
89
97
  others do |*args|
90
98
  r = @fact.method_missing(*args)
91
99
  k = args[0].to_s
@@ -89,6 +89,14 @@ class Factbase::Rules
89
89
  @check = check
90
90
  end
91
91
 
92
+ def to_s
93
+ @fact.to_s
94
+ end
95
+
96
+ def all_properties
97
+ @fact.all_properties
98
+ end
99
+
92
100
  others do |*args|
93
101
  r = @fact.method_missing(*args)
94
102
  k = args[0].to_s
data/lib/factbase/tee.rb CHANGED
@@ -41,6 +41,10 @@ class Factbase::Tee
41
41
  @fact.to_s
42
42
  end
43
43
 
44
+ def all_properties
45
+ @fact.all_properties + (@upper.is_a?(Hash) ? @upper.keys : @upper.all_properties)
46
+ end
47
+
44
48
  others do |*args|
45
49
  if args[0].to_s == '[]' && args[1].to_s.start_with?('$')
46
50
  n = args[1].to_s
@@ -51,6 +51,27 @@ module Factbase::Term::Math
51
51
  vv.any? { |v| (v.is_a?(Integer) || v.is_a?(Float)) && v.zero? }
52
52
  end
53
53
 
54
+ def to_str(fact, maps)
55
+ assert_args(1)
56
+ vv = the_values(0, fact, maps)
57
+ return nil if vv.nil?
58
+ vv[0].to_s
59
+ end
60
+
61
+ def to_int(fact, maps)
62
+ assert_args(1)
63
+ vv = the_values(0, fact, maps)
64
+ return nil if vv.nil?
65
+ vv[0].to_i
66
+ end
67
+
68
+ def to_float(fact, maps)
69
+ assert_args(1)
70
+ vv = the_values(0, fact, maps)
71
+ return nil if vv.nil?
72
+ vv[0].to_f
73
+ end
74
+
54
75
  def eq(fact, maps)
55
76
  cmp(:==, fact, maps)
56
77
  end
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.0.56'
84
+ VERSION = '0.0.58'
85
85
 
86
86
  # An exception that may be thrown in a transaction, to roll it back.
87
87
  class Rollback < StandardError; end
@@ -32,13 +32,14 @@ require_relative '../../../lib/factbase/accum'
32
32
  class TestAliases < Minitest::Test
33
33
  def test_aliases
34
34
  maps = [
35
- { 'x' => [1], 'y' => [0] },
36
- { 'x' => [2], 'y' => [42] }
35
+ { 'x' => [1], 'y' => [0], 't1' => [Time.now], 't2' => [Time.now] },
36
+ { 'x' => [2], 'y' => [42], 't' => [Time.now] }
37
37
  ]
38
38
  {
39
39
  '(as foo (plus x 1))' => '(exists foo)',
40
40
  '(as foo (plus x y))' => '(gt foo 0)',
41
- '(as foo (plus bar 1))' => '(absent foo)'
41
+ '(as foo (plus bar 1))' => '(absent foo)',
42
+ '(as foo (minus t1 t2))' => '(when (exists foo) (eq "Float" (type foo)))'
42
43
  }.each do |q, r|
43
44
  t = Factbase::Syntax.new(q).to_term
44
45
  maps.each do |m|
@@ -91,6 +91,21 @@ class TestMath < Minitest::Test
91
91
  assert(!t.evaluate(fact('bar' => [100]), []))
92
92
  end
93
93
 
94
+ def test_to_str
95
+ t = Factbase::Term.new(:to_str, [Time.now])
96
+ assert_equal('String', t.evaluate(fact, []).class.to_s)
97
+ end
98
+
99
+ def test_to_int
100
+ t = Factbase::Term.new(:to_int, [[42, 'hello']])
101
+ assert_equal('Integer', t.evaluate(fact, []).class.to_s)
102
+ end
103
+
104
+ def test_to_float
105
+ t = Factbase::Term.new(:to_float, [[3.14, 'hello']])
106
+ assert_equal('Float', t.evaluate(fact, []).class.to_s)
107
+ end
108
+
94
109
  private
95
110
 
96
111
  def fact(map = {})
@@ -110,4 +110,16 @@ class TestFact < Minitest::Test
110
110
  assert_equal(t.utc, f.foo)
111
111
  assert_equal(t.utc.to_s, f.foo.to_s)
112
112
  end
113
+
114
+ def test_some_names_are_prohibited
115
+ f = Factbase::Fact.new(Mutex.new, {})
116
+ assert_raises { f.to_s = 42 }
117
+ assert_raises { f.class = 42 }
118
+ end
119
+
120
+ def test_get_all_properties
121
+ f = Factbase::Fact.new(Mutex.new, {})
122
+ f.foo = 42
123
+ assert(f.all_properties.include?('foo'))
124
+ end
113
125
  end
@@ -173,4 +173,11 @@ class TestQuery < Minitest::Test
173
173
  maps << { 'foo' => [42] }
174
174
  assert(Factbase::Query.new(maps, Mutex.new, '(as bar (plus xxx 3))').each.to_a[0]['bar'].nil?)
175
175
  end
176
+
177
+ def test_get_all_properties
178
+ maps = []
179
+ maps << { 'foo' => [42] }
180
+ f = Factbase::Query.new(maps, Mutex.new, '(always)').each.to_a[0]
181
+ assert(f.all_properties.include?('foo'))
182
+ end
176
183
  end
@@ -31,16 +31,25 @@ require_relative '../../lib/factbase/fact'
31
31
  # License:: MIT
32
32
  class TestTee < Minitest::Test
33
33
  def test_two_facts
34
- map = {}
35
- prim = Factbase::Fact.new(Mutex.new, map)
34
+ prim = Factbase::Fact.new(Mutex.new, {})
36
35
  prim.foo = 42
37
- upper = Factbase::Fact.new(Mutex.new, map)
36
+ upper = Factbase::Fact.new(Mutex.new, {})
38
37
  upper.bar = 13
39
38
  t = Factbase::Tee.new(prim, upper)
40
39
  assert_equal(42, t.foo)
41
40
  assert_equal([13], t['$bar'])
42
41
  end
43
42
 
43
+ def test_all_properties
44
+ prim = Factbase::Fact.new(Mutex.new, {})
45
+ prim.foo = 42
46
+ upper = Factbase::Fact.new(Mutex.new, {})
47
+ upper.bar = 13
48
+ t = Factbase::Tee.new(prim, upper)
49
+ assert(t.all_properties.include?('foo'))
50
+ assert(t.all_properties.include?('bar'))
51
+ end
52
+
44
53
  def test_recursively
45
54
  map = {}
46
55
  prim = Factbase::Fact.new(Mutex.new, map)
@@ -65,12 +65,10 @@ class TestToXML < Minitest::Test
65
65
  f = fb.insert
66
66
  f.type = 1
67
67
  f.f = 2
68
- f.class = 3
69
68
  to = Factbase::ToXML.new(fb)
70
69
  xml = Nokogiri::XML.parse(to.xml)
71
70
  assert(!xml.xpath('/fb/f/type').empty?)
72
71
  assert(!xml.xpath('/fb/f/f').empty?)
73
- assert(!xml.xpath('/fb/f/class').empty?)
74
72
  end
75
73
 
76
74
  def test_show_types_as_attributes
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.56
4
+ version: 0.0.58
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-27 00:00:00.000000000 Z
11
+ date: 2024-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backtrace