From 83cbc40328ce7bfb908ea126f4cbfa33c01ede72 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Wed, 7 Aug 2024 13:20:09 +0300 Subject: [PATCH] #56 overwrite --- lib/fbe/overwrite.rb | 52 ++++++++++++++++++++++++++++++++++++ test/fbe/test_overwrite.rb | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 lib/fbe/overwrite.rb create mode 100644 test/fbe/test_overwrite.rb diff --git a/lib/fbe/overwrite.rb b/lib/fbe/overwrite.rb new file mode 100644 index 0000000..b1f6d4c --- /dev/null +++ b/lib/fbe/overwrite.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +# MIT License +# +# Copyright (c) 2024 Zerocracy +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +require_relative '../fbe' +require_relative 'fb' + +# Overwrite a property in the fact. +# +# If the property doesn't exist in the fact, it will be added. If it does +# exist, it will be removed (the entire fact will be destroyed, new fact +# created, and property set). +# +# @param [Factbase::Fact] fact The fact to modify +# @param [String] property The name of the property to set +# @param [Any] vqlue The value to set +def Fbe.overwrite(fact, property, value, fb: Fbe.fb) + before = {} + fact.all_properties.each do |prop| + before[prop.to_s] = fact[prop] + end + fb.query("(and #{before.map { |k, vv| vv.is_a?(Array) ? '' : " (eq #{k} #{vv})" }.join})").delete! + n = fb.insert + before[property.to_s] = value + before.each do |k, vv| + next if k.start_with?('_') + vv = [vv] unless vv.is_a?(Array) + vv.each do |v| + n.send("#{k}=", v) + end + end +end diff --git a/test/fbe/test_overwrite.rb b/test/fbe/test_overwrite.rb new file mode 100644 index 0000000..8296b39 --- /dev/null +++ b/test/fbe/test_overwrite.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +# MIT License +# +# Copyright (c) 2024 Zerocracy +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +require 'minitest/autorun' +require 'factbase' +require_relative '../test__helper' +require_relative '../../lib/fbe/overwrite' + +# Test. +# Author:: Yegor Bugayenko (yegor256@gmail.com) +# Copyright:: Copyright (c) 2024 Zerocracy +# License:: MIT +class TestOverwrite < Minitest::Test + def test_simple_overwrite + fb = Factbase.new + f = fb.insert + f.foo = 42 + f.bar = 'hey' + f.many = 3 + f.many = 3.14 + Fbe.overwrite(f, :foo, 55, fb:) + assert_equal(55, fb.query('(always)').each.to_a.first['foo'].first) + assert_equal('hey', fb.query('(always)').each.to_a.first['bar'].first) + assert_equal(2, fb.query('(always)').each.to_a.first['many'].size) + end + + def test_simple_insert + fb = Factbase.new + f = fb.insert + Fbe.overwrite(f, :foo, 42, fb:) + assert_equal(42, fb.query('(always)').each.to_a.first['foo'].first) + end +end