From fdd494762d50d50a5366db7053795fa5f3db25bd Mon Sep 17 00:00:00 2001 From: Sai Asish Y Date: Sat, 18 Jul 2026 13:57:25 -0700 Subject: [PATCH] #620: raise Fbe::Error when a fact lacks the fid property in kill_if Signed-off-by: Sai Asish Y --- lib/fbe/kill_if.rb | 6 +++++- test/fbe/test_kill_if.rb | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/fbe/kill_if.rb b/lib/fbe/kill_if.rb index 47f21e2..89fc013 100644 --- a/lib/fbe/kill_if.rb +++ b/lib/fbe/kill_if.rb @@ -10,6 +10,8 @@ # # @param [Array] facts List of facts to kill # @param [Factbase] fb The factbase to use (defaults to Fbe.fb) +# @param [String] fid The name of the property that holds the ID (defaults to '_id') +# @raise [Fbe::Error] If a fact does not have the +fid+ property def Fbe.kill_if(facts, fb: Fbe.fb, fid: '_id') ids = [] facts.each do |f| @@ -17,7 +19,9 @@ def Fbe.kill_if(facts, fb: Fbe.fb, fid: '_id') t = yield(f) next unless t end - ids << f[fid].first + id = f[fid]&.first + raise(Fbe::Error, "There is no #{fid} in the fact, cannot use Fbe.kill_if") if id.nil? + ids << id end return 0 if ids.empty? fb.query("(or #{ids.map { |id| "(eq #{fid} #{id})" }.join(' ')})").delete! diff --git a/test/fbe/test_kill_if.rb b/test/fbe/test_kill_if.rb index 7b97818..c9d0a85 100644 --- a/test/fbe/test_kill_if.rb +++ b/test/fbe/test_kill_if.rb @@ -59,4 +59,13 @@ def test_returns_zero_when_block_rejects_all assert_equal(0, Fbe.kill_if(fb.query('(always)').each.to_a, fb:) { false }) assert_equal(2, fb.size) end + + def test_raises_when_fact_lacks_the_id_property + fb = Factbase.new + f = fb.insert + f.foo = 42 + assert_raises(Fbe::Error) do + Fbe.kill_if([f], fb:) + end + end end