From e4937629e5787c92095c0bd540b37d74f04b2c00 Mon Sep 17 00:00:00 2001 From: Titanism0v0 <15958739196@163.com> Date: Wed, 30 Sep 2026 11:06:09 +0800 Subject: [PATCH] #1243: Accept Symbol property names in Fbe.overwrite --- lib/fbe/overwrite.rb | 5 +++-- test/fbe/test_overwrite.rb | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/fbe/overwrite.rb b/lib/fbe/overwrite.rb index b3f7fbb2..ce8faced 100644 --- a/lib/fbe/overwrite.rb +++ b/lib/fbe/overwrite.rb @@ -16,12 +16,12 @@ # an exception will be raised. # # @param [Factbase::Fact] fact The fact to modify (must have _id property) -# @param [String, Hash] property_or_hash The name of the property to set, or a hash of properties +# @param [String, Symbol, Hash] property_or_hash The name of the property to set, or a hash of properties # @param [Any] values The value to set (can be any type, including array) - ignored if first param is Hash # @param [Factbase] fb The factbase to use (defaults to Fbe.fb) # @param [String] fid The property used as the fact identifier (defaults to '_id') # @return [nil] Nothing -# @raise [Fbe::Error] If fact is nil, has no identifier, or property is neither a String nor a Hash +# @raise [Fbe::Error] If fact is nil, has no identifier, or property is neither a String, Symbol, nor Hash # @note This operation preserves all other properties during recreation # @note If property already has the same single value, no changes are made # @example Update a user's status @@ -88,6 +88,7 @@ def Fbe.overwrite(fact, property_or_hash, values = nil, fb: Fbe.fb, fid: '_id') return end property = property_or_hash + property = property.to_s if property.is_a?(Symbol) raise(Fbe::Error, "The property is not a String but #{property.class} (#{property})") unless property.is_a?(String) raise(Fbe::Error, 'The values is nil') if values.nil? values = [values] unless values.is_a?(Array) diff --git a/test/fbe/test_overwrite.rb b/test/fbe/test_overwrite.rb index 61fbc1cd..045b162b 100644 --- a/test/fbe/test_overwrite.rb +++ b/test/fbe/test_overwrite.rb @@ -28,6 +28,43 @@ def test_simple_overwrite assert_equal(2, fb.query('(always)').each.first['many'].size) end + def test_overwrites_missing_symbol_property + fb = Factbase.new + f = fb.insert + f._id = 1 + f.foo = 42 + Fbe.overwrite(f, :bar, 55, fb:) + result = fb.query('(eq _id 1)').each.to_a + assert_equal(1, result.size) + assert_equal([42], result.first['foo']) + assert_equal([55], result.first['bar']) + end + + def test_overwrites_existing_symbol_property + fb = Factbase.new + f = fb.insert + f._id = 1 + f.foo = 42 + f.bar = 'keep' + Fbe.overwrite(f, :foo, 55, fb:) + result = fb.query('(eq _id 1)').each.to_a + assert_equal(1, result.size) + assert_equal([55], result.first['foo']) + assert_equal(['keep'], result.first['bar']) + end + + def test_rejects_unsupported_property_name_types + fb = Factbase.new + f = fb.insert + { + 42 => 'The property is not a String but Integer (42)', + nil => 'The property is not a String but NilClass ()' + }.each do |property, message| + error = assert_raises(Fbe::Error) { Fbe.overwrite(f, property, 55, fb:) } + assert_equal(message, error.message) + end + end + def test_avoids_duplicates fb = Factbase.new f = fb.insert