Skip to content

Commit

Permalink
#73 copy with except
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Aug 17, 2024
1 parent fe6ab5c commit a81fd34
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/fbe/copy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@
#
# @param [Factbase::Fact] source The source
# @param [Factbase::Fact] target The targer
def Fbe.copy(source, target)
# @param [Array<String>] except List of properties to NOT copy
def Fbe.copy(source, target, except: [])
raise 'The source is nil' if source.nil?
raise 'The target is nil' if target.nil?
raise 'The except is nil' if except.nil?
source.all_properties.each do |k|
next unless target[k].nil?
next if except.include?(k)
source[k].each do |v|
target.send(:"#{k}=", v)
end
Expand Down
11 changes: 11 additions & 0 deletions test/fbe/test_copy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,15 @@ def test_simple_copy
assert_equal(2, f2._id)
assert_equal(42, f2.foo)
end

def test_with_except
fb = Factbase.new
f1 = fb.insert
f1._id = 1
f1.foo = 42
f2 = fb.insert
f2._id = 2
Fbe.copy(f1, f2, except: ['foo'])
assert(f2['foo'].nil?)
end
end

0 comments on commit a81fd34

Please sign in to comment.