fbe 0.0.39 → 0.0.40

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 32f31aad59acf471e8f0075c5ed1d0449a2039fb7d13cd3e0b4c3804b6ef1271
4
- data.tar.gz: 65356a4c23c7a6630a0c0387a511cd0d367c72eb2609cb3ded007f9efacbf6e9
3
+ metadata.gz: c5738119a9d73b0ec3e019726bb057059059d0bd5fe08815b0290477902aa4c9
4
+ data.tar.gz: f2a54bd36e5672e060d9066fe3aa67077e52dc705da9a2cc3f01002dd9ef1bd4
5
5
  SHA512:
6
- metadata.gz: b50e3e2aaf0a16e640c8dc8068574fdb2dac380b767b00b2ae348c58a7ce0940b4db5dd85719aa263ee19ef5c2d402afef9a41cd31637f9a0e4418415a18ebc5
7
- data.tar.gz: 732aedff1ade21b5db01cf591ad343889e234fd7e646d4012c399c9f053651fb220b5c9a746de63bff129efa37614d99c5ef9842fa688b4a7abda687d7c49d06
6
+ metadata.gz: c394e6be7d4a376681ba87e308b28b21187454f68725c2fd3774dda934c0fa9a7caadced6e982da9c8f79c6b276abdf4fc2d26e64e325e801e8a0487ddb7dcc2
7
+ data.tar.gz: 7815c260f71e1b77fee5a61faaafc41bd5a98843ef250209fb267fc0dc3cd81b466a257ac7452df664bf5e859ab1ae78cc7e23d3e9bc6ad58d24aa33c57750be
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ # MIT License
4
+ #
5
+ # Copyright (c) 2024 Zerocracy
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+ require_relative '../fbe'
26
+ require_relative 'fb'
27
+
28
+ # Overwrite a property in the fact.
29
+ #
30
+ # If the property doesn't exist in the fact, it will be added. If it does
31
+ # exist, it will be removed (the entire fact will be destroyed, new fact
32
+ # created, and property set).
33
+ #
34
+ # @param [Factbase::Fact] fact The fact to modify
35
+ # @param [String] property The name of the property to set
36
+ # @param [Any] vqlue The value to set
37
+ def Fbe.overwrite(fact, property, value, fb: Fbe.fb)
38
+ before = {}
39
+ fact.all_properties.each do |prop|
40
+ before[prop.to_s] = fact[prop]
41
+ end
42
+ fb.query("(and #{before.map { |k, vv| vv.is_a?(Array) ? '' : " (eq #{k} #{vv})" }.join})").delete!
43
+ n = fb.insert
44
+ before[property.to_s] = value
45
+ before.each do |k, vv|
46
+ next if k.start_with?('_')
47
+ vv = [vv] unless vv.is_a?(Array)
48
+ vv.each do |v|
49
+ n.send("#{k}=", v)
50
+ end
51
+ end
52
+ end
data/lib/fbe.rb CHANGED
@@ -27,5 +27,5 @@
27
27
  # License:: MIT
28
28
  module Fbe
29
29
  # Current version of the gem (changed by .rultor.yml on every release)
30
- VERSION = '0.0.39'
30
+ VERSION = '0.0.40'
31
31
  end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ # MIT License
4
+ #
5
+ # Copyright (c) 2024 Zerocracy
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+ require 'minitest/autorun'
26
+ require 'factbase'
27
+ require_relative '../test__helper'
28
+ require_relative '../../lib/fbe/overwrite'
29
+
30
+ # Test.
31
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
32
+ # Copyright:: Copyright (c) 2024 Zerocracy
33
+ # License:: MIT
34
+ class TestOverwrite < Minitest::Test
35
+ def test_simple_overwrite
36
+ fb = Factbase.new
37
+ f = fb.insert
38
+ f.foo = 42
39
+ f.bar = 'hey'
40
+ f.many = 3
41
+ f.many = 3.14
42
+ Fbe.overwrite(f, :foo, 55, fb:)
43
+ assert_equal(55, fb.query('(always)').each.to_a.first['foo'].first)
44
+ assert_equal('hey', fb.query('(always)').each.to_a.first['bar'].first)
45
+ assert_equal(2, fb.query('(always)').each.to_a.first['many'].size)
46
+ end
47
+
48
+ def test_simple_insert
49
+ fb = Factbase.new
50
+ f = fb.insert
51
+ Fbe.overwrite(f, :foo, 42, fb:)
52
+ assert_equal(42, fb.query('(always)').each.to_a.first['foo'].first)
53
+ end
54
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fbe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.39
4
+ version: 0.0.40
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
@@ -233,6 +233,7 @@ files:
233
233
  - lib/fbe/middleware.rb
234
234
  - lib/fbe/middleware/quota.rb
235
235
  - lib/fbe/octo.rb
236
+ - lib/fbe/overwrite.rb
236
237
  - lib/fbe/pmp.rb
237
238
  - lib/fbe/regularly.rb
238
239
  - lib/fbe/sec.rb
@@ -248,6 +249,7 @@ files:
248
249
  - test/fbe/test_issue.rb
249
250
  - test/fbe/test_iterate.rb
250
251
  - test/fbe/test_octo.rb
252
+ - test/fbe/test_overwrite.rb
251
253
  - test/fbe/test_pmp.rb
252
254
  - test/fbe/test_regularly.rb
253
255
  - test/fbe/test_sec.rb