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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 562ad71216aae43bcbc061bc11381c765eacae86bf7f7deb5e5c2a7a68e9d279
4
- data.tar.gz: '0942fcb9385e3828e1d50dfde029c8368e3b9f2cd5cd5b0630b732f6b5cf32cf'
3
+ metadata.gz: '095a45978ce9e57eae04c5d10dc974071b3400dfe5f3fe0ce5da88acf87d6fd1'
4
+ data.tar.gz: 63e430609dab7392f310b9772f35a427d8ec8799717da1a7fb70c4552d92a44c
5
5
  SHA512:
6
- metadata.gz: d683b6cd0ecbf15ec069fee0bf0b92ad1387010bf2ea9cb0fd12d85d3f245d03552941c0ca054cf3d2b3a3730a1bc2419ae16383b442c55bfe674e7e9b8aac42
7
- data.tar.gz: 4d74ef7c0765883a5f2829cc4b960650736aa2fc7dc0e829bba50de93a34c334196a5f37a324611b8d5b5d8bbeac4e7b4ef725937e4ea7d58ab9215ec50e0722
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
- fb.query("(and #{before.map { |k, vv| vv.is_a?(Array) ? '' : " (eq #{k} #{vv})" }.join})").delete!
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 if k.start_with?('_')
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
@@ -27,5 +27,5 @@
27
27
  # License:: MIT
28
28
  module Fbe
29
29
  # Current version of the gem (changed by .rultor.yml on every release)
30
- VERSION = '0.0.41'
30
+ VERSION = '0.0.42'
31
31
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fbe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.41
4
+ version: 0.0.42
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko