fbe 0.0.41 → 0.0.42
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fbe/overwrite.rb +8 -4
- data/lib/fbe.rb +1 -1
- data/test/fbe/test_overwrite.rb +17 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '095a45978ce9e57eae04c5d10dc974071b3400dfe5f3fe0ce5da88acf87d6fd1'
|
4
|
+
data.tar.gz: 63e430609dab7392f310b9772f35a427d8ec8799717da1a7fb70c4552d92a44c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 223d399f84eb5a6290b4c0e13d0e04124a6ebd449964ce60b439942b73e6fda71db43ddead097b0a8ca15adea0e37bf9e6d5938a281d602ba3333fd4634bcdda
|
7
|
+
data.tar.gz: 405c244aa9637bda9fb035051248c54edf9c6eba6008f4c371e435a34ab5e80c7d93b4796e55650335714b30062cbf66b211acb7bdf48a8270fbeb5fd0c29ae7
|
data/lib/fbe/overwrite.rb
CHANGED
@@ -31,6 +31,9 @@ require_relative 'fb'
|
|
31
31
|
# exist, it will be removed (the entire fact will be destroyed, new fact
|
32
32
|
# created, and property set).
|
33
33
|
#
|
34
|
+
# It is important that the fact has +_id+ property. If it doesn't, an exception
|
35
|
+
# will be raised.
|
36
|
+
#
|
34
37
|
# @param [Factbase::Fact] fact The fact to modify
|
35
38
|
# @param [String] property The name of the property to set
|
36
39
|
# @param [Any] vqlue The value to set
|
@@ -39,12 +42,13 @@ def Fbe.overwrite(fact, property, value, fb: Fbe.fb)
|
|
39
42
|
fact.all_properties.each do |prop|
|
40
43
|
before[prop.to_s] = fact[prop]
|
41
44
|
end
|
42
|
-
|
45
|
+
id = fact['_id'].first
|
46
|
+
raise 'There is no _id in the fact, cannot use Fbe.overwrite' if id.nil?
|
47
|
+
raise "No facts by _id = #{id}" if fb.query("(eq _id #{id})").delete!.zero?
|
43
48
|
n = fb.insert
|
44
|
-
before[property.to_s] = value
|
49
|
+
before[property.to_s] = [value]
|
45
50
|
before.each do |k, vv|
|
46
|
-
next
|
47
|
-
vv = [vv] unless vv.is_a?(Array)
|
51
|
+
next unless n[k].nil?
|
48
52
|
vv.each do |v|
|
49
53
|
n.send("#{k}=", v)
|
50
54
|
end
|
data/lib/fbe.rb
CHANGED
data/test/fbe/test_overwrite.rb
CHANGED
@@ -35,20 +35,35 @@ class TestOverwrite < Minitest::Test
|
|
35
35
|
def test_simple_overwrite
|
36
36
|
fb = Factbase.new
|
37
37
|
f = fb.insert
|
38
|
+
f._id = 1
|
38
39
|
f.foo = 42
|
39
|
-
f.bar = 'hey'
|
40
|
+
f.bar = 'hey you друг'
|
40
41
|
f.many = 3
|
41
42
|
f.many = 3.14
|
42
43
|
Fbe.overwrite(f, :foo, 55, fb:)
|
43
44
|
assert_equal(55, fb.query('(always)').each.to_a.first['foo'].first)
|
44
|
-
assert_equal('hey', fb.query('(always)').each.to_a.first['bar'].first)
|
45
|
+
assert_equal('hey you друг', fb.query('(always)').each.to_a.first['bar'].first)
|
45
46
|
assert_equal(2, fb.query('(always)').each.to_a.first['many'].size)
|
46
47
|
end
|
47
48
|
|
48
49
|
def test_simple_insert
|
49
50
|
fb = Factbase.new
|
50
51
|
f = fb.insert
|
52
|
+
f._id = 1
|
51
53
|
Fbe.overwrite(f, :foo, 42, fb:)
|
52
54
|
assert_equal(42, fb.query('(always)').each.to_a.first['foo'].first)
|
53
55
|
end
|
56
|
+
|
57
|
+
def test_safe_insert
|
58
|
+
fb = Factbase.new
|
59
|
+
f1 = fb.insert
|
60
|
+
f1.bar = 'a'
|
61
|
+
f2 = fb.insert
|
62
|
+
f2.bar = 'b'
|
63
|
+
f2._id = 2
|
64
|
+
f3 = fb.insert
|
65
|
+
f3._id = 1
|
66
|
+
Fbe.overwrite(f3, :foo, 42, fb:)
|
67
|
+
assert_equal(3, fb.size)
|
68
|
+
end
|
54
69
|
end
|