factbase 0.0.26 → 0.0.27

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: 1be768141fbb97e4753b38b4b8c438a8cf870957744868af05ab325bec94582c
4
- data.tar.gz: bcef0970b79fba877d7f677440e595382a64cab724b00d7df101a0a2bf66d146
3
+ metadata.gz: 29134d600523642a5dd215f28aa9b48ec3337fec96d4dc41ea0500a3a4ee46c5
4
+ data.tar.gz: 2335e295a7b5dfe6f3cb1eb5ace692cfbc32d788c91906a6f609249b26c4da13
5
5
  SHA512:
6
- metadata.gz: 2b705bad3bc10f08a9865217f015069c34e679c6718382f321926d6ae76b96db6e56dd4c76540eeec8fb0511a8e7a40077d7ac8092fbe6588d75cefaf9e5f4d1
7
- data.tar.gz: 3e4c1a563194008beb1697f5a4e146a0a88713fb876c8414893bff69db34b33a10d812135f85cac8ef9655d50d40d7186a3b00aa4c863f5ce4ba4a52429d9c37
6
+ metadata.gz: bf293f3f8ce5b6f1a515a071f771f1a8d84149e0198b0f11073900ed55a6b218bc2d4dcc65ecc439b777fb8163bb13b2f5d24bea9f2600639276183218f2d9f5
7
+ data.tar.gz: ec4c0b3a780ea432b8560485b4a8c467d2b7fc165a1d29fd27b1e03d35f7f481cc11584b3f60100f4a63b308d84023304eb92f8f970cc105cff7e9eba06df0af
@@ -43,6 +43,7 @@ class Factbase::Syntax
43
43
  @ast ||= to_ast(@tokens, 0)
44
44
  term = @ast[0]
45
45
  raise 'No terms found' if term.nil?
46
+ raise 'Not a term' unless term.is_a?(Factbase::Term)
46
47
  term
47
48
  end
48
49
 
@@ -81,7 +82,7 @@ class Factbase::Syntax
81
82
  list = []
82
83
  acc = ''
83
84
  string = false
84
- @query.to_s.chars.each do |c|
85
+ @query.to_s.gsub(/#.*$/, '').chars.each do |c|
85
86
  if ['\'', '"'].include?(c)
86
87
  if string && acc[acc.length - 1] == '\\'
87
88
  acc = acc[0..-2]
data/lib/factbase/term.rb CHANGED
@@ -133,6 +133,15 @@ class Factbase::Term
133
133
  fact[k].size
134
134
  end
135
135
 
136
+ def type(fact)
137
+ assert_args(1)
138
+ o = @operands[0]
139
+ raise "A symbol expected: #{o}" unless o.is_a?(Symbol)
140
+ k = o.to_s
141
+ return 'nil' if fact[k].nil?
142
+ fact[k].class.to_s
143
+ end
144
+
136
145
  def arithmetic(op, fact)
137
146
  assert_args(2)
138
147
  o = @operands[0]
data/lib/factbase.rb CHANGED
@@ -29,7 +29,7 @@ require 'yaml'
29
29
  # License:: MIT
30
30
  class Factbase
31
31
  # Current version of the gem (changed by .rultor.yml on every release)
32
- VERSION = '0.0.26'
32
+ VERSION = '0.0.27'
33
33
 
34
34
  # Constructor.
35
35
  def initialize(facts = [])
@@ -45,6 +45,7 @@ class TestSyntax < Minitest::Test
45
45
  '(foo)',
46
46
  '(foo (bar) (zz 77) )',
47
47
  "(eq foo \n\n 'Hello, world!'\n)\n",
48
+ "# this is a comment\n(eq foo # test\n 42)\n\n# another comment\n",
48
49
  "(or ( a 4) (b 5) () (and () (c 5) \t\t(r 7 8s 8is 'Foo')))"
49
50
  ].each do |q|
50
51
  Factbase::Syntax.new(q).to_term
@@ -90,6 +91,7 @@ class TestSyntax < Minitest::Test
90
91
  [
91
92
  '',
92
93
  '(foo',
94
+ 'some text',
93
95
  '"hello, world!',
94
96
  '(foo 7',
95
97
  "(foo 7 'Dude'",
@@ -111,6 +111,16 @@ class TestTerm < Minitest::Test
111
111
  assert(!t.eval(fact('foo' => 100)))
112
112
  end
113
113
 
114
+ def test_type_matching
115
+ t = Factbase::Term.new(:type, [:foo])
116
+ assert_equal('Integer', t.eval(fact('foo' => 42)))
117
+ assert_equal('Array', t.eval(fact('foo' => [1, 2, 3])))
118
+ assert_equal('String', t.eval(fact('foo' => 'Hello, world!')))
119
+ assert_equal('Float', t.eval(fact('foo' => 3.14)))
120
+ assert_equal('Time', t.eval(fact('foo' => Time.now)))
121
+ assert_equal('nil', t.eval(fact))
122
+ end
123
+
114
124
  def test_or_matching
115
125
  t = Factbase::Term.new(
116
126
  :or,
@@ -139,7 +149,7 @@ class TestTerm < Minitest::Test
139
149
 
140
150
  private
141
151
 
142
- def fact(map)
152
+ def fact(map = {})
143
153
  Factbase::Fact.new(Mutex.new, map)
144
154
  end
145
155
  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.26
4
+ version: 0.0.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko