Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/fbe/overwrite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,24 @@ def Fbe.overwrite(fact, property_or_hash, values = nil, fb: Fbe.fb, fid: '_id')
before[k.to_s] = fact[k]
end
modified = false
missing = true
property_or_hash.each do |k, vv|
raise(Fbe::Error, "The value for #{k} is nil") if vv.nil?
vv = [vv] unless vv.is_a?(Array)
missing = false unless before[k.to_s].nil?
next if before[k.to_s] == vv
before[k.to_s] = vv
modified = true
end
return fact unless modified
if missing
before.slice(*property_or_hash.keys.map(&:to_s)).each do |k, vv|
vv.each do |v|
fact.public_send(:"#{k}=", v)
end
end
return
end
id = fact[fid]&.first
raise(Fbe::Error, "There is no #{fid} in the fact, cannot use Fbe.overwrite") if id.nil?
raise(Fbe::Error, "No facts by #{fid} = #{id}") if fb.query("(eq #{fid} #{id})").delete!.zero?
Expand Down
16 changes: 16 additions & 0 deletions test/fbe/test_overwrite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ def test_without_previous_property
assert_equal(before, fbx.query('(eq bar 44)').each.first._id)
end

def test_hash_without_previous_property
c = 0
fb =
Factbase::Pre.new(Factbase.new) do |f, _fbt|
f.pos = c
c += 1
end
f = fb.insert
f._id = 1
f.foo = 42
before = f.pos
Fbe.overwrite(f, { bar: 44 }, fb:)
found = fb.query('(eq bar 44)').each.first
assert_equal(before, found.pos)
end

def test_safe_insert
fb = Factbase.new
first = fb.insert
Expand Down
Loading