Skip to content

Commit

Permalink
#56 overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Aug 7, 2024
1 parent 4e478ce commit 83cbc40
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
52 changes: 52 additions & 0 deletions lib/fbe/overwrite.rb
Original file line number Diff line number Diff line change
@@ -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
54 changes: 54 additions & 0 deletions test/fbe/test_overwrite.rb
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
# 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

0 comments on commit 83cbc40

Please sign in to comment.