fbe 0.0.39 → 0.0.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 32f31aad59acf471e8f0075c5ed1d0449a2039fb7d13cd3e0b4c3804b6ef1271
4
- data.tar.gz: 65356a4c23c7a6630a0c0387a511cd0d367c72eb2609cb3ded007f9efacbf6e9
3
+ metadata.gz: 562ad71216aae43bcbc061bc11381c765eacae86bf7f7deb5e5c2a7a68e9d279
4
+ data.tar.gz: '0942fcb9385e3828e1d50dfde029c8368e3b9f2cd5cd5b0630b732f6b5cf32cf'
5
5
  SHA512:
6
- metadata.gz: b50e3e2aaf0a16e640c8dc8068574fdb2dac380b767b00b2ae348c58a7ce0940b4db5dd85719aa263ee19ef5c2d402afef9a41cd31637f9a0e4418415a18ebc5
7
- data.tar.gz: 732aedff1ade21b5db01cf591ad343889e234fd7e646d4012c399c9f053651fb220b5c9a746de63bff129efa37614d99c5ef9842fa688b4a7abda687d7c49d06
6
+ metadata.gz: d683b6cd0ecbf15ec069fee0bf0b92ad1387010bf2ea9cb0fd12d85d3f245d03552941c0ca054cf3d2b3a3730a1bc2419ae16383b442c55bfe674e7e9b8aac42
7
+ data.tar.gz: 4d74ef7c0765883a5f2829cc4b960650736aa2fc7dc0e829bba50de93a34c334196a5f37a324611b8d5b5d8bbeac4e7b4ef725937e4ea7d58ab9215ec50e0722
data/lib/fbe/if_absent.rb CHANGED
@@ -27,7 +27,8 @@ require 'others'
27
27
  require_relative '../fbe'
28
28
  require_relative 'fb'
29
29
 
30
- # Injects a fact if it's absent in the factbase.
30
+ # Injects a fact if it's absent in the factbase, otherwise (it is already
31
+ # there) returns NIL.
31
32
  def Fbe.if_absent(fb: Fbe.fb)
32
33
  attrs = {}
33
34
  f =
@@ -50,7 +51,7 @@ def Fbe.if_absent(fb: Fbe.fb)
50
51
  "(eq #{k} #{vv})"
51
52
  end.join(' ')
52
53
  q = "(and #{q})"
53
- return unless fb.query(q).each.to_a.empty?
54
+ return nil unless fb.query(q).each.to_a.empty?
54
55
  n = fb.insert
55
56
  attrs.each { |k, v| n.send("#{k}=", v) }
56
57
  n
@@ -0,0 +1,59 @@
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 'time'
26
+ require 'others'
27
+ require_relative '../fbe'
28
+ require_relative 'fb'
29
+
30
+ # Injects a fact if it's absent in the factbase, otherwise (it is already
31
+ # there) returns the existing one.
32
+ def Fbe.just_one(fb: Fbe.fb)
33
+ attrs = {}
34
+ f =
35
+ others(map: attrs) do |*args|
36
+ k = args[0]
37
+ if k.end_with?('=')
38
+ @map[k[0..-2].to_sym] = args[1]
39
+ else
40
+ @map[k.to_sym]
41
+ end
42
+ end
43
+ yield f
44
+ q = attrs.except('_id', '_time', '_version').map do |k, v|
45
+ vv = v.to_s
46
+ if v.is_a?(String)
47
+ vv = "'#{vv.gsub('"', '\\\\"').gsub("'", "\\\\'")}'"
48
+ elsif v.is_a?(Time)
49
+ vv = v.utc.iso8601
50
+ end
51
+ "(eq #{k} #{vv})"
52
+ end.join(' ')
53
+ q = "(and #{q})"
54
+ before = fb.query(q).each.to_a.first
55
+ return before unless before.nil?
56
+ n = fb.insert
57
+ attrs.each { |k, v| n.send("#{k}=", v) }
58
+ n
59
+ end
@@ -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.41'
31
31
  end
@@ -0,0 +1,53 @@
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/just_one'
29
+
30
+ # Test.
31
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
32
+ # Copyright:: Copyright (c) 2024 Zerocracy
33
+ # License:: MIT
34
+ class TestJustOne < Minitest::Test
35
+ def test_ignores
36
+ fb = Factbase.new
37
+ fb.insert.foo = 'hello dude'
38
+ n =
39
+ Fbe.just_one(fb:) do |f|
40
+ f.foo = 'hello dude'
41
+ end
42
+ assert(!n.nil?)
43
+ end
44
+
45
+ def test_injects
46
+ fb = Factbase.new
47
+ n =
48
+ Fbe.just_one(fb:) do |f|
49
+ f.foo = 42
50
+ end
51
+ assert_equal(42, n.foo)
52
+ end
53
+ 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.41
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
@@ -230,9 +230,11 @@ files:
230
230
  - lib/fbe/if_absent.rb
231
231
  - lib/fbe/issue.rb
232
232
  - lib/fbe/iterate.rb
233
+ - lib/fbe/just_one.rb
233
234
  - lib/fbe/middleware.rb
234
235
  - lib/fbe/middleware/quota.rb
235
236
  - lib/fbe/octo.rb
237
+ - lib/fbe/overwrite.rb
236
238
  - lib/fbe/pmp.rb
237
239
  - lib/fbe/regularly.rb
238
240
  - lib/fbe/sec.rb
@@ -247,7 +249,9 @@ files:
247
249
  - test/fbe/test_if_absent.rb
248
250
  - test/fbe/test_issue.rb
249
251
  - test/fbe/test_iterate.rb
252
+ - test/fbe/test_just_one.rb
250
253
  - test/fbe/test_octo.rb
254
+ - test/fbe/test_overwrite.rb
251
255
  - test/fbe/test_pmp.rb
252
256
  - test/fbe/test_regularly.rb
253
257
  - test/fbe/test_sec.rb