fbe 0.0.40 → 0.0.42
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 +4 -4
- data/lib/fbe/if_absent.rb +3 -2
- data/lib/fbe/just_one.rb +59 -0
- data/lib/fbe/overwrite.rb +8 -4
- data/lib/fbe.rb +1 -1
- data/test/fbe/test_just_one.rb +53 -0
- data/test/fbe/test_overwrite.rb +17 -2
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '095a45978ce9e57eae04c5d10dc974071b3400dfe5f3fe0ce5da88acf87d6fd1'
|
4
|
+
data.tar.gz: 63e430609dab7392f310b9772f35a427d8ec8799717da1a7fb70c4552d92a44c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 223d399f84eb5a6290b4c0e13d0e04124a6ebd449964ce60b439942b73e6fda71db43ddead097b0a8ca15adea0e37bf9e6d5938a281d602ba3333fd4634bcdda
|
7
|
+
data.tar.gz: 405c244aa9637bda9fb035051248c54edf9c6eba6008f4c371e435a34ab5e80c7d93b4796e55650335714b30062cbf66b211acb7bdf48a8270fbeb5fd0c29ae7
|
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
|
data/lib/fbe/just_one.rb
ADDED
@@ -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
|
data/lib/fbe/overwrite.rb
CHANGED
@@ -31,6 +31,9 @@ require_relative 'fb'
|
|
31
31
|
# exist, it will be removed (the entire fact will be destroyed, new fact
|
32
32
|
# created, and property set).
|
33
33
|
#
|
34
|
+
# It is important that the fact has +_id+ property. If it doesn't, an exception
|
35
|
+
# will be raised.
|
36
|
+
#
|
34
37
|
# @param [Factbase::Fact] fact The fact to modify
|
35
38
|
# @param [String] property The name of the property to set
|
36
39
|
# @param [Any] vqlue The value to set
|
@@ -39,12 +42,13 @@ def Fbe.overwrite(fact, property, value, fb: Fbe.fb)
|
|
39
42
|
fact.all_properties.each do |prop|
|
40
43
|
before[prop.to_s] = fact[prop]
|
41
44
|
end
|
42
|
-
|
45
|
+
id = fact['_id'].first
|
46
|
+
raise 'There is no _id in the fact, cannot use Fbe.overwrite' if id.nil?
|
47
|
+
raise "No facts by _id = #{id}" if fb.query("(eq _id #{id})").delete!.zero?
|
43
48
|
n = fb.insert
|
44
|
-
before[property.to_s] = value
|
49
|
+
before[property.to_s] = [value]
|
45
50
|
before.each do |k, vv|
|
46
|
-
next
|
47
|
-
vv = [vv] unless vv.is_a?(Array)
|
51
|
+
next unless n[k].nil?
|
48
52
|
vv.each do |v|
|
49
53
|
n.send("#{k}=", v)
|
50
54
|
end
|
data/lib/fbe.rb
CHANGED
@@ -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
|
data/test/fbe/test_overwrite.rb
CHANGED
@@ -35,20 +35,35 @@ class TestOverwrite < Minitest::Test
|
|
35
35
|
def test_simple_overwrite
|
36
36
|
fb = Factbase.new
|
37
37
|
f = fb.insert
|
38
|
+
f._id = 1
|
38
39
|
f.foo = 42
|
39
|
-
f.bar = 'hey'
|
40
|
+
f.bar = 'hey you друг'
|
40
41
|
f.many = 3
|
41
42
|
f.many = 3.14
|
42
43
|
Fbe.overwrite(f, :foo, 55, fb:)
|
43
44
|
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('hey you друг', fb.query('(always)').each.to_a.first['bar'].first)
|
45
46
|
assert_equal(2, fb.query('(always)').each.to_a.first['many'].size)
|
46
47
|
end
|
47
48
|
|
48
49
|
def test_simple_insert
|
49
50
|
fb = Factbase.new
|
50
51
|
f = fb.insert
|
52
|
+
f._id = 1
|
51
53
|
Fbe.overwrite(f, :foo, 42, fb:)
|
52
54
|
assert_equal(42, fb.query('(always)').each.to_a.first['foo'].first)
|
53
55
|
end
|
56
|
+
|
57
|
+
def test_safe_insert
|
58
|
+
fb = Factbase.new
|
59
|
+
f1 = fb.insert
|
60
|
+
f1.bar = 'a'
|
61
|
+
f2 = fb.insert
|
62
|
+
f2.bar = 'b'
|
63
|
+
f2._id = 2
|
64
|
+
f3 = fb.insert
|
65
|
+
f3._id = 1
|
66
|
+
Fbe.overwrite(f3, :foo, 42, fb:)
|
67
|
+
assert_equal(3, fb.size)
|
68
|
+
end
|
54
69
|
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.
|
4
|
+
version: 0.0.42
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yegor Bugayenko
|
@@ -230,6 +230,7 @@ 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
|
@@ -248,6 +249,7 @@ files:
|
|
248
249
|
- test/fbe/test_if_absent.rb
|
249
250
|
- test/fbe/test_issue.rb
|
250
251
|
- test/fbe/test_iterate.rb
|
252
|
+
- test/fbe/test_just_one.rb
|
251
253
|
- test/fbe/test_octo.rb
|
252
254
|
- test/fbe/test_overwrite.rb
|
253
255
|
- test/fbe/test_pmp.rb
|