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
6 changes: 5 additions & 1 deletion lib/fbe/kill_if.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
#
# @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|
if block_given?
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!
Expand Down
9 changes: 9 additions & 0 deletions test/fbe/test_kill_if.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading